Skip to content

Speed up character scoring paths (v6.0.4) - #40

Open
ycherkes wants to merge 4 commits into
masterfrom
perf/char-scoring-fast-paths
Open

ycherkes wants to merge 4 commits into
masterfrom
perf/char-scoring-fast-paths

Conversation

@ycherkes

Copy link
Copy Markdown
Contributor

Summary

Speeds up the character scoring paths (Ratio, PartialRatio, TokenSetRatio, WeightedRatio and extraction) and bumps the package to 6.0.4. Scores, alignment tie order, the generic IEquatable<T> API, cached scorers and all 12 target frameworks are unchanged: the 388-pair scoring matrix matches 6.0.3 (ac2e0a2) exactly and Lodestar within 1e-9.

This branch also carries the benchmark harness commit (1376f8b) that the measurements below use.

Changes

  • Short inputs (≤ 64 UTF-16 units): stack-backed character masks for Ratio (direct ASCII index, bounded open addressing otherwise) and a score-only PartialRatio path; the generic implementation stays the authority for alignment coordinates and ties.
  • Long inputs: a direct char LCS kernel on the pattern vector instead of one interface call per symbol: register state for one and two words, stack state above that, absent symbols skipped and a branch-free word carry (u | (s & ~sum)) >> 63.
  • Pattern tables: tables for patterns up to 4,096 units come from two per-thread slots and are returned clean by clearing only populated entries. Symbols above Latin-1 use an open-addressed index instead of a pooled dictionary.
  • PartialRatio:
    • prefix/suffix scans are skipped when the exact outer-window scores prove no edge can beat the best window;
    • inputs with disjoint alphabets exit early (bounded to length ratios ≤ 8);
    • the window queue lives in the pooled scores buffer instead of two lists, keeping breadth-first order.
  • WeightedRatio: exact upper-bound exits (stateless and cached), shared token preparation, and span-based token sets on .NET 6+ (≤ 16 raw tokens and ≤ 256 combined units). Culture-sensitive sorting and char.IsWhiteSpace are preserved.
  • Stack zeroing: each short stack table is zeroed once. [SkipLocalsInit] sits on the short partial-ratio paths and on the Unicode branch of short Ratio, with an internal polyfill for older targets; AllowUnsafeBlocks is required for the attribute, and no unsafe code is added.

Results

Measured on an Intel Core Ultra 9 275HX performance core (logical CPU 10), Windows 11, .NET 10.0.15, BenchmarkDotNet 0.15.8, tiering disabled, two launches, no outlier removal. Before = 6.0.3 sources, Lodestar = 52d30ea. Selected rows:

Scenario Operation Before Current Lodestar Allocated B / C / L
Original Ratio 113.1 ns 53.4 ns 56.9 ns 80 / 0 / 0 B
Original PartialRatio 776.0 ns 301.9 ns 544.9 ns 160 / 0 / 0 B
Original TokenSetRatio 611.6 ns 160.5 ns 492.9 ns 1664 / 0 / 896 B
Original WeightedRatio 2,158.3 ns 679.0 ns 635.3 ns 2816 / 0 / 1120 B
0016-Cyrillic-Equal Ratio 284.7 ns 31.2 ns 239.0 ns 144 / 0 / 0 B
0064-ASCII-Near PartialRatio 1,257.4 ns 526.4 ns 2,487.3 ns 256 / 0 / 0 B
0128-ASCII-Near PartialRatio 4,935.6 ns 1,656.9 ns 27,130.4 ns 336 / 80 / 0 B
0128-ASCII-Near Ratio 981.8 ns 599.8 ns 439.8 ns 80 / 80 / 0 B
0256-Mixed-Equal PartialRatio 10,928.2 ns 8,858.0 ns 7,382.7 ns 576 / 352 / 0 B
1024-Mixed-Near Ratio 59.6 µs 35.6 µs 38.1 µs 144 / 80 / 0 B
1024-Mixed-Near PartialRatio 496.0 µs 169.5 µs 7,397.6 µs 464 / 89 / 0 B
1024-Mixed-Near WeightedRatio 787.5 µs 204.6 µs 22,247.9 µs 1639 / 176 / 16723 B
ExtractOne, 1,000 choices — 122.0 µs 59.6 µs 63.0 µs 80176 / 176 / 32 B
  • Scoring matrix: across 44 workloads current wins 35 and loses 8 against Lodestar with separated confidence intervals. No current mean is over 5% slower than Before.
  • Remaining losses: 65–128-unit ASCII Ratio/TokenSetRatio, 256-unit equal-length PartialRatio/Ratio, 64-unit ASCII Ratio and the original-phrase WeightedRatio.
  • Allocation footnote: the vector object was trimmed after the full run, so rows the report shows as 88 B now allocate 80 B; the table above already shows 80 B.
  • Full report: CharFastPaths-README.md has every row, the default-JIT check and the measured decisions.

