Skip to content

perf: replace string concatenation with arithmetic in DecimalType.serialize#764

Draft
mykaul wants to merge 1 commit intoscylladb:masterfrom
mykaul:perf/decimal-serialize-arithmetic
Draft

perf: replace string concatenation with arithmetic in DecimalType.serialize#764
mykaul wants to merge 1 commit intoscylladb:masterfrom
mykaul:perf/decimal-serialize-arithmetic

Conversation

@mykaul
Copy link
Copy Markdown

@mykaul mykaul commented Mar 25, 2026

Summary

  • Replace int(''.join([str(d) for d in digits])) with a multiply-and-add loop in DecimalType.serialize()
  • Eliminates per-digit str() allocations and the intermediate joined string

Details

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

unscaled = int(''.join([str(digit) for digit in digits]))

After

unscaled = 0
for digit in digits:
    unscaled = unscaled * 10 + digit

Benchmark results (isolated digit-to-integer conversion)

Digit count String join (us) Arithmetic (us) Speedup
3 0.215 0.081 2.66x
20 0.900 0.582 1.55x
31 1.296 0.964 1.34x

Benchmark script included at benchmarks/decimal_serialize.py.

Testing

  • All 651 existing unit tests pass (16 pre-existing skips)
  • Correctness verified via round-trip serialize/deserialize for small, medium, and large Decimal values

…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
@mykaul mykaul marked this pull request as draft March 25, 2026 20:32
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