Skip to content

perf: replace gob-based DeepCopy with manual copy functions - #462

Closed
evgeniybutenko wants to merge 1 commit into
a2aproject:mainfrom
evgeniybutenko:perf/replace-gob-deepcopy-with-manual-copy
Closed

evgeniybutenko wants to merge 1 commit into
a2aproject:mainfrom
evgeniybutenko:perf/replace-gob-deepcopy-with-manual-copy

Conversation

@evgeniybutenko

Copy link
Copy Markdown

Problem

utils.DeepCopy uses gob encode-decode to copy a2a.Task and a2a.Artifact objects. It is called on every streamed event in taskupdate.Manager.Process → a2aevent.ApplyUpdate → deepCopyResult, and again in Manager.create and NewManager for initial task copies.

For a streaming response with N text deltas, deepCopyResult is 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 new bytes.Buffer + encoder + decoder on every call, walks the entire type tree via reflection, and serializes all accumulated content.

Solution

Replace the generic gob-based DeepCopy with type-specific manual copy functions in a new a2a/copy.go:

  • CopyTask — copies Task including Artifacts, History, Metadata, Status (with Message and Timestamp)
  • CopyArtifact — copies Artifact including Parts, Extensions, Metadata
  • CopyMessage — copies Message including Parts, Extensions, Metadata, ReferenceTasks
  • CopyPart — copies Part including Content (deep-copies Raw bytes, Data.Value via JSON fallback for unknown types) and Metadata
  • CopyPushConfig — copies PushConfig including Auth

These functions use slices.Clone, maps.Clone, and manual element-wise copying instead of serialization. For Data.Value containing arbitrary any types, a JSON roundtrip fallback is used (matching the previous behavior of gob).

Changes

File Change
a2a/copy.go (new) Type-specific copy functions
a2aevent/event.go deepCopyResult uses a2a.CopyTask
internal/taskupdate/manager.go NewManager and create use a2a.CopyTask
a2asrv/taskstore/inmemory.go All 4 DeepCopy calls replaced with a2a.CopyTask; toListTasksResult signature simplified
a2asrv/push/store.go All 4 DeepCopy calls replaced with a2a.CopyPushConfig

The generic utils.DeepCopy (gob-based) is retained — test code uses it for input immutability verification.

What This Does NOT Change

  • The "input task is never modified" contract on ApplyUpdate / ApplyArtifactUpdate / ApplyStatusUpdate — all still return fully independent copies
  • The InMemory store isolation guarantees — still deep-copies on Create, Update, Get, and List
  • The Manager behavior — still copies on initialization and task creation

Benchmark Results

Benchmark comparing utils.DeepCopy (gob) vs a2a.CopyTask (manual) on tasks with varying artifact sizes:

goos: darwin
goarch: arm64
cpu: Apple M5 Pro

BenchmarkDeepCopyTask_1Art_10Parts-18      42861     26339 ns/op    32198 B/op    554 allocs/op
BenchmarkManualCopyTask_1Art_10Parts-18   2628496       442 ns/op     1832 B/op     30 allocs/op

BenchmarkDeepCopyTask_1Art_100Parts-18     20715     60388 ns/op    95124 B/op   1010 allocs/op
BenchmarkManualCopyTask_1Art_100Parts-18  528456      2228 ns/op     9848 B/op    210 allocs/op

BenchmarkDeepCopyTask_1Art_1000Parts-18      3162    367335 ns/op  757138 B/op   5519 allocs/op
BenchmarkManualCopyTask_1Art_1000Parts-18  58464     20870 ns/op   89145 B/op   2010 allocs/op

BenchmarkDeepCopyTask_10Art_100Parts-18      3049    387674 ns/op  765142 B/op   5681 allocs/op
BenchmarkManualCopyTask_10Art_100Parts-18  54418     22419 ns/op   94162 B/op   2055 allocs/op
Task size gob DeepCopy Manual copy Speedup Allocs reduction
1 artifact, 10 parts 26,339 ns 442 ns 59x 554 → 30 (18x)
1 artifact, 100 parts 60,388 ns 2,228 ns 27x 1010 → 210 (5x)
1 artifact, 1000 parts 367,335 ns 20,870 ns 18x 5519 → 2010 (3x)
10 artifacts, 100 parts 387,674 ns 22,419 ns 17x 5681 → 2055 (3x)

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:

ok  github.com/a2aproject/a2a-go/v2/a2a                 0.300s
ok  github.com/a2aproject/a2a-go/v2/a2aevent             1.391s
ok  github.com/a2aproject/a2a-go/v2/a2asrv              (cached)
ok  github.com/a2aproject/a2a-go/v2/a2asrv/taskstore     (cached)
ok  github.com/a2aproject/a2a-go/v2/a2asrv/push          (cached)
ok  github.com/a2aproject/a2a-go/v2/internal/taskupdate (cached)
ok  github.com/a2aproject/a2a-go/v2/internal/taskexec    (cached)
ok  github.com/a2aproject/a2a-go/v2/e2e                  3.852s
ok  github.com/a2aproject/a2a-go/v2/e2e/compat           4.482s

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.
@yarolegovich

Copy link
Copy Markdown
Member

Initially gob was added as a pre 1.0 solution when type stability was not expected. At this point it makes sense to hand-roll converters, but I don't think these should be core package-level functions. I'd rather go with Clone() methods on types.

@yarolegovich

Copy link
Copy Markdown
Member

closing inactive

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants