⚡️ Speed up function build_default_config by 41%
#218
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.
📄 41% (0.41x) speedup for
build_default_configinstanza/pipeline/core.py⏱️ Runtime :
1.56 milliseconds→1.11 milliseconds(best of65runs)📝 Explanation and details
The optimization achieves a 40% speedup through several targeted performance improvements:
Key Optimizations:
Localized
os.path.join: Storedos.path.joinas a local variable (join_path) to avoid repeated global name lookups, reducing overhead in the hot path where path construction happens frequently.Path prefix caching: Pre-computed the model directory prefix (
mpfx = join_path(model_dir, lang, processor)) and reused it with f-string formatting (f"{mpfx}/{ms.package}.pt") instead of callingos.path.joinrepeatedly for each model path.Optimized dictionary lookup in
build_default_config_option: UsedPROCESSOR_VARIANTS.get()with a null check instead of direct indexing, avoiding potential KeyError handling overhead and making the variant check more efficient.Reduced function call overhead: Replaced
os.path.join()calls with f-string concatenation where path prefixes are already constructed, eliminating function call overhead in tight loops.Variable caching: Cached
dependencies[0]asdeps_0andmodel_specs[0]asmodel_spec_0to avoid repeated list indexing.Performance Impact by Test Type:
The line profiler shows the most significant time reductions in path construction operations (lines with
os.path.joincalls), which were the primary bottlenecks consuming 15-35% of total execution time.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
⏪ Replay Tests and Runtime
To edit these changes
git checkout codeflash/optimize-build_default_config-mh4exzdland push.