⚡️ Speed up function detect_unused_helper_functions by 15% in PR #1166 (skyvern-grace)
#1169
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 #1166
If you approve this dependent PR, these changes will be merged into the original PR branch
skyvern-grace.📄 15% (0.15x) speedup for
detect_unused_helper_functionsincodeflash/context/unused_definition_remover.py⏱️ Runtime :
4.80 milliseconds→4.19 milliseconds(best of5runs)📝 Explanation and details
This optimization achieves a 14% runtime improvement (4.80ms → 4.19ms) through several targeted micro-optimizations that reduce overhead in hot code paths:
Key Performance Improvements
1. Eliminated Redundant Dictionary Lookups via Caching
In
CodeStringsMarkdownproperties (flat,file_to_path), the original code calledself._cache.get("key")twice per invocation. The optimized version caches the result in a local variable:This eliminates redundant hash table lookups in frequently accessed properties.
2. Replaced
dict.setdefault()for Atomic List OperationsIn
_analyze_imports_in_optimized_code, the original code used an if-check followed by assignment for the helpers dictionary:The
setdefault()approach reduces the operation to a single dictionary call, eliminating the membership test.3. Hoisted
as_posix()Calls Outside String FormattingIn the
markdownproperty, path conversion was moved outside the f-string:This avoids repeated method calls during string formatting.
4. Optimized Set Membership Tests with Early Exit
The most impactful change replaced
set.intersection()with short-circuit boolean checks:With ~200 helpers in large-scale tests, this avoids creating temporary sets for every comparison, showing 50% speedup in the large helper test (1.31ms → 868μs).
5. Minimized Repeated Attribute Access
Variables like
entrypoint_file_path,attr_name, andvalue_idare now cached before use, reducing attribute lookups in the AST traversal loop.Impact Based on Test Results
This optimization is particularly valuable when
detect_unused_helper_functionsis called repeatedly during code analysis pipelines, as the cumulative effect of these micro-optimizations scales with the number of helper functions and code blocks analyzed.✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
test_unused_helper_revert.py::test_async_class_methodstest_unused_helper_revert.py::test_async_entrypoint_with_async_helperstest_unused_helper_revert.py::test_async_generators_and_coroutinestest_unused_helper_revert.py::test_class_method_calls_external_helper_functionstest_unused_helper_revert.py::test_class_method_entrypoint_with_helper_methodstest_unused_helper_revert.py::test_detect_unused_helper_functionstest_unused_helper_revert.py::test_detect_unused_in_multi_file_projecttest_unused_helper_revert.py::test_mixed_sync_and_async_helperstest_unused_helper_revert.py::test_module_dot_function_import_styletest_unused_helper_revert.py::test_multi_file_import_stylestest_unused_helper_revert.py::test_nested_class_method_optimizationtest_unused_helper_revert.py::test_no_unused_helpers_no_reverttest_unused_helper_revert.py::test_recursive_helper_function_not_detected_as_unusedtest_unused_helper_revert.py::test_static_method_and_class_methodtest_unused_helper_revert.py::test_sync_entrypoint_with_async_helpers🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1166-2026-01-24T15.53.33and push.