Skip to content

perf: cache apply_parameters to avoid repeated type() class creation (~5us saving - 30x speedup per call)#794

Draft
mykaul wants to merge 1 commit intoscylladb:masterfrom
mykaul:perf/cache-apply-parameters
Draft

perf: cache apply_parameters to avoid repeated type() class creation (~5us saving - 30x speedup per call)#794
mykaul wants to merge 1 commit intoscylladb:masterfrom
mykaul:perf/cache-apply-parameters

Conversation

@mykaul
Copy link
Copy Markdown

@mykaul mykaul commented Apr 5, 2026

Summary

  • Cache the results of _CassandraType.apply_parameters() in a class-level dict keyed by (cls, subtypes, names)
  • Avoids the expensive type() metaclass machinery on repeated calls with the same type signature (common case during result-set deserialization)
  • 10-line change in cassandra/cqltypes.py

Benchmark

Measured with min() of timeit.repeat(repeat=7, number=200_000) on a quiet machine (load <1).

Scenario Per-call Speedup
Uncached (type() each time) 5521 ns baseline
Cached (cache hit) 183 ns 30.2x

Savings: 5339 ns/call

Tests

  • 11 focused unit tests covering cache hits, different subtypes/names, cache clearing, tuple/list input equivalence
  • Full unit test suite passes (656 passed)

Cache the results of _CassandraType.apply_parameters() in a class-level
dict keyed by (cls, subtypes, names). This avoids the expensive type()
metaclass machinery on repeated calls with the same type signature,
which is the common case during result-set deserialization.

Benchmark: 31.7x speedup (6.48 us/call -> 0.20 us/call) for cached hits.
@mykaul mykaul force-pushed the perf/cache-apply-parameters branch from 90c77b5 to 5d25b9b Compare April 5, 2026 17:27
@mykaul mykaul changed the title perf: cache apply_parameters to avoid repeated type() class creation perf: cache apply_parameters to avoid repeated type() class creation (~5us saving - 30x speedup per call) Apr 7, 2026
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