perf: use stdlib bisect and attrgetter in tablets.py#757
Open
mykaul wants to merge 1 commit intoscylladb:masterfrom
Open
perf: use stdlib bisect and attrgetter in tablets.py#757mykaul wants to merge 1 commit intoscylladb:masterfrom
mykaul wants to merge 1 commit intoscylladb:masterfrom
Conversation
- Use bisect.bisect_left from stdlib on Python >= 3.10 (C implementation) instead of the bundled pure-Python copy; 1.3-2.1x faster for key= lookups, up to 6x faster for plain int searches - Replace per-call lambda closures with module-level operator.attrgetter for first_token/last_token extraction - Fix bug in the pure-Python bisect_left fallback (Python < 3.10): the key=None branch executed bare 'return' (returning None) instead of 'return lo' - Move bisect_left definition before the classes that use it - Add unit tests for the fallback bisect_left and get_tablet_for_key Benchmark results (get_tablet_for_key hit): 10 tablets: 517 ns -> 365 ns (1.42x) 100 tablets: 616 ns -> 351 ns (1.75x) 1000 tablets: 1008 ns -> 529 ns (1.91x) 10000 tablets: 1339 ns -> 610 ns (2.20x)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
bisect.bisect_lefton Python >= 3.10 (C implementation) instead of the bundled pure-Python copy. The fallback is retained for Python < 3.10 wherekey=is not supported.operator.attrgetterforfirst_token/last_tokenextraction, avoiding repeated function-object allocation.bisect_leftfallback: thekey=Nonebranch executed barereturn(returningNone) instead ofreturn lo. This bug is currently latent (all callers passkey=), but would cause silent incorrect behavior ifbisect_leftwere ever called withoutkey.bisect_left(7 tests) andget_tablet_for_key(3 tests).Benchmark Results
Measured on Intel i7-1270P, Python 3.14.3,
pytest-benchmark.get_tablet_for_key(hit — the primary hot path)bisect_leftwithkey=(isolated)bisect_leftwithoutkey=(plain ints — bug fix path)The full
TokenAwarePolicy.make_query_planwith tablet lookup shows minimal change because it is dominated by other costs (policy iteration, host filtering), but theget_tablet_for_keycomponent is substantially faster.