Repository navigation
Conversation
| } | ||
|
|
||
| // DeepCopy uses gob encode-decode pass to create a deep-copy of the referenced data. | ||
| // Preserve explicit empty slices so metadata arrays do not become null. |
There was a problem hiding this comment.
nit: let's drop, the comment in the body already covers it
| } | ||
|
|
||
| // gob decodes non-nil empty slices as nil; restore their shape without changing nil values. | ||
| restoreEmptySlices(reflect.ValueOf(task).Elem(), reflect.ValueOf(©).Elem()) |
There was a problem hiding this comment.
I'd probably replace gob with a reflection-based deep copy instead of copying and then walking the result again. one pass and we stop depending on gob quirks. not blocking though
| } | ||
|
|
||
| // MustPrettyPrint is a debugging utility that prints indented JSON represenation of a value. | ||
| // Print structured diagnostic values, failing explicitly if encoding fails. |
There was a problem hiding this comment.
unrelated change, let's revert
| fmt.Println(string(raw)) | ||
| } | ||
|
|
||
| // restoreEmptySlices restores empty slices lost by gob while preserving nil values, types, and deep-copy semantics. |
There was a problem hiding this comment.
nit: no need to document unexported helpers, here and on test helpers and test funcs
| encoder := reflect.TypeFor[gob.GobEncoder]() | ||
| if source.Type().Implements(encoder) || (source.Kind() != reflect.Pointer && reflect.PointerTo(source.Type()).Implements(encoder)) { |
There was a problem hiding this comment.
gob also uses encoding.BinaryMarshaler and encoding.TextMarshaler (docs), let's skip those too
There was a problem hiding this comment.
correction: gob doesn't use TextMarshaler (see the NOTE in encoding/gob/type.go and golang.org/issue/6760), current handling is correct
| ) | ||
|
|
||
| // TestDeepCopyPreservesEmptyCollections verifies empty arrays, explicit null, nested metadata, and write isolation. | ||
| func TestDeepCopyPreservesEmptyCollections(t *testing.T) { |
There was a problem hiding this comment.
let's please move these to utils_test.go and make them table-driven. also let's add a case with a2a.Task that has []any{} in metadata and in a DataPart, that's the behavior we care about
| source := map[string]any{"empty": []any{}, "null": nil, "nested": map[string]any{"items": []any{}, "body": []any{"正文"}}, "metadata": []any{map[string]any{"empty": []any{}}}} | ||
| copied, err := DeepCopy(&source) | ||
| if err != nil { | ||
| t.Fatal(err) |
There was a problem hiding this comment.
t.Fatalf("DeepCopy() error = %v, want nil", err) here and in other places
| Nil []byte | ||
| Nested [1][]int | ||
| Pointer *[]string | ||
| Integer uint64 |
There was a problem hiding this comment.
not related to the fix, let's drop
|
I think it's probably time to hand roll clones like in #462 instead of adding a reflection pass on top of gob, but not in the current release. |
| for i := 0; i < min(source.Len(), target.Len()); i++ { | ||
| restoreEmptySlices(source.Index(i), target.Index(i)) | ||
| } |
There was a problem hiding this comment.
this walks every element, for a2a.Raw that's a reflection call per byte. DeepCopy of a task with a 4MB raw part goes from ~8ms to ~365ms, and we copy on every task store read/write and every event. let's skip the loop when the element type can't contain slices (at least []byte) and add a case with a large Raw part
DeepCopy's gob round trip converts explicitly empty slices to nil. Task metadata and DataPart values consequently change JSON
[]tonull, losing a distinction callers rely on.Based on official v2.9.0 (
c746bfd5e4db3806d423d0cea15dbf0919f9bbfa), this change restores originally non-nil empty slices after successful gob decoding while preserving nil values, non-JSON types, copy isolation, and existing encoding errors. Custom GobEncoder and BinaryMarshaler representations remain authoritative. TextMarshaler-only types continue using gob's structural encoding.The restoration pass skips per-element traversal of byte slices after handling their empty/nil shape. This addresses the large Raw-part performance regression raised in review without changing serialization. A 4 MiB Raw Task benchmark on the same host measured approximately 138–140 ms/op before this adjustment and 1.14–1.21 ms/op afterward (3 iterations, two runs per variant); this is a focused comparison, not a general latency guarantee.
Tests remain table-driven in
internal/utils/utils_test.goand cover actual Task/DataPart values, nested containers, explicit null, empty bytes and Raw values, custom codecs, and isolation of a 4 MiB Raw part. Raw comparisons preserve the nil/empty distinction.Validation of the tree in
a370a56522a1c397686266891d49f2d89f62a19e:go test -race -mod=readonly -count=1 -shuffle=on ./...: 38 test-bearing packages pass; 17 other packages have no tests.go vet -mod=readonly ./...passes.The nested
examples/clustermodemodule is outside the root-module./...run. Cancellation is already fixed upstream; this PR contains no cancellation changes.