Skip to content

Commit

Permalink
runtime: intern some small strings
Browse files Browse the repository at this point in the history
DO NOT SUBMIT

Updates golang#5160

This is the simplest thing I could think of to experiment with.

It's incomplete: It doesn't cover all the ways to create strings.
It's not optimized at all. I picked the magic numbers haphazardly.

Nevertheless, it doesn't do too badly, when applied to the compiler:

name        old time/op       new time/op       delta
Template          187ms ± 3%        184ms ± 2%  -1.47%  (p=0.000 n=97+95)
Unicode          86.9ms ± 5%       87.3ms ± 4%    ~     (p=0.065 n=99+94)
GoTypes           658ms ± 2%        659ms ± 2%    ~     (p=0.614 n=99+97)
Compiler          2.94s ± 2%        2.94s ± 2%    ~     (p=0.945 n=95+95)
SSA               8.53s ± 1%        8.54s ± 1%    ~     (p=0.276 n=97+98)
Flate             125ms ± 3%        124ms ± 4%  -0.78%  (p=0.000 n=99+97)
GoParser          149ms ± 4%        149ms ± 3%    ~     (p=0.595 n=100+95)
Reflect           410ms ± 3%        412ms ± 4%  +0.48%  (p=0.047 n=97+96)
Tar               167ms ± 3%        166ms ± 5%    ~     (p=0.078 n=99+98)
XML               227ms ± 3%        227ms ± 4%    ~     (p=0.723 n=96+95)
[Geo mean]        388ms             387ms       -0.17%

name        old user-time/op  new user-time/op  delta
Template          223ms ± 3%        221ms ± 3%  -0.78%  (p=0.000 n=99+95)
Unicode           109ms ± 8%        111ms ± 6%  +1.36%  (p=0.001 n=99+99)
GoTypes           846ms ± 2%        848ms ± 2%    ~     (p=0.092 n=97+97)
Compiler          3.91s ± 2%        3.91s ± 2%    ~     (p=0.666 n=97+96)
SSA               12.1s ± 2%        12.1s ± 1%    ~     (p=0.128 n=92+96)
Flate             145ms ± 3%        144ms ± 4%    ~     (p=0.157 n=93+99)
GoParser          180ms ± 5%        181ms ± 5%  +0.63%  (p=0.004 n=90+94)
Reflect           522ms ± 3%        524ms ± 4%    ~     (p=0.055 n=95+96)
Tar               203ms ± 5%        203ms ± 5%    ~     (p=0.880 n=100+99)
XML               280ms ± 4%        281ms ± 4%    ~     (p=0.170 n=99+98)
[Geo mean]        487ms             488ms       +0.19%

name        old alloc/op      new alloc/op      delta
Template         36.3MB ± 0%       36.2MB ± 0%  -0.23%  (p=0.008 n=5+5)
Unicode          29.7MB ± 0%       29.7MB ± 0%  -0.07%  (p=0.008 n=5+5)
GoTypes           126MB ± 0%        126MB ± 0%  -0.27%  (p=0.008 n=5+5)
Compiler          537MB ± 0%        536MB ± 0%  -0.21%  (p=0.008 n=5+5)
SSA              2.00GB ± 0%       2.00GB ± 0%  -0.12%  (p=0.008 n=5+5)
Flate            24.6MB ± 0%       24.6MB ± 0%  -0.28%  (p=0.008 n=5+5)
GoParser         29.4MB ± 0%       29.4MB ± 0%  -0.20%  (p=0.008 n=5+5)
Reflect          87.3MB ± 0%       86.9MB ± 0%  -0.52%  (p=0.008 n=5+5)
Tar              35.6MB ± 0%       35.5MB ± 0%  -0.28%  (p=0.008 n=5+5)
XML              48.4MB ± 0%       48.4MB ± 0%  -0.16%  (p=0.008 n=5+5)
[Geo mean]       83.3MB            83.1MB       -0.23%

name        old allocs/op     new allocs/op     delta
Template           352k ± 0%         347k ± 0%  -1.16%  (p=0.008 n=5+5)
Unicode            341k ± 0%         339k ± 0%  -0.76%  (p=0.008 n=5+5)
GoTypes           1.28M ± 0%        1.26M ± 0%  -1.48%  (p=0.008 n=5+5)
Compiler          4.97M ± 0%        4.90M ± 0%  -1.38%  (p=0.008 n=5+5)
SSA               15.6M ± 0%        15.3M ± 0%  -2.11%  (p=0.016 n=4+5)
Flate              233k ± 0%         228k ± 0%  -1.92%  (p=0.008 n=5+5)
GoParser           294k ± 0%         290k ± 0%  -1.32%  (p=0.008 n=5+5)
Reflect           1.04M ± 0%        1.03M ± 0%  -1.73%  (p=0.008 n=5+5)
Tar                343k ± 0%         337k ± 0%  -1.62%  (p=0.008 n=5+5)
XML                432k ± 0%         426k ± 0%  -1.28%  (p=0.008 n=5+5)
[Geo mean]         813k              801k       -1.48%

Change-Id: I4cd95bf4a74479b0e8a8d339d77e248d1467a6e0
  • Loading branch information
josharian committed Oct 12, 2018
1 parent 5745652 commit ab074c9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/runtime/runtime2.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,8 @@ type p struct {
deferpool [5][]*_defer // pool of available defer structs of different sizes (see panic.go)
deferpoolbuf [5][32]*_defer

strings [16]string // interned strings

// Cache of goroutine ids, amortizes accesses to runtime·sched.goidgen.
goidcache uint64
goidcacheend uint64
Expand Down
15 changes: 15 additions & 0 deletions src/runtime/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,29 @@ func slicebytetostring(buf *tmpBuf, b []byte) (str string) {
}

var p unsafe.Pointer
var dst *string
if buf != nil && len(b) <= len(buf) {
p = unsafe.Pointer(buf)
} else {
if len(b) < 64 {
curp := getg().m.p.ptr()
for _, s := range &curp.strings {
if string(b) == s {
// println("HIT")
return s
}
}
dst = &curp.strings[fastrand()%uint32(len(curp.strings))]
}
p = mallocgc(uintptr(len(b)), nil, false)
}
stringStructOf(&str).str = p
stringStructOf(&str).len = len(b)
memmove(p, (*(*slice)(unsafe.Pointer(&b))).array, uintptr(len(b)))
if dst != nil {
// println("STORE")
*dst = str
}
return
}

Expand Down

0 comments on commit ab074c9

Please sign in to comment.