⚡️ Speed up function built_with_nvcodec by 16,398% - #4
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
Conversation
The optimization introduces **result caching** to avoid expensive FFI (Foreign Function Interface) calls. The key change is adding a global variable `_built_with_nvcodec` that stores the result of the first call to `_libspdl_cuda.built_with_nvcodec()`. **What was optimized:** - Added a global cache variable `_built_with_nvcodec: bool | None = None` - Modified the function to check the cache first and return immediately if available - Only calls the expensive FFI function `_libspdl_cuda.built_with_nvcodec()` once per program execution **Why this is faster:** The line profiler shows that `_libspdl_cuda.built_with_nvcodec()` takes ~5.12ms (98.8% of total time) in the original code. This is a costly FFI call that crosses the Python-C boundary. With caching, subsequent calls only perform a simple variable lookup (~14ns) instead of the expensive FFI call. **Performance characteristics:** - **First call**: Similar performance to original (still needs the FFI call) - **Subsequent calls**: ~164x faster due to cache hits - **Multiple calls scenario**: The test showing 10 consecutive calls demonstrates 19,731% speedup, highlighting the dramatic benefit when the function is called repeatedly This optimization is particularly effective for applications that check NVCODEC availability multiple times during execution, which is common in media processing workflows where capability detection happens frequently.
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.
📄 16,398% (163.98x) speedup for
built_with_nvcodecinsrc/spdl/io/utils/_build.py⏱️ Runtime :
1.64 milliseconds→9.95 microseconds(best of568runs)📝 Explanation and details
The optimization introduces result caching to avoid expensive FFI (Foreign Function Interface) calls. The key change is adding a global variable
_built_with_nvcodecthat stores the result of the first call to_libspdl_cuda.built_with_nvcodec().What was optimized:
_built_with_nvcodec: bool | None = None_libspdl_cuda.built_with_nvcodec()once per program executionWhy this is faster:
The line profiler shows that
_libspdl_cuda.built_with_nvcodec()takes ~5.12ms (98.8% of total time) in the original code. This is a costly FFI call that crosses the Python-C boundary. With caching, subsequent calls only perform a simple variable lookup (~14ns) instead of the expensive FFI call.Performance characteristics:
This optimization is particularly effective for applications that check NVCODEC availability multiple times during execution, which is common in media processing workflows where capability detection happens frequently.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_xpyvdxks/tmpk9gbptv4/test_concolic_coverage.py::test_built_with_nvcodecTo edit these changes
git checkout codeflash/optimize-built_with_nvcodec-mgqp8anpand push.