⚡️ Speed up method KIEPredictor.get_text by 42%
#6
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.
📄 42% (0.42x) speedup for
KIEPredictor.get_textindoctr/models/kie_predictor/pytorch.py⏱️ Runtime :
438 microseconds→308 microseconds(best of93runs)📝 Explanation and details
The optimization replaces an inefficient nested loop with repeated list concatenations with a single flattened list comprehension.
Key optimization:
text += [item[0] for item in value]inside a loop, which creates a new list comprehension on each iteration and then concatenates it to the existingtextlist[item[0] for value in text_pred.values() for item in value]that builds the entire result list in one passWhy this is faster:
+=is O(n) for each operation because it creates a new list and copies existing elementsPerformance characteristics from tests:
test_large_many_keys_single_item_eachshows 86% speedup)The optimization is most effective when there are many dictionary keys, as it eliminates the quadratic behavior of repeated list concatenations.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-KIEPredictor.get_text-mg7ra9vuand push.