⚡️ Speed up function translate_log_level by 121%#46
Open
codeflash-ai[bot] wants to merge 1 commit intomainfrom
Open
⚡️ Speed up function translate_log_level by 121%#46codeflash-ai[bot] wants to merge 1 commit intomainfrom
translate_log_level by 121%#46codeflash-ai[bot] wants to merge 1 commit intomainfrom
Conversation
The optimization achieves a **120% speedup** by replacing expensive string-based lookups with a direct integer-to-integer dictionary mapping. ## Key Changes **Original approach:** 1. Calls `logging.getLevelName(level)` - a function that converts numeric levels to strings (56.2% of runtime) 2. Performs two list membership checks with string comparisons (`in ["NOTSET", "DEBUG", ...]`) 3. Initializes a variable and conditionally assigns values **Optimized approach:** 1. Pre-computes a static dictionary `_LOG_LEVEL_TO_ONNX` mapping integer log levels directly to ONNX levels 2. Performs a single `dict.get()` operation with default fallback ## Why This Is Faster 1. **Eliminates expensive `getLevelName()` call**: The original spent >56% of its time converting integers to strings. The optimization removes this entirely by working directly with integer keys. 2. **O(1) dictionary lookup vs O(n) list scanning**: Dictionary lookups are constant-time hash operations, while list membership checks require linear scanning and string comparisons. The line profiler shows 12.2% + 6.8% of time spent on these checks. 3. **Reduced operations**: The optimized version executes one dictionary lookup vs 4-5 operations (function call, variable initialization, two conditional checks with list scans). ## Test Results Analysis - **Standard levels** (NOTSET, DEBUG, INFO, etc.): 67-150% faster, as these are the primary use case - **Unknown/custom levels**: 200-400% faster due to avoiding the `getLevelName()` fallback behavior for non-standard integers - **Bulk operations**: The 500-call performance test shows 97% improvement, demonstrating the optimization scales well with high call volumes ## Impact Considerations Since `translate_log_level` is a logging utility function, it's likely called frequently during application runtime, especially in hot paths where logging decisions are made. The optimization is particularly beneficial for: - High-throughput applications with frequent log level translations - Scenarios with many custom/unknown log levels (where the original code would construct "Level N" strings) - Initialization and configuration code that processes multiple log levels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
📄 121% (1.21x) speedup for
translate_log_levelinunstructured_inference/logger.py⏱️ Runtime :
690 microseconds→313 microseconds(best of115runs)📝 Explanation and details
The optimization achieves a 120% speedup by replacing expensive string-based lookups with a direct integer-to-integer dictionary mapping.
Key Changes
Original approach:
logging.getLevelName(level)- a function that converts numeric levels to strings (56.2% of runtime)in ["NOTSET", "DEBUG", ...])Optimized approach:
_LOG_LEVEL_TO_ONNXmapping integer log levels directly to ONNX levelsdict.get()operation with default fallbackWhy This Is Faster
Eliminates expensive
getLevelName()call: The original spent >56% of its time converting integers to strings. The optimization removes this entirely by working directly with integer keys.O(1) dictionary lookup vs O(n) list scanning: Dictionary lookups are constant-time hash operations, while list membership checks require linear scanning and string comparisons. The line profiler shows 12.2% + 6.8% of time spent on these checks.
Reduced operations: The optimized version executes one dictionary lookup vs 4-5 operations (function call, variable initialization, two conditional checks with list scans).
Test Results Analysis
getLevelName()fallback behavior for non-standard integersImpact Considerations
Since
translate_log_levelis a logging utility function, it's likely called frequently during application runtime, especially in hot paths where logging decisions are made. The optimization is particularly beneficial for:✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
test_logger.py::test_translate_log_level🌀 Click to see Generated Regression Tests
🔎 Click to see Concolic Coverage Tests
codeflash_concolic_toh405kj/tmp7fg2j1bt/test_concolic_coverage.py::test_translate_log_levelcodeflash_concolic_toh405kj/tmp7fg2j1bt/test_concolic_coverage.py::test_translate_log_level_2To edit these changes
git checkout codeflash/optimize-translate_log_level-mkoup6xqand push.