Commit 730fd3b9 authored by Kirill Smelkov's avatar Kirill Smelkov

X xfmt/qpy: try to reorganizefor common case = ascii printable characters

name       old time/op    new time/op    delta
PyQuote-4     950ns ± 0%     894ns ± 0%  -5.89%  (p=0.008 n=5+5)

name       old alloc/op   new alloc/op   delta
PyQuote-4     0.00B          0.00B         ~     (all equal)

name       old allocs/op  new allocs/op  delta
PyQuote-4      0.00           0.00         ~     (all equal)
parent 80776e05
...@@ -47,28 +47,31 @@ func AppendQuotePyBytes(buf, b []byte) []byte { ...@@ -47,28 +47,31 @@ func AppendQuotePyBytes(buf, b []byte) []byte {
switch { switch {
// fast path - ASCII only - trying to avoid UTF-8 decoding // fast path - ASCII only - trying to avoid UTF-8 decoding
case c < utf8.RuneSelf: case c < utf8.RuneSelf:
switch c { switch {
case '\\', quote: case c == '\\' || c == quote:
buf = append(buf, '\\', c) buf = append(buf, '\\', c)
case ' ' <= c && c <= '\x7e':
// printable ASCII
buf = append(buf, c)
// below: non-printable ASCII
// NOTE python converts to \<letter> only \t \n \r (not e.g. \v) // NOTE python converts to \<letter> only \t \n \r (not e.g. \v)
// https://github.com/python/cpython/blob/v2.7.13-116-g1aa1803b3d/Objects/stringobject.c#L963 // https://github.com/python/cpython/blob/v2.7.13-116-g1aa1803b3d/Objects/stringobject.c#L963
case '\t': case c == '\t':
buf = append(buf, `\t`...) buf = append(buf, `\t`...)
case '\n': case c == '\n':
buf = append(buf, `\n`...) buf = append(buf, `\n`...)
case '\r': case c == '\r':
buf = append(buf, `\r`...) buf = append(buf, `\r`...)
default: default:
if c < ' ' || c == '\x7f' /* the only non-printable ASCII character > space */ { // NOTE c < ' ' or c == '\x7f' (the only non-printable ASCII character > space) here
// we already converted to \<letter> what python represents as such above // we already converted to \<letter> what python represents as such above
// everything else goes in numeric byte escapes // everything else goes in numeric byte escapes
buf = append(buf, '\\', 'x', hexdigits[c>>4], hexdigits[c&0xf]) buf = append(buf, '\\', 'x', hexdigits[c>>4], hexdigits[c&0xf])
} else {
// printable ASCII
buf = append(buf, c)
}
} }
b = b[1:] b = b[1:]
...@@ -77,21 +80,19 @@ func AppendQuotePyBytes(buf, b []byte) []byte { ...@@ -77,21 +80,19 @@ func AppendQuotePyBytes(buf, b []byte) []byte {
default: default:
r, size := utf8.DecodeRune(b) r, size := utf8.DecodeRune(b)
switch r { switch {
case utf8.RuneError: case r == utf8.RuneError:
// decode error - just emit raw byte as escaped
buf = append(buf, '\\', 'x', hexdigits[c>>4], hexdigits[c&0xf]) buf = append(buf, '\\', 'x', hexdigits[c>>4], hexdigits[c&0xf])
default: case strconv.IsPrint(r):
switch { // printable utf-8 characters go as is
case strconv.IsPrint(r): buf = append(buf, b[:size]...)
// printable utf-8 characters go as is
buf = append(buf, b[:size]...)
default: default:
// everything else goes in numeric byte escapes // everything else goes in numeric byte escapes
for i := 0; i < size; i++ { for i := 0; i < size; i++ {
buf = append(buf, '\\', 'x', hexdigits[b[i]>>4], hexdigits[b[i]&0xf]) buf = append(buf, '\\', 'x', hexdigits[b[i]>>4], hexdigits[b[i]&0xf])
}
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment