perf: replace string concatenation with arithmetic in DecimalType.serialize#764
Draft
mykaul wants to merge 1 commit intoscylladb:masterfrom
Draft
perf: replace string concatenation with arithmetic in DecimalType.serialize#764mykaul wants to merge 1 commit intoscylladb:masterfrom
mykaul wants to merge 1 commit intoscylladb:masterfrom
Conversation
…ialize
Replace int(''.join([str(d) for d in digits])) with a multiply-and-add
loop to convert Decimal digit tuples to integers. This avoids creating
per-digit string objects and the intermediate joined string.
Benchmark results (isolated digit conversion):
- 3 digits: 2.66x faster
- 20 digits: 1.55x faster
- 31 digits: 1.34x faster
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
int(''.join([str(d) for d in digits]))with a multiply-and-add loop inDecimalType.serialize()str()allocations and the intermediate joined stringDetails
Decimal.as_tuple()returns digits as a tuple of ints (0-9). The previous code converted each digit to a string, joined them, and parsed the result back to int. The new code uses simple arithmetic:n = n * 10 + digit.Before
After
Benchmark results (isolated digit-to-integer conversion)
Benchmark script included at
benchmarks/decimal_serialize.py.Testing