couchbaselabs / gojsonsm

Go implementation of my JSONSM algorithm.
9 stars 7 forks source link

Optimize by minimizing len() calls #96

Closed nelio2k closed 5 years ago

nelio2k commented 5 years ago

While doing XDCR performance testing/optimization iterations, we find that calling len() in repeated ops could be quite expensive over time. Step() is one of the bigger offenders in CPU profile, and this change should remove a lot of len() calls that aren't really necessary.

Running local benchmark test shows improvements across the board:

Master:

neil.huang@NeilsMacbookPro:~/source/couchbase/godeps/src/github.com/couchbase/gojsonsm$ go test -bench=.
goos: darwin
goarch: amd64
pkg: github.com/couchbase/gojsonsm
BenchmarkBinTree-8              20000000            66.2 ns/op
BenchmarkParseString-8          300000000            5.88 ns/op
BenchmarkParseBigString-8       300000000            5.94 ns/op
BenchmarkParseEscString-8       30000000            52.9 ns/op
BenchmarkParseBigEscString-8    10000000           170 ns/op
BenchmarkParseInteger-8         100000000           12.7 ns/op
BenchmarkParseNumber-8          30000000            55.0 ns/op
BenchmarkParseNull-8            200000000            7.09 ns/op
BenchmarkParseTrue-8            200000000            8.34 ns/op
BenchmarkParseFalse-8           200000000            7.14 ns/op
BenchmarkTokenize-8                30000         45472 ns/op     343.63 MB/s
PASS
ok      github.com/couchbase/gojsonsm   21.338s

With len() calls removed:

neil.huang@NeilsMacbookPro:~/source/couchbase/godeps/src/github.com/couchbase/gojsonsm$ go test -bench=.
goos: darwin
goarch: amd64
pkg: github.com/couchbase/gojsonsm
BenchmarkBinTree-8              20000000            65.5 ns/op
BenchmarkParseString-8          300000000            5.71 ns/op
BenchmarkParseBigString-8       300000000            5.84 ns/op
BenchmarkParseEscString-8       30000000            51.4 ns/op
BenchmarkParseBigEscString-8    10000000           163 ns/op
BenchmarkParseInteger-8         100000000           12.3 ns/op
BenchmarkParseNumber-8          30000000            54.5 ns/op
BenchmarkParseNull-8            200000000            6.80 ns/op
BenchmarkParseTrue-8            200000000            7.98 ns/op
BenchmarkParseFalse-8           200000000            6.88 ns/op
BenchmarkTokenize-8                30000         42358 ns/op     368.90 MB/s
PASS
ok      github.com/couchbase/gojsonsm   20.686s