Repository navigation
perf: replace gob-based DeepCopy with manual copy functions - #462
Closed
evgeniybutenko wants to merge 1 commit into
Closed
evgeniybutenko wants to merge 1 commit into
evgeniybutenko wants to merge 1 commit into
Conversation
Replace gob encode-decode DeepCopy with type-specific manual copy functions (CopyTask, CopyArtifact, CopyMessage, CopyPart, CopyPushConfig) in all production code paths. DeepCopy via gob was called on every streamed event in the task update manager and event applier, serializing the entire accumulated task on each call. This resulted in O(n^2) total work for a streaming response with N deltas. Benchmark results (Apple M5 Pro): Task Size gob DeepCopy Manual Copy Speedup 1 art, 10 parts 26,339 ns 442 ns 59x 1 art, 100 parts 60,388 ns 2,228 ns 27x 1 art, 1000 parts 367,335 ns 20,870 ns 18x 10 arts, 100 parts 387,674 ns 22,419 ns 17x All existing tests pass including e2e. The generic gob-based utils.DeepCopy is retained for test code that uses it for input immutability verification.
Member
|
Initially |
Member
|
closing inactive |
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.
Problem
utils.DeepCopyusesgobencode-decode to copya2a.Taskanda2a.Artifactobjects. It is called on every streamed event intaskupdate.Manager.Process→a2aevent.ApplyUpdate→deepCopyResult, and again inManager.createandNewManagerfor initial task copies.For a streaming response with N text deltas,
deepCopyResultis called N times. Each call serializes the entire accumulated task (including all prior artifact parts and history messages) via gob, resulting in O(n²) total work. Gob allocates a newbytes.Buffer+ encoder + decoder on every call, walks the entire type tree via reflection, and serializes all accumulated content.Solution
Replace the generic gob-based
DeepCopywith type-specific manual copy functions in a newa2a/copy.go:CopyTask— copiesTaskincludingArtifacts,History,Metadata,Status(withMessageandTimestamp)CopyArtifact— copiesArtifactincludingParts,Extensions,MetadataCopyMessage— copiesMessageincludingParts,Extensions,Metadata,ReferenceTasksCopyPart— copiesPartincludingContent(deep-copiesRawbytes,Data.Valuevia JSON fallback for unknown types) andMetadataCopyPushConfig— copiesPushConfigincludingAuthThese functions use
slices.Clone,maps.Clone, and manual element-wise copying instead of serialization. ForData.Valuecontaining arbitraryanytypes, a JSON roundtrip fallback is used (matching the previous behavior of gob).Changes
a2a/copy.go(new)a2aevent/event.godeepCopyResultusesa2a.CopyTaskinternal/taskupdate/manager.goNewManagerandcreateusea2a.CopyTaska2asrv/taskstore/inmemory.goDeepCopycalls replaced witha2a.CopyTask;toListTasksResultsignature simplifieda2asrv/push/store.goDeepCopycalls replaced witha2a.CopyPushConfigThe generic
utils.DeepCopy(gob-based) is retained — test code uses it for input immutability verification.What This Does NOT Change
ApplyUpdate/ApplyArtifactUpdate/ApplyStatusUpdate— all still return fully independent copiesInMemorystore isolation guarantees — still deep-copies onCreate,Update,Get, andListManagerbehavior — still copies on initialization and task creationBenchmark Results
Benchmark comparing
utils.DeepCopy(gob) vsa2a.CopyTask(manual) on tasks with varying artifact sizes:The speedup scales with task complexity because gob re-serializes the entire task on every call, while the manual copy only walks each field once.
Test Results
All existing tests pass, including e2e: