perf(encode): emit in place, pool the buffer and cache interface flags
Test / test (push) Successful in 1m29s

This commit is contained in:
2026-09-20 22:15:38 +02:00
parent 3a4bd74bf0
commit 582222738b
4 changed files with 337 additions and 118 deletions
+7 -1
View File
@@ -24,6 +24,7 @@ import (
"context"
"errors"
"fmt"
"slices"
)
// A SyntaxError describes a malformed TOML document, including the 1-based
@@ -423,7 +424,12 @@ func (e *Encoder) MarshalContext(ctx context.Context, v any) ([]byte, error) {
enc.ctx = ctx
enc.opts = *e
if err := enc.encode(v); err != nil {
enc.release()
return nil, err
}
return enc.bytes(), nil
// The output leaves the pooled buffer as a copy, so the next Marshal
// reuses the buffer without touching what the caller holds.
out := slices.Clone(enc.buf.Bytes())
enc.release()
return out, nil
}