Skip to content

perf: short-circuit equal strings in Python partial ratio - #514

Draft
moelomda wants to merge 1 commit into
rapidfuzz:mainfrom
moelomda:fix/equal-partial-ratio
Draft

moelomda wants to merge 1 commit into
rapidfuzz:mainfrom
moelomda:fix/equal-partial-ratio

Conversation

@moelomda

Copy link
Copy Markdown

Related to #430.

The Python implementation of partial_ratio_alignment compares prefixes before reaching the complete string, even when both strings are equal. Return the full-range alignment immediately for equal, exact built-in str or bytes inputs after preprocessing and sequence conversion. partial_ratio benefits through its existing delegation.

The exact-type guard deliberately leaves subclasses and other sequences on the existing path, so custom or ambiguous equality cannot replace element-wise scoring. It retains the cutoff check, processor calls, alignment coordinates, and existing nonempty perfect-score representation. Empty-input handling is unchanged. This only improves the Python fallback; it does not resolve the native implementation or interruption behavior discussed in #430.

Local measurements on Ubuntu 22.04 / Python 3.12.15, comparing the Python implementation at e5ba5955 with this change:

Input Before After
Equal, separately allocated 1,000-character strings 275 ms 1.1 microseconds
Equal, separately allocated 2,000-character strings 1.54 s 1.4 microseconds
Unequal one-character strings 3.8 microseconds 4.2 microseconds
Unequal eight-character strings 7.3 microseconds 7.6 microseconds

These are medians of five alternating-order samples on one machine, with the benchmark process pinned to one CPU. Other host activity was not excluded, so the small unequal-input differences are approximate. The equality check adds work when strings differ; this is not a claim of a speedup for every workload or for the native implementation. A minimal timing command to run on each revision is:

python -m timeit -r 5 -n 10 -s 'from rapidfuzz.fuzz_py import partial_ratio_alignment; a = "hello" * 200; b = a.encode().decode()' 'partial_ratio_alignment(a, b)'

Verification:

  • Added 38 correctness cases covering native/Python parity, Unicode, bytes, other sequences, cutoffs, preprocessing, and overridden subclass equality. These are behavior-preservation tests, not failing performance assertions.
  • A separate local differential probe compared 6,000 results, return types and exception outcomes across five affected scorers and ordinary/edge cutoffs, with no differences.
  • Built the native extensions from the same current source in Debug mode. With the new tests present in both runs, the full suite has identical failures on the unchanged implementation and this patch: native selection 580 passed / 1 failed; Python selection 577 passed / 4 failed. The shared failure is test_score_cutoff_equal_to_score[scorer9]; Python selection additionally fails the three test_extract*exceptions cases. The full suite is therefore not green locally.
  • All applicable repository pre-commit hooks on the two changed files and git diff --check pass. Documentation, packaging, other operating systems and other Python versions were not tested.

Draft pending review of that baseline limitation. Implemented, reviewed, and verified with OpenAI Codex assistance.

Avoid scanning prefixes when exact built-in strings or bytes match.
Keep custom sequence equality on the existing scoring path and cover
cutoffs, processor calls, full-range alignment and subclass equality.

Related to rapidfuzz#430.

Assisted-by: OpenAI Codex

This branch has not been deployed

No deployments
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