⚡️ Speed up function get_test_info_from_stack by 1,809% in PR #1014 (feat/extract-imported-class-definitions)
#1016
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 #1014
If you approve this dependent PR, these changes will be merged into the original PR branch
feat/extract-imported-class-definitions.📄 1,809% (18.09x) speedup for
get_test_info_from_stackincodeflash/verification/codeflash_capture.py⏱️ Runtime :
29.0 milliseconds→1.52 milliseconds(best of62runs)📝 Explanation and details
The optimized code achieves an 18x speedup by eliminating two major bottlenecks identified in the line profiler results:
Key Optimizations
1. Replaced
inspect.getmodule(frame)with direct dictionary lookup (45.6% → 2.1% of runtime)inspect.getmodule(frame)which traverses sys.modules and performs expensive introspectionframe.f_globals.get("__name__", None)to directly access the module name from the frame's global namespace2. Optimized path containment check (49.5% → 0.3% of runtime)
Path(filename).resolve().is_relative_to(Path(tests_root))on every frame, creating Path objects and resolving symlinks repeatedlytests_root_resolvedonce usingos.path.normcase(os.path.abspath(tests_root))os.path.commonpath()with normalized strings for fast comparison3. Minor optimization in module name check
frame.f_globalsin a local variablefgto avoid repeated attribute lookups__name__retrievalWhy This Matters in Context
Based on the function_references,
get_test_info_from_stack()is called on every test invocation within a wrapper that:Since this function executes in the hot path of test execution, any overhead directly impacts test suite runtime. The optimization is especially valuable for:
test_large_scale_repeated_calls: 27.6ms → 1.37ms for 500 calls)Test Case Performance Patterns
The annotated tests show consistent 8-20x speedups across different scenarios:
test_basic_test_function_detection: 80.4μs → 8.97μs)The optimization works best when stack walking finds test functions quickly (avoiding the fallback to environment variables), which is the common case in normal test execution.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1014-2026-01-07T22.27.39and push.