⚡️ Speed up function should_modify_pyproject_toml by 146% in PR #487 (better-UX)
#492
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 #487
If you approve this dependent PR, these changes will be merged into the original PR branch
better-UX.📄 146% (1.46x) speedup for
should_modify_pyproject_tomlincodeflash/cli_cmds/cmd_init.py⏱️ Runtime :
266 milliseconds→108 milliseconds(best of11runs)📝 Explanation and details
Here are targeted and safe optimizations for your code, focusing primarily on the parse_config_file function, since it dominates the runtime (~98% of
should_modify_pyproject_toml).The main bottlenecks per the profile are both TOML parsing (external, little to be optimized from user code) and the massive number of slow in-place config key conversions (
config[key.replace("-", "_")] = config[key]; del config[key]).Most of the
key in configlookups and repeated work can be reduced by processing keys more efficiently in fewer iterations.Key Optimizations:
Single Pass Normalization:
Instead of scanning the dictionary repeatedly converting hyphens to underscores, process the keys in-place in a single pass, creating a new dict with both normalized and original keys pointing to the same value, replacing
config.This is faster and safe.
Batch Default Handling:
Instead of sequentially modifying for each key + default type, merge in default values for all missing keys at once using
.setdefault.Avoid Excessive Path Conversion/Resolving:
Convert/resolve each path once, only if present, and do not build new
Pathobjects multiple times.Minimize Repeated
Path(...).parentCalculations:Compute parent once.
Optimize
[str(cmd) for cmd in config[key]]:Move path computations and casting to lists earlier, minimize unnecessary transformations.
Re-use objects and variables rather than repeated lookups.
Pre-filter config keys for path work.
No changes to behavior or function signatures.
All existing comments are kept where relevant.
Here is your optimized, drop-in replacement.
Summary of changes:
This version will significantly reduce the overhead in
parse_config_filedue to a much more efficient key normalization and default merging logic.If you want even more speed, consider switching from
tomlkittotomllibfor TOML parsing if you do not require preservation of comments or formatting.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr487-2025-07-03T00.46.51and push.