⚡️ Speed up function full_path_exists by 47%
#72
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.
📄 47% (0.47x) speedup for
full_path_existsinwandb/integration/kfp/kfp_patch.py⏱️ Runtime :
3.97 milliseconds→2.70 milliseconds(best of46runs)📝 Explanation and details
The optimization achieves a 47% speedup through two key improvements:
1. LRU Caching in
get_module():@lru_cache(maxsize=128)decorators to separate cached functions for lazy and non-lazy module importsimport_module_lazy()calls for the same module namesget_moduletime reduced from 20.1ms to 18.9ms, with the cached import line dropping from 92.7% to 92.4% of function timefull_path_existsrepeatedly checks the same parent modules (e.g., "os" then "os.path")2. Streamlined Logic in
full_path_exists():get_parent_child_pairs()function and its list construction overheadrange(1, len(split))and slice operationswandb.util.get_modulelookup in a local variable to avoid repeated attribute lookupshasattr(module, child) or getattr(module, child) is Noneinto clearer separategetattr(module, child, None)andif attr is NonechecksPerformance Benefits by Test Type:
The caching is most effective when the same modules are accessed repeatedly, which is common in path validation scenarios where parent modules like "os", "sys", etc. are checked multiple times.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-full_path_exists-mhdlsmhaand push.