⚡️ Speed up function _fix_metadata_field_precision by 304%
#264
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.
📄 304% (3.04x) speedup for
_fix_metadata_field_precisioninunstructured/staging/base.py⏱️ Runtime :
164 milliseconds→40.6 milliseconds(best of57runs)📝 Explanation and details
The optimized code achieves a 304% speedup by replacing expensive
deepcopy()operations with selective shallow copying. Here's what changed and why it matters:Key Optimization: Selective Shallow Copying
Original approach: Called
deepcopy(element)on every element, which recursively copies the entire object graph—accounting for 94.8% of the function's runtime.Optimized approach: Uses
copy(element)for shallow copying, then selectively copies only the metadata objects that need modification:copy()ofelement.metadataandelement.metadata.coordinatesonly when coordinates existtuple()instead of building a list for rounded points, which is more memory-efficientelement.metadatawhendetection_class_probneeds rounding AND metadata hasn't already been copied (viaif el.metadata is element.metadatacheck)Why This Works
The optimization exploits the fact that most element attributes don't need modification—only specific metadata fields require precision adjustment. By sharing unmodified attributes between the original and new elements, we avoid the overhead of deep copying nested objects like coordinate systems, detection origins, and other metadata fields.
Performance breakdown:
copy()is ~32x faster thandeepcopy()for these element objectsis) prevents redundant metadata copying when both coordinate and probability rounding are neededImpact on Workloads
Based on
function_references, this function is called in serialization hot paths:elements_to_base64_gzipped_json()- Used for compressing elements in HTTP responseselements_to_json()- Direct JSON serializationelements_to_ndjson()- Newline-delimited JSON serializationAll three functions call
_fix_metadata_field_precision()before serialization, making this optimization critical for document processing pipelines that serialize large volumes of elements.Test Results
The optimization excels across all test scenarios:
The speedup is consistent regardless of coordinate system type (PixelSpace vs. CoordinateSystem) or whether detection probabilities are present, demonstrating robust performance gains across diverse workloads.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-_fix_metadata_field_precision-mkrzwqchand push.