⚡️ Speed up function _consume_ent by 57%
#4
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.
📄 57% (0.57x) speedup for
_consume_entinspacy/training/iob_utils.py⏱️ Runtime :
1.42 milliseconds→905 microseconds(best of245runs)📝 Explanation and details
The optimization achieves a 56% speedup by eliminating inefficient list operations and replacing them with more performant alternatives:
Key Optimizations
1. Index-based scanning instead of repeated pop(0) operations
while tags and tags[0] in {target_in, target_last}: tags.pop(0)which performs O(n) operations for each popfor i in range(n): t = tags[i]to scan without modification, then removes matched elements in one operation withdel tags[:length-1]2. List multiplication instead of list comprehension
middle = [f"I-{label}" for _ in range(1, length - 1)]creates strings in a loopmiddle = ["I-" + label] * (length - 2)uses faster list multiplication for repeated identical strings3. Early label validation
if not label:check earlier to avoid unnecessary work when tags are invalid4. Conditional logic optimization
length > 2case to avoid unnecessary list operations for simple B-L pairsPerformance Impact by Workload
The optimization shows dramatic improvements for large entities (200-207% faster for 999-token entities) because the original O(n²) pop(0) operations become O(n) index scanning. Small entities see mixed results - some are 20-40% slower due to additional overhead, while multi-token entities are 16-40% faster.
The function appears well-suited for NLP entity processing pipelines where large named entities are common, making the substantial gains on large sequences very valuable despite minor overhead on single tokens.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_consume_ent-mhljaefjand push.