Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions jreader/reader_benchmark_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jreader

import (
"strings"
"testing"

"github.com/launchdarkly/go-jsonstream/v4/internal/commontest"
Expand Down Expand Up @@ -108,6 +109,39 @@ func BenchmarkReadArrayOfStrings(b *testing.B) {
}
}

// BenchmarkReadStringKinds covers the tokenizer's distinct string paths: zero-copy scanning of
// plain ASCII at several lengths, multi-byte characters, and escape sequences that require
// decoding into a new buffer.
func BenchmarkReadStringKinds(b *testing.B) {
for _, tc := range []struct {
name string
elem string
count int
}{
{"shortASCII", "value123", 50},
{"mediumASCII", strings.Repeat("m", 40), 50},
{"longASCII", strings.Repeat("L", 400), 20},
{"multiByte", "café naïve \U0001D11E", 50},
{"escaped", `x\ty` + strings.Repeat("p", 20), 50},
} {
parts := make([]string, tc.count)
for i := range parts {
parts[i] = `"` + tc.elem + `"`
}
data := []byte("[" + strings.Join(parts, ",") + "]")
b.Run(tc.name, func(b *testing.B) {
b.SetBytes(int64(len(data)))
for i := 0; i < b.N; i++ {
r := NewReader(data)
for arr := r.Array(); arr.Next(); {
r.StringAsBytes()
}
failBenchmarkOnReaderError(b, &r)
}
})
}
}

func BenchmarkReadArrayOfNullsNoAlloc(b *testing.B) {
// This just verifies that simply parsing an array doesn't cause any allocations, if the values don't.
data := []byte(`[null,null]`)
Expand Down
Loading
Loading