⚡️ Speed up function _char_indices by 10%#6
Open
codeflash-ai[bot] wants to merge 1 commit intomasterfrom
Open
Conversation
The optimization eliminates redundant indexing operations by caching the last token of the span. In the original code, `span[-1]` is accessed twice - once to get `idx` and again for `len()`. The optimized version stores `span[-1]` in a local variable `last` and reuses it, reducing the number of span indexing operations from 3 to 2. **Key changes:** - Introduced `last = span[-1]` to cache the final token - Combined variable assignment into the return statement - Eliminated the intermediate `start` and `end` variables **Why this speeds up the code:** In Python, sequence indexing (especially negative indexing like `span[-1]`) involves method calls and bounds checking. By caching the result, we avoid repeating this overhead. The line profiler shows the most expensive operation was `span[-1].idx + len(span[-1])` (54.2% of total time), which required two indexing operations. The optimization reduces this to a single indexing operation plus reuse of the cached token. **Performance impact:** The 10% overall speedup is consistent across test cases, with improvements ranging from 0% (unicode edge case) to 23% (large unicode tokens). The optimization is particularly effective for spans with larger tokens or more complex token objects where the indexing overhead is more significant. Given that this function calculates character boundaries for spans in NLP pipelines, even small improvements can compound when processing large documents or datasets.
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.
📄 10% (0.10x) speedup for
_char_indicesinspacy/pipeline/span_finder.py⏱️ Runtime :
47.2 microseconds→42.8 microseconds(best of250runs)📝 Explanation and details
The optimization eliminates redundant indexing operations by caching the last token of the span. In the original code,
span[-1]is accessed twice - once to getidxand again forlen(). The optimized version storesspan[-1]in a local variablelastand reuses it, reducing the number of span indexing operations from 3 to 2.Key changes:
last = span[-1]to cache the final tokenstartandendvariablesWhy this speeds up the code:
In Python, sequence indexing (especially negative indexing like
span[-1]) involves method calls and bounds checking. By caching the result, we avoid repeating this overhead. The line profiler shows the most expensive operation wasspan[-1].idx + len(span[-1])(54.2% of total time), which required two indexing operations. The optimization reduces this to a single indexing operation plus reuse of the cached token.Performance impact:
The 10% overall speedup is consistent across test cases, with improvements ranging from 0% (unicode edge case) to 23% (large unicode tokens). The optimization is particularly effective for spans with larger tokens or more complex token objects where the indexing overhead is more significant. Given that this function calculates character boundaries for spans in NLP pipelines, even small improvements can compound when processing large documents or datasets.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_char_indices-mhmic9uoand push.