Skip to main content

Module maximal

Module maximal 

Source
Expand description

Folding overlapping seed matches back into maximal shared statement runs.

Statement windows slide with stride 1 over every block, so a shared run of n statements does not surface as one match — it surfaces as a fan of overlapping window matches, one per offset and per window length. Reporting those raw would bury a single duplicated block under a dozen findings that all describe the same code.

This stage reverses the sliding: seed matches that describe the same shared run are folded into the maximal run they jointly cover, so a duplicated block is one region no matter how many windows detected it.

§Why folding is sound

A window match means the two windows’ statement summaries are equal statement for statement. Two matches fold only when they agree on alignment — the same enclosing blocks on both sides and the same offset between them — and their runs touch. Under those conditions the equalities compose: if a[0..4] == b[2..6] and a[2..6] == b[4..8] then a[0..6] == b[2..8], because each statement of the union is covered by at least one of the two matches at the same relative position. No similarity is re-estimated here and nothing is approximated: the folded region is exactly as much of an exact match as the seeds it came from.

Gapped runs — a shared block interrupted by an edited statement — are not bridged here. Bridging a gap makes the region an approximate match, so it belongs behind the judge rather than in a fold that claims exactness.

§What a window hash does not see

A statement summary is its shape, its native kind and its leading token kinds — deliberately shallow, so the index stays cheap. A loop whose body is one line therefore summarises exactly like a loop whose body is forty, and two runs can match on summaries while covering wildly different amounts of code. That is not a duplicate of anything, so a seed whose two sides differ in source length by more than MaximalConfig::max_extent_ratio is dropped and counted: the size gap is the direct evidence that the summary hid the difference.

§One duplication, not every pair of its copies

A run copied into n places matches pairwise n * (n - 1) / 2 times, and every one of those pairs describes the same duplication. The stage therefore also reports SharedRegions: one entry per duplicated run holding all of its occurrences.

§Nesting

An inner block’s run sits inside its enclosing statement, so a duplicated loop body is also detected as part of the duplicated loop. The larger region is the one worth reporting, so a region whose source spans are contained in another region’s on both sides is absorbed into it and counted. Containment is indexed per file pair with a first-span sweep and two-dimensional Fenwick query, so a bucket of m folded regions costs O(m log² m), not O(m²).

Output is deterministic: regions are keyed and ordered by content position alone, and the fold never depends on the order seeds arrive in.

Structs§

CloneRegion
A maximal shared statement run between two units.
MaximalConfig
Tuning for region consolidation.
RegionSet
The consolidation stage’s output.
RegionSide
One side of a clone region: where the shared run sits in one unit.
RegionStats
What consolidation saw and dropped.
SharedRegion
One duplicated run and every place it occurs.

Constants§

DEFAULT_MAX_EXTENT_RATIO
Default largest source-length ratio between a seed’s two sides.
DEFAULT_MIN_STATEMENTS
Default minimum reportable region length, in statements: the shortest window length, so the floor never silently discards a run the seed layer could detect.
MAXIMAL_VERSION
Version of the maximal-region folding and containment rules.

Functions§

adjoins
Whether one run picks up exactly where the other stops, in the same block of the same unit.
consolidate
Fold statement-window seed matches into maximal shared runs.
intersects
Whether two byte ranges share at least one byte.