⚡️ Speed up method Seq._is_seq_like by 20%
#136
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.
📄 20% (0.20x) speedup for
Seq._is_seq_likeinsrc/bokeh/core/property/container.py⏱️ Runtime :
1.60 milliseconds→1.34 milliseconds(best of204runs)📝 Explanation and details
The optimization reorganizes the
_is_seq_likemethod's condition checks to follow a more efficient early-exit pattern, achieving a 19% speedup.Key Changes:
Early Mapping check: The original code performed expensive ABC checks first (
isinstance(value, (Container, Sized, Iterable))) then checked Mapping at the end. The optimized version checksisinstance(value, Mapping)first and returnsFalseimmediately.Hasattr check moved up: The
hasattr(value, "__getitem__")check is moved before the ABC checks since it's much faster thanisinstancecalls against multiple abstract base classes.Final ABC check: Only objects that pass both filters get the expensive ABC check.
Why This Is Faster:
__getitem__(sets, basic objects) exit early without expensive ABC validation. Sets show 15-16% improvement, and basic types likeint/Noneshow 60-250% speedups.Performance Impact by Type:
__getitem__: Moderate improvements (15-250%)The optimization maintains identical behavior while restructuring checks from most-to-least expensive, allowing faster rejection of invalid types.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-Seq._is_seq_like-mhwhjd3zand push.