From 2828fa21c0ffd2a32a108b37c0417b01abc42929 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Thu, 26 Jan 2023 21:02:56 -0700 Subject: Avoid doing type switching in inner functions The CPU profiler tells me that the encoder is spending a lot of time on type switches. --- encode_string.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'encode_string.go') 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) } } -- cgit v1.2.3-2-g168b From d5b1b73eaaa060ef468f20d8b9eed029eb60ce45 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 27 Jan 2023 01:24:02 -0700 Subject: encode: Don't use panic for flow-control --- encode_string.go | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) (limited to 'encode_string.go') diff --git a/encode_string.go b/encode_string.go index 831a038..12f934e 100644 --- a/encode_string.go +++ b/encode_string.go @@ -83,29 +83,47 @@ func writeStringChar(w internal.AllWriter, c rune, wasEscaped BackslashEscapeMod } } -func encodeStringFromString(w internal.AllWriter, escaper BackslashEscaper, str string) { - encodeWriteByte(w, '"') +func encodeStringFromString(w internal.AllWriter, escaper BackslashEscaper, str string) error { + if err := w.WriteByte('"'); err != nil { + return err + } for _, c := range str { if _, err := writeStringChar(w, c, BackslashEscapeNone, escaper); err != nil { - panic(encodeError{err}) + return err } } - encodeWriteByte(w, '"') + if err := w.WriteByte('"'); err != nil { + return err + } + return nil } -func encodeStringFromBytes(w internal.AllWriter, escaper BackslashEscaper, str []byte) { - encodeWriteByte(w, '"') +func encodeStringFromBytes(w internal.AllWriter, escaper BackslashEscaper, str []byte) error { + if err := w.WriteByte('"'); err != nil { + return err + } for i := 0; i < len(str); { c, size := utf8.DecodeRune(str[i:]) if _, err := writeStringChar(w, c, BackslashEscapeNone, escaper); err != nil { - panic(encodeError{err}) + return err } i += size } - encodeWriteByte(w, '"') + if err := w.WriteByte('"'); err != nil { + return err + } + return nil } func init() { - 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) } + internal.EncodeStringFromString = func(w io.Writer, s string) { + if err := encodeStringFromString(internal.NewAllWriter(w), nil, s); err != nil { + panic(err) + } + } + internal.EncodeStringFromBytes = func(w io.Writer, s []byte) { + if err := encodeStringFromBytes(internal.NewAllWriter(w), nil, s); err != nil { + panic(err) + } + } } -- cgit v1.2.3-2-g168b