-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose AppendJSONString() function for fast construction of JSON-enco…
…ded strings While at it, optimize AppendJSONString() for cases when the string contains chars, which need to be escaped Benchmark results: BenchmarkAppendJSONString/no-special-chars-16 134810218 8.647 ns/op 6129.48 MB/s 0 B/op 0 allocs/op BenchmarkAppendJSONString/with-special-chars-16 72410246 17.31 ns/op 3061.36 MB/s 0 B/op 0 allocs/op BenchmarkAppendJSONStringViaStrconv/no-special-chars-16 14370494 78.87 ns/op 672.03 MB/s 0 B/op 0 allocs/op BenchmarkAppendJSONStringViaStrconv/with-special-chars-16 14202278 81.71 ns/op 648.65 MB/s 0 B/op 0 allocs/op
- Loading branch information
Showing
4 changed files
with
109 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package quicktemplate | ||
|
||
import ( | ||
"strconv" | ||
"testing" | ||
) | ||
|
||
func BenchmarkAppendJSONString(b *testing.B) { | ||
b.Run("no-special-chars", func(b *testing.B) { | ||
benchmarkAppendJSONString(b, "foo bar baz abc defkjlkj lkjdfs klsdjflfdjoqjo lkj ss") | ||
}) | ||
b.Run("with-special-chars", func(b *testing.B) { | ||
benchmarkAppendJSONString(b, `foo bar baz abc defkjlkj lkjdf" klsdjflfdjoqjo\lkj ss`) | ||
}) | ||
} | ||
|
||
func benchmarkAppendJSONString(b *testing.B, s string) { | ||
b.ReportAllocs() | ||
b.SetBytes(int64(len(s))) | ||
b.RunParallel(func(pb *testing.PB) { | ||
var buf []byte | ||
for pb.Next() { | ||
buf = AppendJSONString(buf[:0], s, true) | ||
} | ||
}) | ||
} | ||
|
||
func BenchmarkAppendJSONStringViaStrconv(b *testing.B) { | ||
b.Run("no-special-chars", func(b *testing.B) { | ||
benchmarkAppendJSONStringViaStrconv(b, "foo bar baz abc defkjlkj lkjdfs klsdjflfdjoqjo lkj ss") | ||
}) | ||
b.Run("with-special-chars", func(b *testing.B) { | ||
benchmarkAppendJSONStringViaStrconv(b, `foo bar baz abc defkjlkj lkjdf" klsdjflfdjoqjo\lkj ss`) | ||
}) | ||
} | ||
|
||
func benchmarkAppendJSONStringViaStrconv(b *testing.B, s string) { | ||
b.ReportAllocs() | ||
b.SetBytes(int64(len(s))) | ||
b.RunParallel(func(pb *testing.PB) { | ||
var buf []byte | ||
for pb.Next() { | ||
buf = strconv.AppendQuote(buf[:0], s) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters