⚡️ Speed up function filter_sensitive_headers by 213% #10
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.
📄 213% (2.13x) speedup for
filter_sensitive_headersinsrc/deepgram/extensions/core/telemetry_events.py⏱️ Runtime :
3.64 milliseconds→1.16 milliseconds(best of305runs)📝 Explanation and details
The optimization replaces
any(key_lower.startswith(prefix) for prefix in sensitive_prefixes)with the more efficientkey_lower.startswith(sensitive_prefixes).Key Change:
str.startswith()method natively accepts a tuple of prefixes, eliminating the need for a generator expression andany()function call.Why This is Faster:
any(), which involves Python's iterator protocol overheadstartswith()calls wrapped inany(), there's a singlestartswith()call that handles the tuple internally in optimized C codePerformance Impact:
The line profiler shows the prefix checking line (
if key_lower.startswith...) dropped from 65.3% of total runtime (13.57ms) to 23.6% (2.12ms) - a ~6.4x improvement on this specific line. This optimization is particularly effective for:The overall 212% speedup demonstrates how optimizing the most expensive operation (prefix checking) in a tight loop can dramatically improve performance across all test scenarios.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_d0k9fm5y/tmpdeqw_shz/test_concolic_coverage.py::test_filter_sensitive_headerscodeflash_concolic_d0k9fm5y/tmpdeqw_shz/test_concolic_coverage.py::test_filter_sensitive_headers_2To edit these changes
git checkout codeflash/optimize-filter_sensitive_headers-mh2vh80fand push.