diff options
| author | Joe Tsai <joetsai@digital-static.net> | 2025-06-24 18:56:26 -0700 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2025-07-25 10:47:45 -0700 |
| commit | ebdbfccd989b07a8aef75af5fbe7448f035ee239 (patch) | |
| tree | 6072864893f39149ffcc48c85bc5780175da581b /src/encoding/json/jsontext/decode_test.go | |
| parent | 91c4f0ccd542a505f72ad0db952f55688851e49e (diff) | |
| download | go-ebdbfccd989b07a8aef75af5fbe7448f035ee239.tar.xz | |
encoding/json/jsontext: preserve buffer capacity in Encoder.Reset
This does the equivalent of CL 681177 for the Encoder.
It preserves the internal buffer between resets.
Change-Id: I5e9353b6d7755e067d4f9a4d1ea3d8f056253027
Reviewed-on: https://go-review.googlesource.com/c/go/+/690375
Reviewed-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
Auto-Submit: Joseph Tsai <joetsai@digital-static.net>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Diffstat (limited to 'src/encoding/json/jsontext/decode_test.go')
| -rw-r--r-- | src/encoding/json/jsontext/decode_test.go | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/src/encoding/json/jsontext/decode_test.go b/src/encoding/json/jsontext/decode_test.go index 78296bb590..209ff65ec8 100644 --- a/src/encoding/json/jsontext/decode_test.go +++ b/src/encoding/json/jsontext/decode_test.go @@ -1269,14 +1269,14 @@ func TestPeekableDecoder(t *testing.T) { // TestDecoderReset tests that the decoder preserves its internal // buffer between Reset calls to avoid frequent allocations when reusing the decoder. // It ensures that the buffer capacity is maintained while avoiding aliasing -// issues with bytes.Buffer. +// issues with [bytes.Buffer]. func TestDecoderReset(t *testing.T) { - // Create a decoder with a reasonably large JSON input to ensure buffer growth + // Create a decoder with a reasonably large JSON input to ensure buffer growth. largeJSON := `{"key1":"value1","key2":"value2","key3":"value3","key4":"value4","key5":"value5"}` dec := NewDecoder(strings.NewReader(largeJSON)) t.Run("Test capacity preservation", func(t *testing.T) { - // Read the first JSON value to grow the internal buffer + // Read the first JSON value to grow the internal buffer. val1, err := dec.ReadValue() if err != nil { t.Fatalf("first ReadValue failed: %v", err) @@ -1285,22 +1285,22 @@ func TestDecoderReset(t *testing.T) { t.Fatalf("first ReadValue = %q, want %q", val1, largeJSON) } - // Get the buffer capacity after first use + // Get the buffer capacity after first use. initialCapacity := cap(dec.s.buf) if initialCapacity == 0 { t.Fatalf("expected non-zero buffer capacity after first use") } - // Reset with a new reader - this should preserve the buffer capacity + // Reset with a new reader - this should preserve the buffer capacity. dec.Reset(strings.NewReader(largeJSON)) - // Verify the buffer capacity is preserved (or at least not smaller) + // Verify the buffer capacity is preserved (or at least not smaller). preservedCapacity := cap(dec.s.buf) if preservedCapacity < initialCapacity { t.Fatalf("buffer capacity reduced after Reset: got %d, want at least %d", preservedCapacity, initialCapacity) } - // Read the second JSON value to ensure the decoder still works correctly + // Read the second JSON value to ensure the decoder still works correctly. val2, err := dec.ReadValue() if err != nil { t.Fatalf("second ReadValue failed: %v", err) @@ -1317,7 +1317,7 @@ func TestDecoderReset(t *testing.T) { dec.Reset(bb) bbBuf = bb.Bytes() - // Read the third JSON value to ensure functionality with bytes.Buffer + // Read the third JSON value to ensure functionality with bytes.Buffer. val3, err := dec.ReadValue() if err != nil { t.Fatalf("fourth ReadValue failed: %v", err) |
