Skip to main content

SystemsDK

Blog Details

  • Home
  • Upcoming: PHP Copy/Paste Detector v9.2.0

Upcoming: PHP Copy/Paste Detector v9.2.0

🚀 Pre-release Teaser: Massive Performance Boost for Suffix Tree in phpcpd

Exciting news for the PHP community! In the upcoming release of `phpcpd` (Copy/Paste Detector), we’ve completely overhauled the performance of the Suffix Tree algorithm.

Recently, we made the engine much smarter by enabling it to parse and hash single-character operators (`+`, `-`, `=`, `{`, etc.). This eliminated false-positive clone detections between structurally identical methods with different math/logic. However, this dramatically increased token density, causing the Suffix Tree’s execution time to skyrocket (e.g., 22+ seconds on highly repetitive files) due to combinatorial explosions.

To fix this, we implemented two major architectural optimizations under the hood:

1️⃣ “Fast Path” for Exact Matches (O(N²) to O(N) reduction)
Historically, the engine allocated and calculated heavy 2D Levenshtein distance matrices even when looking for exact clones. We introduced a “Fast Path” early-exit strategy. Now, if the engine is searching for exact matches, it completely bypasses the dynamic programming matrix and uses direct token-by-token comparison.
📉 Result: Execution time on dense mathematical files dropped from ~22 seconds to fractions of a second (a 90%+ speedup).

2️⃣ Early Scanning Cutoff
The original search loop analyzed the file until the very last token. We introduced a mathematical cutoff: if the number of remaining unparsed tokens is less than the required minimum clone length, the engine immediately halts the traversal for that branch.
📉 Result: Even when scanning for fuzzy Type-3 clones (where the heavy matrix is required), this optimization cuts off impossible tail evaluations, reducing execution time by ~35-40% (e.g., from 22s down to 14s).

🤔 Why use Suffix Tree over Rabin-Karp for exact matches (`–edit-distance=0`)?
While Rabin-Karp is a fantastic, lightweight sliding-window algorithm for everyday CI/CD, the Suffix Tree offers unique structural advantages even without fuzzy matching:

  • Global Clustering: Instead of piecing together sequential pairwise overlaps, the Suffix Tree builds a global graph of the entire project, inherently clustering all identical code blocks into single nodes for a perfect topological map.
  • Maximal Boundaries: It naturally discovers the absolute longest common substrings in a single descent, rather than relying on fixed window shifts.

⚙️ How Your Input Parameters Drive Performance Now:
Understanding these limits will help you optimize your pipelines:

  •  `–edit-distance` (Default: 0): Leaving this at `0` keeps the engine in the lightning-fast O(N) “Fast Path”. Setting it > 0 enables Type-3 fuzzy clone detection, intentionally activating the heavy O(N²) Levenshtein matrix.
  •  `–min-tokens` (Default: 70): This is no longer just a post-scan filter! It now acts as a powerful runtime cutoff trigger. Higher values will significantly accelerate the Suffix Tree traversal.

We are planning to roll out this next release within the next 1-2 months. Stay tuned! 🐘💻