⚡️ Speed up function serialise_image by 679% in PR #1672 (bruno/parent-metadata-serialization)
#1673
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.
⚡️ This pull request contains optimizations for PR #1672
If you approve this dependent PR, these changes will be merged into the original PR branch
bruno/parent-metadata-serialization.📄 679% (6.79x) speedup for
serialise_imageininference/core/workflows/core_steps/common/serializers.py⏱️ Runtime :
7.36 milliseconds→944 microseconds(best of306runs)📝 Explanation and details
The optimization eliminates expensive Pydantic model instantiation and serialization by replacing
ParentOrigin.from_origin_coordinates_system().model_dump()calls with a simple helper function_parent_origin_as_dict()that directly constructs the required dictionary.Key changes:
ParentOriginBaseModel instances just to immediately serialize them to dictionaries viamodel_dump(). This involves validation, field processing, and internal Pydantic machinery._parent_origin_as_dict()function directly maps the four required fields (offset_x,offset_y,width,height) from the origin coordinates to a dictionary, eliminating all intermediate object creation.Why this is faster:
Performance characteristics:
The optimization shows dramatic speedups (400-700%) specifically for test cases involving crops/slices where
parent_metadata.parent_id != root_metadata.parent_id, as these trigger the expensive Pydantic operations. Non-crop cases see minimal impact since they skip this code path entirely. The profiler data confirms this - the original code spent 44.2% of its time inParentOrigin.from_origin_coordinates_system()andmodel_dump()calls, which are completely eliminated in the optimized version.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr1672-2025-11-02T01.50.52and push.