⚡️ Speed up function _shared_object_saving_scope by 105%
#207
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.
📄 105% (1.05x) speedup for
_shared_object_saving_scopeinkeras/src/legacy/saving/serialization.py⏱️ Runtime :
249 microseconds→122 microseconds(best of174runs)📝 Explanation and details
The optimization replaces
getattr(SHARED_OBJECT_SAVING, "scope", None)with direct dictionary access usingSHARED_OBJECT_SAVING.__dict__.get("scope", None). This achieves a 104% speedup by bypassing Python's attribute lookup mechanism.Key Performance Improvement:
getattr()triggers Python's descriptor protocol and attribute resolution machinery, which involves multiple method calls and checks__dict__.get()access skips this overhead and performs a simple dictionary lookupNone), as shown in the test results where missing attributes see 53-93% speedupWhy This Works:
threading.local()objects store their thread-specific data in a standard__dict__, making direct dictionary access safe and equivalent. Both approaches handle missing attributes identically by returningNone.Impact on Workloads:
Based on the function references,
_shared_object_saving_scope()is called in hot paths during Keras model serialization:__enter__methods that are invoked during model savingserialize_keras_class_and_config()which processes each layer/component during serializationTest Case Performance:
The optimization shows consistent 53-121% improvements across all test scenarios, with particularly strong gains for missing attributes and large-scale operations (111% speedup for 500 iterations), making it effective for both individual calls and batch serialization workloads.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_shared_object_saving_scope-mjadanrpand push.