⚡️ Speed up method PipelineBuilder.pipe by 180% - #16
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
Conversation
The optimization replaces an expensive `isinstance(op, SupportsGetItem)` check with a more efficient duck-typing approach using `getattr(op, "__getitem__", None)`. **Key Changes:** - Replaced `isinstance(op, SupportsGetItem)` with `getitem = getattr(op, "__getitem__", None)` followed by `if getitem is not None and callable(getitem)` - This avoids the overhead of Python's Abstract Base Class (ABC) machinery used by typing protocols **Why It's Faster:** The `isinstance` check with typing protocols like `SupportsGetItem` involves complex metaclass machinery and registration checks that are computationally expensive. The `getattr` approach directly queries the object's attribute dictionary, which is a simple hash table lookup - orders of magnitude faster. **Performance Impact:** Line profiler shows the original `isinstance` check took 94,999ns (72.1% of total time), while the optimized `getattr` + `callable` checks took only 1,786ns combined (4.2% of total time). This represents a ~53x improvement for this specific check, leading to the overall 179% speedup. **Test Case Performance:** This optimization is particularly effective for workloads that frequently create `Pipe` configurations, especially when the `op` parameter is typically a function/callable rather than a container with `__getitem__`. The duck-typing approach performs well regardless of the input type while being consistently faster than protocol checking.
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.
📄 180% (1.80x) speedup for
PipelineBuilder.pipeinsrc/spdl/pipeline/_builder.py⏱️ Runtime :
47.7 microseconds→17.0 microseconds(best of44runs)📝 Explanation and details
The optimization replaces an expensive
isinstance(op, SupportsGetItem)check with a more efficient duck-typing approach usinggetattr(op, "__getitem__", None).Key Changes:
isinstance(op, SupportsGetItem)withgetitem = getattr(op, "__getitem__", None)followed byif getitem is not None and callable(getitem)Why It's Faster:
The
isinstancecheck with typing protocols likeSupportsGetIteminvolves complex metaclass machinery and registration checks that are computationally expensive. Thegetattrapproach directly queries the object's attribute dictionary, which is a simple hash table lookup - orders of magnitude faster.Performance Impact:
Line profiler shows the original
isinstancecheck took 94,999ns (72.1% of total time), while the optimizedgetattr+callablechecks took only 1,786ns combined (4.2% of total time). This represents a ~53x improvement for this specific check, leading to the overall 179% speedup.Test Case Performance:
This optimization is particularly effective for workloads that frequently create
Pipeconfigurations, especially when theopparameter is typically a function/callable rather than a container with__getitem__. The duck-typing approach performs well regardless of the input type while being consistently faster than protocol checking.✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
spdl_unittest/dataloader/pipeline_test.py::test_pipeline_stage_hook_wrong_def1spdl_unittest/dataloader/pipeline_test.py::test_pipeline_stage_hook_wrong_def2To edit these changes
git checkout codeflash/optimize-PipelineBuilder.pipe-mgrkzv0eand push.