tstime: hand-implement parseInt for specific needs of rfc3339 parsing.

Makes parsing 4.6x faster.

name         old time/op  new time/op  delta
ParseInt-12  32.1ns ± 1%   6.9ns ± 2%  -78.55%  (p=0.000 n=10+9)

Signed-off-by: David Anderson <danderson@tailscale.com>
This commit is contained in:
David Anderson
2020-04-14 12:05:19 -07:00
committed by Dave Anderson
parent dc9b39e3fb
commit b925e18f70
2 changed files with 44 additions and 4 deletions

View File

@@ -8,7 +8,6 @@ package tstime
import (
"errors"
"fmt"
"strconv"
"strings"
"sync"
"time"
@@ -105,10 +104,19 @@ func Parse3339(s string) (time.Time, error) {
}
func parseInt(s string, dst *int) bool {
n, err := strconv.ParseInt(s, 10, 0)
if err != nil || n < 0 {
if len(s) == 0 || len(s) > len("999999999") {
*dst = 0
return false
}
*dst = int(n)
n := 0
for i := 0; i < len(s); i++ {
d := s[i] - '0'
if d > 9 {
*dst = 0
return false
}
n = n*10 + int(d)
}
*dst = n
return true
}