go/src/cmd/compile/internal/ssa/sparsemap.go

94 lines
2 KiB
Go
Raw Normal View History

// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ssa
import "cmd/internal/src"
// from https://research.swtch.com/sparse
// in turn, from Briggs and Torczon
type sparseEntry struct {
key ID
val int32
2016-12-15 17:17:01 -08:00
aux src.XPos
}
type sparseMap struct {
dense []sparseEntry
sparse []int32
}
// newSparseMap returns a sparseMap that can map
// integers between 0 and n-1 to int32s.
func newSparseMap(n int) *sparseMap {
return &sparseMap{dense: nil, sparse: make([]int32, n)}
}
cmd/compile: cache sparse maps across ssa passes This is done for sparse sets already, but it was missing for sparse maps. Only affects deadstore and regalloc, as they're the only ones that use sparse maps. name old time/op new time/op delta DSEPass-4 247µs ± 0% 216µs ± 0% -12.75% (p=0.008 n=5+5) DSEPassBlock-4 3.05ms ± 1% 2.87ms ± 1% -6.02% (p=0.002 n=6+6) CSEPass-4 2.30ms ± 0% 2.32ms ± 0% +0.53% (p=0.026 n=6+6) CSEPassBlock-4 23.8ms ± 0% 23.8ms ± 0% ~ (p=0.931 n=6+5) DeadcodePass-4 51.7µs ± 1% 51.5µs ± 2% ~ (p=0.429 n=5+6) DeadcodePassBlock-4 734µs ± 1% 742µs ± 3% ~ (p=0.394 n=6+6) MultiPass-4 152µs ± 0% 149µs ± 2% ~ (p=0.082 n=5+6) MultiPassBlock-4 2.67ms ± 1% 2.41ms ± 2% -9.77% (p=0.008 n=5+5) name old alloc/op new alloc/op delta DSEPass-4 41.2kB ± 0% 0.1kB ± 0% -99.68% (p=0.002 n=6+6) DSEPassBlock-4 560kB ± 0% 4kB ± 0% -99.34% (p=0.026 n=5+6) CSEPass-4 189kB ± 0% 189kB ± 0% ~ (all equal) CSEPassBlock-4 3.10MB ± 0% 3.10MB ± 0% ~ (p=0.444 n=5+5) DeadcodePass-4 10.5kB ± 0% 10.5kB ± 0% ~ (all equal) DeadcodePassBlock-4 164kB ± 0% 164kB ± 0% ~ (all equal) MultiPass-4 240kB ± 0% 199kB ± 0% -17.06% (p=0.002 n=6+6) MultiPassBlock-4 3.60MB ± 0% 2.99MB ± 0% -17.06% (p=0.002 n=6+6) name old allocs/op new allocs/op delta DSEPass-4 8.00 ± 0% 4.00 ± 0% -50.00% (p=0.002 n=6+6) DSEPassBlock-4 240 ± 0% 120 ± 0% -50.00% (p=0.002 n=6+6) CSEPass-4 9.00 ± 0% 9.00 ± 0% ~ (all equal) CSEPassBlock-4 1.35k ± 0% 1.35k ± 0% ~ (all equal) DeadcodePass-4 3.00 ± 0% 3.00 ± 0% ~ (all equal) DeadcodePassBlock-4 9.00 ± 0% 9.00 ± 0% ~ (all equal) MultiPass-4 11.0 ± 0% 10.0 ± 0% -9.09% (p=0.002 n=6+6) MultiPassBlock-4 165 ± 0% 150 ± 0% -9.09% (p=0.002 n=6+6) Change-Id: I43860687c88f33605eb1415f36473c5cfe8fde4a Reviewed-on: https://go-review.googlesource.com/98449 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
2018-03-05 10:46:44 +00:00
func (s *sparseMap) cap() int {
return len(s.sparse)
}
func (s *sparseMap) size() int {
return len(s.dense)
}
func (s *sparseMap) contains(k ID) bool {
i := s.sparse[k]
return i < int32(len(s.dense)) && s.dense[i].key == k
}
// get returns the value for key k, or -1 if k does
// not appear in the map.
func (s *sparseMap) get(k ID) int32 {
i := s.sparse[k]
if i < int32(len(s.dense)) && s.dense[i].key == k {
return s.dense[i].val
}
return -1
}
2016-12-15 17:17:01 -08:00
func (s *sparseMap) set(k ID, v int32, a src.XPos) {
i := s.sparse[k]
if i < int32(len(s.dense)) && s.dense[i].key == k {
s.dense[i].val = v
s.dense[i].aux = a
return
}
s.dense = append(s.dense, sparseEntry{k, v, a})
s.sparse[k] = int32(len(s.dense)) - 1
}
// setBit sets the v'th bit of k's value, where 0 <= v < 32
func (s *sparseMap) setBit(k ID, v uint) {
if v >= 32 {
panic("bit index too large.")
}
i := s.sparse[k]
if i < int32(len(s.dense)) && s.dense[i].key == k {
s.dense[i].val |= 1 << v
return
}
2016-12-15 17:17:01 -08:00
s.dense = append(s.dense, sparseEntry{k, 1 << v, src.NoXPos})
s.sparse[k] = int32(len(s.dense)) - 1
}
func (s *sparseMap) remove(k ID) {
i := s.sparse[k]
if i < int32(len(s.dense)) && s.dense[i].key == k {
y := s.dense[len(s.dense)-1]
s.dense[i] = y
s.sparse[y.key] = i
s.dense = s.dense[:len(s.dense)-1]
}
}
func (s *sparseMap) clear() {
s.dense = s.dense[:0]
}
func (s *sparseMap) contents() []sparseEntry {
return s.dense
}