Python: fix: optimize function_copy to avoid unnecessary deepcopy#13599
Open
nimanikoo wants to merge 2 commits intomicrosoft:mainfrom
Open
Python: fix: optimize function_copy to avoid unnecessary deepcopy#13599nimanikoo wants to merge 2 commits intomicrosoft:mainfrom
nimanikoo wants to merge 2 commits intomicrosoft:mainfrom
Conversation
- Implement lazy deepcopy in function_copy() method - Only copy metadata when plugin_name actually changes - Reuse metadata reference when no modification needed - performance improvement in function_copy 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 function_copy() to avoid unnecessary deepcopy
Motivation and Context
The
KernelFunction.function_copy()method currently performs an unconditionaldeepcopy()on metadata regardless of whether the plugin_name changes. This is inefficient because:function_copy()is called without a plugin_name argument, meaning metadata is copied unnecessarilyProblem: Each function copy incurs the cost of deepcopying the entire metadata structure even when it won't be modified.
Impact: Applications that frequently copy kernel functions see significant performance degradation.
Example Scenario
Description
This PR implements lazy deepcopy in
KernelFunction.function_copy():Changes
python/semantic_kernel/functions/kernel_function.pyfunction_copy(self, plugin_name: str | None = None)Before
After
Key Points
model_copy()instead ofdeepcopy()for safer copyingPerformance Impact
Benchmark Results
Tested with 1000 function copies:
Benchmark Code
Real-world Impact
For an application that creates 10,000 function copies:
Testing
Test Coverage
New test file:
python/tests/unit/functions/test_function_copy_optimization.pyTests added:
test_function_copy_same_plugin_no_deepcopy- Verifies reference reuse when no changetest_function_copy_different_plugin_creates_copy- Verifies copy when plugin_name changestest_function_copy_preserves_function_behavior- Verifies functional correctnesstest_function_copy_no_unnecessary_deepcopy- Mocks deepcopy to ensure it's not calledtest_function_copy_multiple_calls_same_plugin- Performance test with multiple copiesVerification
Backward Compatibility
✅ 100% backward compatible
Contribution Checklist
Related Issues
Part of performance optimization initiative for Semantic Kernel.
Additional Notes
This optimization is safe because:
The optimization follows the principle of "pay for what you use" - only incur copying cost when modification is needed.