Robustness checks

  • Default JIT / dynamic PGO (50 warmups, six scenarios and extraction) shows the same picture. Short default-JIT runs are misleading: tier-up lands inside measured iterations, as documented in the report.
  • Efficiency core (logical CPU 5, different microarchitecture): 38 of 44 verdicts against Lodestar match the performance core, and nothing is over 5% slower than Before. Short PartialRatio (8 units, 16-unit equal) is where the cores disagree.
  • Not covered: AMD, ARM64, .NET 8 and .NET Framework runtimes.

Measured decisions

  • Stack zeroing. A first version cleared the short ASCII table twice: runtime zeroing plus an explicit AVX Span.Clear. Depending on what else had run in the process, 1,000-choice extraction took 64.8–74.5 µs; with a single runtime zeroing it was 59.1–60.4 µs in every context. With AVX disabled the related Unicode penalty disappeared (129 → 39 ns), which points to an SSE/AVX transition between the native zeroing helper and nearby 256-bit code.
  • Kept duplicate window search. Routing 9–64-unit unequal-length PartialRatio through the generic search would remove about 125 lines, but measured +13–36% and 80 B per call. Short queries in longer text are the main use, so the duplicate stays.

Test plan

  • Release build of all 12 library targets (Raffinert.FuzzySharp.6.0.4.nupkg packs).
  • dotnet test: 5,320 tests each on .NET 8 and .NET 10, 5,313 each on .NET Framework 4.6.2 and 4.7.2.
  • New tests compare against independent dynamic-programming references. They cover:
    • the char LCS kernel at 1–4,097 units, including NUL, Latin-1, surrogates and hash collisions;
    • edge skips at 9–1,025 units, with the winning prefix or suffix kept;
    • per-thread table cleanliness, double dispose and the non-Latin-1 index growing to 5,000 distinct symbols;
    • token preparation under five cultures and skipped InitLocals on every target.
  • Mutation checks (weakened edge bounds, broken carry, skipped table cleanup, wrong rehash mask) each fail the suite.
  • Benchmark setup re-verifies all 388 scores against Before and Lodestar before timing.

🤖 Generated with Claude Code

ycherkes and others added 2 commits November 28, 2026 14:38
Short Ratio and score-only PartialRatio use stack-backed character masks.
Long patterns use a direct char LCS kernel (register state for one and two
words, branch-free carry) with per-thread clean pattern tables and an
open-addressed index for non-Latin-1 symbols, replacing a dictionary and an
interface call per symbol. PartialRatio skips prefix/suffix scans that the
exact outer-window scores prove cannot win, exits early for disjoint
alphabets, and keeps its window queue in the pooled scores buffer.
WeightedRatio gains exact upper-bound exits and shared token preparation,
with span-based token sets on .NET 6+.

Scores, alignment tie order and the generic API are unchanged; the
388-pair matrix matches 6.0.3 exactly. Each short stack table is zeroed
once: a second clear measured unstable and up to 15% slower.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-28T21:23:05.901086Z d5f41ed PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

ycherkes and others added 2 commits November 28, 2026 23:00
The window queue lived in the scores rental for every input (five ints per
window), so a 65-unit pattern against a 1,000,065-unit text allocated
33,563,888 B on a cold pool against 4,208,712 B for 6.0.3. Up to 256
windows the queue still shares that rental; above it a separate queue grows
between breadth-first levels with the windows actually queued (4,204,040 B
for the same input). Growth stays out of the window loop and the method's
locals are kept minimal: one more local stopped the JIT inlining it, which
cost about 30 ns on 8-unit patterns.

Compare-Lodestar.ps1 now defaults to 50 warmups with -DefaultJit, the count
the 1024-unit workloads needed to finish tiering up. The non-Latin-1 index
test now builds its vector from all 5,000 symbols instead of 600. New tests
cover the cold long-text allocation and unpruned window levels in both
queue modes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
PartialRatio is 100 exactly when the shorter string occurs in the longer
one, so the string entry point checks that ordinally before building masks
or renting buffers: contained or equal 8-128 unit inputs take 6-16 ns
instead of 46-1,667 ns, and a 65-unit pattern at the start of a
1,000,065-unit text allocates 3,312 B instead of about 4 MB. Inputs without
an exact occurrence pay +1-4% for the scan.

Cached WeightedRatio now skips token sort/set when the query and the
candidate are both single tokens, as stateless scoring already did (one-token
query over 1,000 one-token candidates: 226 -> 160 us, 36 KB -> 4 KB).
Cached token-set scoring returns 0 for an empty query before splitting the
candidate. New tests compare each shortcut with the full computation.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant