⚡️ Speed up function with_content_type by 432%
#18
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.
📄 432% (4.32x) speedup for
with_content_typeinsrc/deepgram/core/file.py⏱️ Runtime :
2.85 milliseconds→536 microseconds(best of111runs)📝 Explanation and details
The optimization achieves a 432% speedup by eliminating two major performance bottlenecks:
1. Removed expensive
cast()calls (74% of original runtime)The original code used
cast()for type hints on tuple unpacking, which added significant overhead:cast(Tuple[Optional[str], FileContent], file)took 2.98ms (26.7% of total time)cast(...Mapping[str, str]], file)took 5.27ms (47.3% of total time)The optimized version directly unpacks tuples (
filename, content = file), eliminating these costly function calls entirely.2. Cached
len(file)calculationInstead of calling
len(file)multiple times in the conditional chain, the optimization calculates it once and stores it infile_len. This reduces redundant length calculations.Performance impact by test case:
cast()overheadThe optimization is particularly effective for applications processing many file objects with metadata (filename, content-type, headers), which is the primary use case for this utility function.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
unit/test_core_file.py::TestFileTyping.test_various_file_content_typesunit/test_core_file.py::TestWithContentType.test_four_element_tuple_with_headersunit/test_core_file.py::TestWithContentType.test_four_element_tuple_with_none_content_typeunit/test_core_file.py::TestWithContentType.test_invalid_tuple_lengthunit/test_core_file.py::TestWithContentType.test_io_file_contentunit/test_core_file.py::TestWithContentType.test_simple_file_contentunit/test_core_file.py::TestWithContentType.test_single_element_tupleunit/test_core_file.py::TestWithContentType.test_string_file_contentunit/test_core_file.py::TestWithContentType.test_three_element_tuple_with_content_typeunit/test_core_file.py::TestWithContentType.test_three_element_tuple_with_none_content_typeunit/test_core_file.py::TestWithContentType.test_two_element_tuple🌀 Generated Regression Tests and Runtime
⏪ Replay Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_7zeygj7s/tmpxrd1qf7d/test_concolic_coverage.py::test_with_content_typecodeflash_concolic_7zeygj7s/tmpxrd1qf7d/test_concolic_coverage.py::test_with_content_type_2codeflash_concolic_7zeygj7s/tmpxrd1qf7d/test_concolic_coverage.py::test_with_content_type_3codeflash_concolic_7zeygj7s/tmpxrd1qf7d/test_concolic_coverage.py::test_with_content_type_4To edit these changes
git checkout codeflash/optimize-with_content_type-mh4hq3moand push.