⚡️ Speed up function object_detection_classes by 28%
#67
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.
📄 28% (0.28x) speedup for
object_detection_classesinunstructured/partition/pdf_image/analysis/layout_dump.py⏱️ Runtime :
19.8 microseconds→15.5 microseconds(best of86runs)📝 Explanation and details
The optimization applies static pre-computation by moving the expensive
list(LABEL_MAP.values())operations outside the function and storing the results in module-level constants_YOLOX_CLASSESand_DETECTRON_CLASSES.Key changes:
list(YOLOX_LABEL_MAP.values())andlist(DETECTRON_LABEL_MAP.values())with direct constant referencesWhy this is faster:
The original code calls
list(dict.values())every time the function executes, which involves iterating through dictionary values and creating a new list. With static pre-computation, this work happens only once at module import time, and subsequent calls simply return the pre-built lists.Performance impact based on usage:
Looking at the function reference,
object_detection_classesis called from adump()method in layout analysis, suggesting it's likely called multiple times during PDF processing workflows. The 27% speedup (19.8μs → 15.5μs) becomes significant when processing many documents or layout elements.Test case optimization patterns:
This optimization is particularly effective for workloads that repeatedly query model classes during document processing pipelines.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-object_detection_classes-mje75g8xand push.