Skip to content

perf(a2a/canonical): use bytes.NewReader on Canonicalize input — drop string copy - #11

Open
perlowja wants to merge 1 commit into
bubblefish-tech:mainfrom
perlowja:perf/canonical-zerocopy-reader
Open

perlowja wants to merge 1 commit into
bubblefish-tech:mainfrom
perlowja:perf/canonical-zerocopy-reader

Conversation

@perlowja

Copy link
Copy Markdown

Summary

Canonicalize previously wrapped its []byte input as
`strings.NewReader(string(data))`. The `string([]byte)` conversion
forces a full heap copy of the input bytes before feeding the
json.Decoder. For large agent cards / task payloads on signature hot
paths, that's an allocation roughly the size of the canonical input per
call.

bytes.NewReader(data) exposes the same io.Reader interface to
json.NewDecoder while sharing the caller's existing slice — zero
extra bytes, zero extra copies. The decoded JSON tree (and therefore
the canonical output) is byte-for-byte identical because both readers
feed the same bytes into the tokenizer.

All existing tests continue to pass.

```
$ go test ./internal/a2a/ -count=1
ok github.com/bubblefish-tech/nexus/internal/a2a 0.250s
```

Signed-off-by: Jason Perlow jperlow@gmail.com

… string copy

Canonicalize previously wrapped its []byte input as

    strings.NewReader(string(data))

The `string([]byte)` conversion is a full copy of the input bytes onto the
heap. For large agent cards / task payloads on a signature hot path,
that's an allocation roughly the size of the canonical input per call.

`bytes.NewReader(data)` provides the same `io.Reader` interface to
`json.NewDecoder` while sharing the caller's existing slice — zero
extra bytes allocated, zero extra copies. The decoded JSON tree (and
therefore the canonical output) is byte-for-byte identical to before
because both readers feed the same bytes into the JSON tokenizer.

All existing TestCanonicalize* cases still pass.

Signed-off-by: Jason Perlow <jperlow@gmail.com>
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants