⚡️ Speed up method CodeFlashBenchmarkPlugin.write_benchmark_timings by 111% in PR #217 (proper-cleanup)#219
Closed
codeflash-ai[bot] wants to merge 6 commits into
Closed
Conversation
… by 111% in PR #217 (`proper-cleanup`) Here's a rewritten, optimized version of your program, focusing on what the line profile indicates are bottlenecks. - **Reuse cursor**: Opening a new cursor repeatedly is slow. Maintain a persistent cursor. - **Batching commits**: Commit after many inserts if possible. However, since you clear the buffer after each write, one commit per call is necessary. - **Pragma optimizations**: Set SQLite pragmas (`synchronous = OFF`, `journal_mode = MEMORY`) for faster inserts if durability isn't paramount. - **Avoid excessive object recreation**: Only connect if needed, and clear but *do not reallocate* the benchmark list. - **Reduce exception handling cost**: Trap and re-raise only actual DB exceptions. **Note:** For highest speed, `executemany` and single-transaction-batch inserts are already optimal for SQLite. If even faster, use `bulk insert` with `INSERT INTO ... VALUES (...), (...), ...`, but this requires constructing SQL dynamically. Here’s the optimized version. **Key points:** - `self._ensure_connection()` ensures both persistent connection and cursor. - Pragmas are set only once for connection. - Use `self.benchmark_timings.clear()` to avoid list reallocation. - The cursor is reused for the lifetime of the object. **If your stability requirements are stricter** (durability required), remove or tune the PRAGMA statements. If you want even higher throughput and can collect many queries per transaction, consider accepting a "bulk flush" mode to reduce commit frequency, but this requires API change. This code preserves your public API and all comments, while running considerably faster especially on large inserts.
Contributor
|
i like this actually... |
48716a1 to
0ba52ea
Compare
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.
⚡️ This pull request contains optimizations for PR #217
If you approve this dependent PR, these changes will be merged into the original PR branch
proper-cleanup.📄 111% (1.11x) speedup for
CodeFlashBenchmarkPlugin.write_benchmark_timingsincodeflash/benchmarking/plugin/plugin.py⏱️ Runtime :
26.9 milliseconds→12.8 milliseconds(best of121runs)📝 Explanation and details
Here's a rewritten, optimized version of your program, focusing on what the line profile indicates are bottlenecks.
synchronous = OFF,journal_mode = MEMORY) for faster inserts if durability isn't paramount.Note: For highest speed,
executemanyand single-transaction-batch inserts are already optimal for SQLite. If even faster, usebulk insertwithINSERT INTO ... VALUES (...), (...), ..., but this requires constructing SQL dynamically.Here’s the optimized version.
Key points:
self._ensure_connection()ensures both persistent connection and cursor.self.benchmark_timings.clear()to avoid list reallocation.If your stability requirements are stricter (durability required), remove or tune the PRAGMA statements. If you want even higher throughput and can collect many queries per transaction, consider accepting a "bulk flush" mode to reduce commit frequency, but this requires API change.
This code preserves your public API and all comments, while running considerably faster especially on large inserts.
✅ Correctness verification report:
🌀 Generated Regression Tests Details
To edit these changes
git checkout codeflash/optimize-pr217-2025-05-19T03.55.14and push.