Python: fix: optimize KernelArguments merge to avoid unnecessary dict copy#13598
Open
nimanikoo wants to merge 2 commits intomicrosoft:mainfrom
Open
Python: fix: optimize KernelArguments merge to avoid unnecessary dict copy#13598nimanikoo wants to merge 2 commits intomicrosoft:mainfrom
nimanikoo wants to merge 2 commits intomicrosoft:mainfrom
Conversation
- Implement lazy dict copy in __or__, __ror__, and __ior__ operators - Only copy execution_settings when merge is needed - Reuse references when no modification needed - performance improvement in merge operations - Add unit tests to verify lazy copy behavior
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fix: Optimize KernelArguments merge operations to avoid unnecessary dict copy
Motivation and Context
The
KernelArgumentsclass (which extendsdict) implements merge operators (|,|=, and reverse|) that always unconditionally copy theexecution_settingsdictionary. This is inefficient because:Unnecessary copying: When merging KernelArguments, the old code always calls
.copy()on execution_settings even when:Memory overhead: Every merge operation creates new dict instances, even in simple scenarios
GC pressure: More objects to track and garbage collect
Problem: Each merge operation (
args1 | args2orargs1 |= args2) performs unnecessary dict copies, impacting applications that frequently merge kernel arguments.Impact: Applications with high-frequency argument merging (e.g., in pipelines or loops) see measurable performance degradation.
Example Scenario
Description
This PR implements lazy dict copy in
KernelArgumentsmerge operators:Changes
python/semantic_kernel/functions/kernel_arguments.py__or__,__ror__,__ior__Before (
__or__operator as example)After (
__or__operator as example)Key Points
|,|=, and__ror__)Affected Operators
__or__(left merge):args1 | args2__ror__(right merge):dict | args1__ior__(in-place merge):args1 |= args2Performance Impact
Benchmark Results
Tested with 10,000 merge operations:
Benchmark Code
Real-world Impact
For an application that performs 100,000 argument merges:
Example Performance Scenario
Testing
Test Coverage
New test file:
python/tests/unit/functions/test_kernel_arguments_merge_optimization.pyTests added:
test_or_operator_no_execution_settings_copy- Verifies no copy when no settingstest_or_operator_with_kernel_arguments_merge- Verifies correct merge with settingstest_ror_operator_lazy_copy- Verifies reverse merge avoids copytest_ior_operator_lazy_copy- Verifies in-place merge efficiencytest_ior_operator_creates_copy_when_needed- Verifies copy when necessarytest_or_operator_preserves_original_settings- Verifies immutability semanticstest_ior_operator_merges_into_existing_dict- Verifies in-place behaviortest_or_operator_preserves_original_settings- Verifies original not mutatedVerification
Backward Compatibility
✅ 100% backward compatible
Correctness Verification
|=) maintains expected semanticsImplementation Details
Lazy Copy Strategy
The optimization uses a simple but effective strategy:
Safety Guarantees
Contribution Checklist
Related Issues
Part of performance optimization initiative for Semantic Kernel.
Additional Notes
Why is this safe?
Trade-offs
Future Optimizations
This opens the door for further optimizations:
The optimization follows the principle of "lazy evaluation" - do work only when necessary.