]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: simplify cpuprof hash calculation
authorMatthew Dempsky <mdempsky@google.com>
Thu, 18 Dec 2014 21:50:57 +0000 (13:50 -0800)
committerMinux Ma <minux@golang.org>
Fri, 19 Dec 2014 02:50:40 +0000 (02:50 +0000)
"x*41" computes the same value as "x*31 + x*7 + x*3" and (when
compiled by gc) requires just one multiply instruction instead of
three.

Alternatively, the expression could be written as "(x<<2+x)<<3 + x" to
use shifts instead of multiplies (which is how GCC optimizes "x*41").
But gc currently emits suboptimal instructions for this expression
anyway (e.g., separate SHL+ADD instructions rather than LEA on
386/amd64). Also, if such an optimization was worthwhile, it would
seem better to implement it as part of gc's strength reduction logic.

Change-Id: I7156b793229d723bbc9a52aa9ed6111291335277
Reviewed-on: https://go-review.googlesource.com/1830
Reviewed-by: Minux Ma <minux@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/runtime/cpuprof.go

index d56678e21f603e760283f968d77be1aa583ad118..a9bb1add85a5a0140451cf557fe8c40dc0db32e0 100644 (file)
@@ -203,7 +203,7 @@ func (p *cpuProfile) add(pc []uintptr) {
        h := uintptr(0)
        for _, x := range pc {
                h = h<<8 | (h >> (8 * (unsafe.Sizeof(h) - 1)))
-               h += x*31 + x*7 + x*3
+               h += x * 41
        }
        p.count++