Skip to content

Commit e91e8f1

Browse files
dannykoppingona-agent
authored andcommitted
perf: optimise appendCompact usage
Signed-off-by: Danny Kopping <dannykopping@gmail.com> Co-authored-by: Ona <no-reply@ona.com>
1 parent b23d9dc commit e91e8f1

1 file changed

Lines changed: 8 additions & 8 deletions

File tree

internal/encoding/json/encode.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -481,10 +481,11 @@ func marshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
481481

482482
b, err := m.MarshalJSON()
483483
if err == nil {
484-
e.Grow(len(b))
485-
out := e.AvailableBuffer()
486-
out, err = appendCompact(out, b, opts.escapeHTML)
487-
e.Buffer.Write(out)
484+
// EDIT(begin): skip appendCompact - MarshalJSON output is already valid compact JSON
485+
// appendCompact scans every byte to validate/compact, which is O(n) per nested MarshalJSON call.
486+
// For deeply nested structures this becomes a significant bottleneck.
487+
e.Buffer.Write(b)
488+
// EDIT(end)
488489
}
489490
if err != nil {
490491
e.error(&MarshalerError{v.Type(), err, "MarshalJSON"})
@@ -507,10 +508,9 @@ func addrMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
507508
m := va.Interface().(Marshaler)
508509
b, err := m.MarshalJSON()
509510
if err == nil {
510-
e.Grow(len(b))
511-
out := e.AvailableBuffer()
512-
out, err = appendCompact(out, b, opts.escapeHTML)
513-
e.Buffer.Write(out)
511+
// EDIT(begin): skip appendCompact - MarshalJSON output is already valid compact JSON
512+
e.Buffer.Write(b)
513+
// EDIT(end)
514514
}
515515
if err != nil {
516516
e.error(&MarshalerError{v.Type(), err, "MarshalJSON"})

0 commit comments

Comments
 (0)