⚡️ Speed up function _get_ignore_patterns by 589%
#123
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.
📄 589% (5.89x) speedup for
_get_ignore_patternsinsrc/diffusers/pipelines/pipeline_loading_utils.py⏱️ Runtime :
22.0 milliseconds→3.19 milliseconds(best of204runs)📝 Explanation and details
Here is an optimized version of your code. The line profiler output makes it clear the main hot spots are in
is_safetensors_compatible, especially in.folder_names(OS split in a set comprehension over thousands of files)"/"in a loopos.path.splitextcalled in a loop for every fileThe main optimizations.
Code comments are preserved where relevant.
Summary of speedups:
/)..endswithfor extensions in tight loops, which is much faster thanos.path.splitext.You can expect this code to use much less time and memory in
is_safetensors_compatible(the main hotspot) for large input lists.✅ Correctness verification report:
🌀 Generated Regression Tests Details
To edit these changes
git checkout codeflash/optimize-_get_ignore_patterns-mbdbuzp3and push.