⚡️ Speed up function maybe_filter_request_body by 126%
#67
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.
📄 126% (1.26x) speedup for
maybe_filter_request_bodyinskyvern/client/core/http_client.py⏱️ Runtime :
9.66 milliseconds→4.27 milliseconds(best of148runs)📝 Explanation and details
The optimized code achieves a 126% speedup through several key performance improvements:
Primary Optimizations:
Early primitive type fast-path: Moved
isinstance(obj, (str, int, float, type(None)))check to the very top ofjsonable_encoder(). Since primitives are the most common case (21,152/23,525 hits), this eliminates unnecessary work for ~90% of calls.Dict comprehension over manual loops:
remove_omit_from_dict(): Replaced manual dict construction with{key: value for key, value in original.items() if value is not omit}, reducing bytecode operations by ~10xjsonable_encoder(): Used dict/list comprehensions for encoding collections, leveraging CPython's optimized comprehension implementationEliminated redundant operations:
allowed_keys = set(obj.keys())allocation and lookup in dict encodingdict.get()instead oftype(obj) in custom_encoderReduced function call overhead: Restructured
maybe_filter_request_body()to minimize repeatedjsonable_encoder()calls and dictionary operations.Impact on Workloads:
Based on the
function_references, this function is called fromget_request_body()which processes HTTP request data. The optimizations are particularly beneficial for:The test results show consistent 20-70% improvements on typical API data sizes, with dramatic gains on large datasets, making this optimization highly valuable for HTTP client performance in data-intensive applications.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-maybe_filter_request_body-mi78kfm5and push.