⚡️ Speed up function extract_sql_commands by 12%
#408
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.
📄 12% (0.12x) speedup for
extract_sql_commandsinlitellm/proxy/db/check_migration.py⏱️ Runtime :
1.13 milliseconds→1.01 milliseconds(best of250runs)📝 Explanation and details
The optimized code achieves a 12% speedup by replacing inefficient string concatenation with list-based buffering and reducing redundant string operations.
Key optimizations:
Eliminated expensive string concatenation: The original code used
current_command += line + " "repeatedly, which creates new string objects each time. The optimized version uses abufferlist and only joins strings when commands are complete via' '.join(buffer).Reduced redundant
strip()operations: The original code stripped every line during initial parsing with[line.strip() for line in diff_output.split("\n") if line.strip()], then processed the already-stripped lines. The optimized version only strips non-empty lines once during processing, eliminating duplicate work.Improved empty line handling: Instead of filtering empty lines upfront, the optimized code uses
continueto skip them during iteration, avoiding the overhead of creating a filtered list.More efficient buffer management: Using
buffer.clear()is more efficient than string reassignment, and the list-based approach scales better with command length.The optimizations are particularly effective for:
These improvements make the function more efficient for parsing Prisma migration diffs, which often contain multiline SQL commands and significant whitespace/noise.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-extract_sql_commands-mho927zmand push.