⚡️ Speed up method FunctionRanker.get_function_stats_summary by 2,062% in PR #970 (ranking-changes)
#971
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 #970
If you approve this dependent PR, these changes will be merged into the original PR branch
ranking-changes.📄 2,062% (20.62x) speedup for
FunctionRanker.get_function_stats_summaryincodeflash/benchmarking/function_ranker.py⏱️ Runtime :
1.48 milliseconds→68.6 microseconds(best of115runs)📝 Explanation and details
The optimization replaces an O(N) linear search through all functions with an O(1) hash table lookup followed by iteration over only matching function names.
Key Changes:
_function_stats_by_nameindex in__init__that maps function names to lists of (key, stats) tuplesget_function_stats_summaryto first lookup candidates by function name, then iterate only over those candidatesWhy This is Faster:
The original code iterates through ALL function stats (22,603 iterations in the profiler results) for every lookup. The optimized version uses a hash table to instantly find only the functions with matching names, then iterates through just those candidates (typically 1-2 functions).
Performance Impact:
test_large_scale_performancecase with 900 functions shows 3085% speedup (66.7μs → 2.09μs)When This Optimization Shines:
The optimization maintains identical behavior while transforming the algorithm from O(N) per lookup to O(average functions per name) per lookup, which is typically O(1) in practice.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr970-2025-12-14T17.16.41and push.