summaryrefslogtreecommitdiff
path: root/encode_string.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-01-26 21:02:56 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-01-30 22:00:25 -0700
commit2828fa21c0ffd2a32a108b37c0417b01abc42929 (patch)
treeae671b894fa952e01a410c94fe27e1d0fec37e80 /encode_string.go
parent8aa12d3cb043859229810947da6c52e600d34b55 (diff)
Avoid doing type switching in inner functions
The CPU profiler tells me that the encoder is spending a lot of time on type switches.
Diffstat (limited to 'encode_string.go')
-rw-r--r--encode_string.go16
1 files changed, 8 insertions, 8 deletions
diff --git a/encode_string.go b/encode_string.go
index c5cb442..831a038 100644
--- a/encode_string.go
+++ b/encode_string.go
@@ -45,7 +45,7 @@ func writeStringShortEscape(w io.Writer, c rune) (int, error) {
return w.Write(buf[:])
}
-func writeStringChar(w io.Writer, c rune, wasEscaped BackslashEscapeMode, escaper BackslashEscaper) (int, error) {
+func writeStringChar(w internal.AllWriter, c rune, wasEscaped BackslashEscapeMode, escaper BackslashEscaper) (int, error) {
if escaper == nil {
escaper = EscapeDefault
}
@@ -62,19 +62,19 @@ func writeStringChar(w io.Writer, c rune, wasEscaped BackslashEscapeMode, escape
case c == '"' || c == '\\': // override, gotta escape these
return writeStringShortEscape(w, c)
default: // obey
- return writeRune(w, c)
+ return w.WriteRune(c)
}
case BackslashEscapeShort:
switch c {
case '"', '\\', '/', '\b', '\f', '\n', '\r', '\t': // obey
return writeStringShortEscape(w, c)
default: // override, can't short-escape these
- return writeRune(w, c)
+ return w.WriteRune(c)
}
case BackslashEscapeUnicode:
switch {
case c > 0xFFFF: // override, can't escape these (TODO: unless we use UTF-16 surrogates?)
- return writeRune(w, c)
+ return w.WriteRune(c)
default: // obey
return writeStringUnicodeEscape(w, c)
}
@@ -83,7 +83,7 @@ func writeStringChar(w io.Writer, c rune, wasEscaped BackslashEscapeMode, escape
}
}
-func encodeStringFromString(w io.Writer, escaper BackslashEscaper, str string) {
+func encodeStringFromString(w internal.AllWriter, escaper BackslashEscaper, str string) {
encodeWriteByte(w, '"')
for _, c := range str {
if _, err := writeStringChar(w, c, BackslashEscapeNone, escaper); err != nil {
@@ -93,7 +93,7 @@ func encodeStringFromString(w io.Writer, escaper BackslashEscaper, str string) {
encodeWriteByte(w, '"')
}
-func encodeStringFromBytes(w io.Writer, escaper BackslashEscaper, str []byte) {
+func encodeStringFromBytes(w internal.AllWriter, escaper BackslashEscaper, str []byte) {
encodeWriteByte(w, '"')
for i := 0; i < len(str); {
c, size := utf8.DecodeRune(str[i:])
@@ -106,6 +106,6 @@ func encodeStringFromBytes(w io.Writer, escaper BackslashEscaper, str []byte) {
}
func init() {
- internal.EncodeStringFromString = func(w io.Writer, s string) { encodeStringFromString(w, nil, s) }
- internal.EncodeStringFromBytes = func(w io.Writer, s []byte) { encodeStringFromBytes(w, nil, s) }
+ internal.EncodeStringFromString = func(w io.Writer, s string) { encodeStringFromString(internal.NewAllWriter(w), nil, s) }
+ internal.EncodeStringFromBytes = func(w io.Writer, s []byte) { encodeStringFromBytes(internal.NewAllWriter(w), nil, s) }
}