⚡️ Speed up function _cleanup_spaces by 9%
#351
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.
📄 9% (0.09x) speedup for
_cleanup_spacesinsrc/transformers/models/kosmos2/processing_kosmos2.py⏱️ Runtime :
518 microseconds→475 microseconds(best of249runs)📝 Explanation and details
The optimization replaces an imperative loop with a list comprehension, providing a 9% speedup by eliminating overhead from repeated variable assignments and list appends.
Key changes:
entity_name_leading_spaces,entity_name_trailing_spaces) and list appends. The optimized version computes these values inline within a list comprehension.len(entity_name) - len(entity_name.lstrip())andlen(entity_name) - len(entity_name.rstrip())in variables, these are calculated directly in the tuple construction.Why this is faster:
list.append()callsImpact on workloads:
Based on the function reference,
_cleanup_spacesis called fromclean_text_and_extract_entities_with_bboxes, which processes text with grounding tokens for the Kosmos2 vision-language model. This suggests it's in a text processing pipeline that could be called frequently during model inference.Test case performance:
The optimization shows the best gains (9-18% faster) on large-scale test cases with many entities (
test_large_many_entities,test_large_number_of_empty_entities), where the loop overhead reduction is most significant. For small inputs with few entities, the optimization provides modest gains or is slightly slower due to list comprehension setup costs.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_cleanup_spaces-mi9m0g7band push.