Skip to content

Optimize Cython LCGenerator and SyntheticSource KV generation - #40915

Open
Abacn wants to merge 2 commits into
apache:masterfrom
Abacn:synthetic-opt
Open

Abacn wants to merge 2 commits into
apache:masterfrom
Abacn:synthetic-opt

Conversation

@Abacn

@Abacn Abacn commented Oct 30, 2026

Copy link
Copy Markdown
Contributor
  • Inline _next_uint() in Cython LCGenerator (fast_test_utils.pyx) to eliminate two cpdef calls (next_uint -> seed) per 4-byte chunk in randbytes.

  • Generate key and value bytes separately in SyntheticSource._gen_kv_pair when gen_algo == 'lcg' and key_size % 4 == 0 to avoid allocating and slicing an intermediate combined bytes object while preserving identical output for any seed.

Benchmark results:

Benchmark (1,000,000 elements / run) Baseline Median (per element) Optimized Median (per element) Time Saved Speedup
LCGenerator_Key24_Val1000 (seed + random_sample + randbytes(1024)) 1.640e-06 sec (2.23% std) 3.653e-07 sec (3.18% std) -77.7% (-1,275 ns) 4.49x
SyntheticSourceGenKv_Key24_Val1000 (_gen_kv_pair) 1.815e-06 sec (5.45% std) 4.266e-07 sec (4.40% std) -76.5% (-1,389 ns) 4.25x
SyntheticSourceRead_Key24_Val1000 (source.read) 1.828e-06 sec (0.47% std) 7.581e-07 sec (5.03% std) -58.5% (-1,069 ns) 2.41x

- Inline `_next_uint()` in Cython `LCGenerator` (`fast_test_utils.pyx`) to eliminate two `cpdef` calls (`next_uint` -> `seed`) per 4-byte chunk in `randbytes`.

- Generate key and value bytes separately in `SyntheticSource._gen_kv_pair` when `gen_algo == 'lcg'` and `key_size % 4 == 0` to avoid allocating and slicing an intermediate combined `bytes` object while preserving identical output for any seed.
@github-actions github-actions Bot added python size/s S: 10-49 lines changed labels Oct 30, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📋 Review Summary

This PR optimizes Cython LCGenerator and SyntheticSource._gen_kv_pair by introducing an inlined cdef helper _next_uint (avoiding cpdef overhead in tight loops) and generating key/value bytes directly when gen_algo == 'lcg' and key_size is 4-byte aligned. The changes are mathematically sound, preserve bit-for-bit identical output for any seed, and deliver substantial performance improvements.

🔍 General Feedback

  • Inlining _next_uint with noexcept nogil in fast_test_utils.pyx cleanly eliminates the repeated cpdef call overhead in randbytes, next_int, next_uint, and random_sample.
  • The _gen_kv_separately condition (self.gen_algo == 'lcg' and self._key_size % 4 == 0) accurately ensures that splitting the randbytes call does not alter the PRNG state progression compared to slicing a single combined buffer.
  • Consider adding a unit test in synthetic_pipeline_test.py covering algorithm='lcg' with both 4-byte aligned and unaligned keySizeBytes to explicitly guard the new _gen_kv_separately branch.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

@codecov

codecov Bot commented Oct 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.08%. Comparing base (12bbb17) to head (f9f955b).
⚠️ Report is 12 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #40915      +/-   ##
==========================================
+ Coverage   79.83%   80.08%   +0.24%     
==========================================
  Files         494      494              
  Lines       83575    83585      +10     
==========================================
+ Hits        66722    66935     +213     
+ Misses      16853    16650     -203     
Flag Coverage Δ
python 80.08% <100.00%> (+0.24%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @jrmccluskey for label python.
Backup reviewer: tvalentyn (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant