Skip to content

Commit

Permalink
feat: avoid regression in Plain case, split str and strSlow
Browse files Browse the repository at this point in the history
name                                      old time/op    new time/op    delta
Decoder_StrBytes/Plain/1b-12                9.47ns ± 0%    9.62ns ± 1%   +1.56%  (p=0.000 n=13+15)
Decoder_StrBytes/Plain/8b-12                11.3ns ± 1%    11.6ns ± 1%   +2.67%  (p=0.000 n=15+15)
Decoder_StrBytes/Plain/16b-12               13.9ns ± 0%    13.9ns ± 1%     ~     (p=0.123 n=15+15)
Decoder_StrBytes/Plain/64b-12               28.0ns ± 0%    28.1ns ± 1%   +0.35%  (p=0.001 n=13+14)
Decoder_StrBytes/Plain/128b-12              46.2ns ± 2%    46.3ns ± 1%     ~     (p=0.097 n=15+15)
Decoder_StrBytes/Plain/1024b-12              295ns ± 0%     295ns ± 0%     ~     (p=0.894 n=15+12)
Decoder_StrBytes/EscapedNewline/2b-12       43.2ns ± 1%    50.2ns ± 1%  +15.98%  (p=0.000 n=14+15)
Decoder_StrBytes/EscapedNewline/16b-12       138ns ± 1%     145ns ± 1%   +5.61%  (p=0.000 n=15+14)
Decoder_StrBytes/EscapedNewline/32b-12       253ns ± 0%     279ns ± 1%  +10.33%  (p=0.000 n=14+14)
Decoder_StrBytes/EscapedNewline/128b-12      839ns ± 0%     933ns ± 0%  +11.15%  (p=0.000 n=12+13)
Decoder_StrBytes/EscapedNewline/256b-12     1.56µs ± 0%    1.74µs ± 0%  +11.80%  (p=0.000 n=12+15)
Decoder_StrBytes/EscapedNewline/2048b-12    11.3µs ± 0%    12.8µs ± 0%  +13.30%  (p=0.000 n=15+15)
Decoder_StrBytes/EscapedUnicode/6b-12       57.3ns ± 0%    69.3ns ± 2%  +20.89%  (p=0.000 n=13+15)
Decoder_StrBytes/EscapedUnicode/48b-12       231ns ± 2%     247ns ± 1%   +7.11%  (p=0.000 n=15+13)
Decoder_StrBytes/EscapedUnicode/96b-12       446ns ± 2%     475ns ± 1%   +6.70%  (p=0.000 n=15+15)
Decoder_StrBytes/EscapedUnicode/384b-12     1.64µs ± 2%    1.70µs ± 1%   +3.55%  (p=0.000 n=15+15)
Decoder_StrBytes/EscapedUnicode/768b-12     3.18µs ± 1%    3.25µs ± 1%   +2.41%  (p=0.000 n=13+14)
Decoder_StrBytes/EscapedUnicode/6144b-12    24.3µs ± 2%    24.9µs ± 0%   +2.68%  (p=0.000 n=15+13)
Decoder_StrBytes/Mixed/21b-12                144ns ± 1%     114ns ± 1%  -20.33%  (p=0.000 n=15+15)
Decoder_StrBytes/Mixed/168b-12               860ns ± 2%     574ns ± 2%  -33.32%  (p=0.000 n=14+15)
Decoder_StrBytes/Mixed/336b-12              1.61µs ± 2%    1.01µs ± 1%  -37.35%  (p=0.000 n=15+15)

name                                      old speed      new speed      delta
Decoder_StrBytes/Plain/1b-12               317MB/s ± 0%   312MB/s ± 1%   -1.53%  (p=0.000 n=13+15)
Decoder_StrBytes/Plain/8b-12               883MB/s ± 1%   861MB/s ± 1%   -2.60%  (p=0.000 n=15+15)
Decoder_StrBytes/Plain/16b-12             1.30GB/s ± 0%  1.30GB/s ± 1%     ~     (p=0.202 n=15+15)
Decoder_StrBytes/Plain/64b-12             2.36GB/s ± 0%  2.35GB/s ± 1%   -0.36%  (p=0.002 n=13+14)
Decoder_StrBytes/Plain/128b-12            2.81GB/s ± 2%  2.81GB/s ± 1%     ~     (p=0.089 n=15+15)
Decoder_StrBytes/Plain/1024b-12           3.48GB/s ± 0%  3.48GB/s ± 0%     ~     (p=0.943 n=15+12)
Decoder_StrBytes/EscapedNewline/2b-12     92.5MB/s ± 1%  79.8MB/s ± 1%  -13.78%  (p=0.000 n=14+15)
Decoder_StrBytes/EscapedNewline/16b-12     131MB/s ± 1%   124MB/s ± 1%   -5.31%  (p=0.000 n=15+14)
Decoder_StrBytes/EscapedNewline/32b-12     134MB/s ± 0%   122MB/s ± 1%   -9.36%  (p=0.000 n=14+14)
Decoder_StrBytes/EscapedNewline/128b-12    155MB/s ± 0%   139MB/s ± 0%  -10.03%  (p=0.000 n=12+13)
Decoder_StrBytes/EscapedNewline/256b-12    165MB/s ± 0%   148MB/s ± 0%  -10.56%  (p=0.000 n=12+15)
Decoder_StrBytes/EscapedNewline/2048b-12   182MB/s ± 0%   160MB/s ± 0%  -11.74%  (p=0.000 n=15+15)
Decoder_StrBytes/EscapedUnicode/6b-12      140MB/s ± 0%   116MB/s ± 2%  -17.27%  (p=0.000 n=13+15)
Decoder_StrBytes/EscapedUnicode/48b-12     217MB/s ± 2%   202MB/s ± 1%   -6.66%  (p=0.000 n=15+13)
Decoder_StrBytes/EscapedUnicode/96b-12     220MB/s ± 2%   206MB/s ± 1%   -6.28%  (p=0.000 n=15+15)
Decoder_StrBytes/EscapedUnicode/384b-12    236MB/s ± 2%   227MB/s ± 1%   -3.44%  (p=0.000 n=15+15)
Decoder_StrBytes/EscapedUnicode/768b-12    242MB/s ± 1%   237MB/s ± 1%   -2.35%  (p=0.000 n=13+14)
Decoder_StrBytes/EscapedUnicode/6144b-12   253MB/s ± 2%   246MB/s ± 0%   -2.62%  (p=0.000 n=15+13)
Decoder_StrBytes/Mixed/21b-12              160MB/s ± 1%   201MB/s ± 1%  +25.52%  (p=0.000 n=15+15)
Decoder_StrBytes/Mixed/168b-12             198MB/s ± 2%   296MB/s ± 2%  +49.96%  (p=0.000 n=14+15)
Decoder_StrBytes/Mixed/336b-12             210MB/s ± 2%   335MB/s ± 1%  +59.64%  (p=0.000 n=15+15)
  • Loading branch information
tdakkota committed Jan 18, 2022
1 parent 2867c4b commit 2025236
Showing 1 changed file with 91 additions and 8 deletions.
99 changes: 91 additions & 8 deletions dec_str.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,97 @@ func (d *Decoder) str(v value) (value, error) {
return value{}, errors.Wrap(err, "start")
}
var (
c byte
i int
copied bool
c byte
i int
)
for {
buf := d.buf[d.head:d.tail]
for len(buf) >= 8 {
c = buf[0]
if safeSet[c] != 0 {
goto readTok
}
i++

c = buf[1]
if safeSet[c] != 0 {
goto readTok
}
i++

c = buf[2]
if safeSet[c] != 0 {
goto readTok
}
i++

c = buf[3]
if safeSet[c] != 0 {
goto readTok
}
i++

c = buf[4]
if safeSet[c] != 0 {
goto readTok
}
i++

c = buf[5]
if safeSet[c] != 0 {
goto readTok
}
i++

c = buf[6]
if safeSet[c] != 0 {
goto readTok
}
i++

c = buf[7]
if safeSet[c] != 0 {
goto readTok
}
i++

buf = buf[8:]
}
var n int
for n, c = range buf {
if safeSet[c] != 0 {
i += n
goto readTok
}
}
return d.strSlow(v)
}
readTok:
buf := d.buf[d.head:d.tail]
str := buf[:i]

switch {
case c == '"':
// Skip string + last quote.
d.head += i + 1
if v.raw {
return value{buf: str}, nil
}
return value{buf: append(v.buf, str...)}, nil
case c == '\\':
// Skip only string, keep quote in buffer.
d.head += i
// We need a copy anyway, because string is escaped.
return d.strSlow(value{buf: append(v.buf, str...)})
default:
return v, badToken(c)
}
}

func (d *Decoder) strSlow(v value) (value, error) {
var (
c byte
i int
)
readStr:
for {
Expand Down Expand Up @@ -126,7 +214,6 @@ readStr:
}

v.buf = append(v.buf, d.buf[d.head:d.head+i]...)
copied = true
if err := d.read(); err != nil {
if err == io.EOF {
return value{}, io.ErrUnexpectedEOF
Expand All @@ -141,12 +228,8 @@ readTok:

switch {
case c == '"':
if v.raw && !copied {
return value{buf: str}, nil
}
return value{buf: append(v.buf, str...)}, nil
case c == '\\':
copied = true
v.buf = append(v.buf, str...)
c, err := d.byte()
if err != nil {
Expand Down

0 comments on commit 2025236

Please sign in to comment.