perf: replace string concatenation with arithmetic in DecimalType.serialize (hundreds of ns, x1.3-2.6 speedup) - #764
Conversation
a2f4e20 to
a08da22
Compare
Equivalence verificationRebased onto current Checked directly against the live
Result: all of the above match byte-for-byte between old and new implementations. Added an explicit One genuine (non-blocking) behavioral difference found: for CI / review statusAll CI checks pass (unit tests across all supported Python versions/event loops, wheel builds, build, snyk). No reviews or review threads on the PR to resolve. Test results
Amended the fix into the existing commit and force-pushed (kept as draft). |
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: trueThanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
Optimizes decimal serialization by replacing string conversion with arithmetic accumulation.
Changes:
- Adds multiply-and-add digit conversion.
- Tests zero serialization.
- Adds performance and round-trip benchmarks.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
cassandra/cqltypes.py |
Optimizes decimal digit conversion. |
tests/unit/test_marshalling.py |
Adds zero-value coverage. |
benchmarks/decimal_serialize.py |
Adds serialization benchmarks. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| ("medium (10 digits)", Decimal("1234567890.0123456789")), | ||
| ("large (30 digits)", Decimal("1" * 30 + ".0")), |
…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
Signed-off-by: Yaniv Michael Kaul <yaniv.kaul@scylladb.com>
a08da22 to
babb234
Compare
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