mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cmd/compile: reduce element size of arrays in sparse{map,set}
sparseSet and sparseMap only need 32 bit integers in their arrays, since a sparseEntry key is also limited to 32 bits. This appears to reduce the space allocated for at least one pathological compilation by 1%, perhaps more. Not necessarily for 1.7, but it saves a little and is very low-risk. Change-Id: Icf1185859e9f5fe1261a206b441e02c34f7d02fd Reviewed-on: https://go-review.googlesource.com/22972 Run-TryBot: David Chase <drchase@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Keith Randall <khr@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
parent
2380a039c0
commit
d35a4158ab
2 changed files with 15 additions and 15 deletions
|
|
@ -14,13 +14,13 @@ type sparseEntry struct {
|
|||
|
||||
type sparseMap struct {
|
||||
dense []sparseEntry
|
||||
sparse []int
|
||||
sparse []int32
|
||||
}
|
||||
|
||||
// newSparseMap returns a sparseMap that can map
|
||||
// integers between 0 and n-1 to int32s.
|
||||
func newSparseMap(n int) *sparseMap {
|
||||
return &sparseMap{nil, make([]int, n)}
|
||||
return &sparseMap{dense: nil, sparse: make([]int32, n)}
|
||||
}
|
||||
|
||||
func (s *sparseMap) size() int {
|
||||
|
|
@ -29,14 +29,14 @@ func (s *sparseMap) size() int {
|
|||
|
||||
func (s *sparseMap) contains(k ID) bool {
|
||||
i := s.sparse[k]
|
||||
return i < len(s.dense) && s.dense[i].key == 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 < len(s.dense) && s.dense[i].key == k {
|
||||
if i < int32(len(s.dense)) && s.dense[i].key == k {
|
||||
return s.dense[i].val
|
||||
}
|
||||
return -1
|
||||
|
|
@ -44,12 +44,12 @@ func (s *sparseMap) get(k ID) int32 {
|
|||
|
||||
func (s *sparseMap) set(k ID, v int32) {
|
||||
i := s.sparse[k]
|
||||
if i < len(s.dense) && s.dense[i].key == k {
|
||||
if i < int32(len(s.dense)) && s.dense[i].key == k {
|
||||
s.dense[i].val = v
|
||||
return
|
||||
}
|
||||
s.dense = append(s.dense, sparseEntry{k, v})
|
||||
s.sparse[k] = len(s.dense) - 1
|
||||
s.sparse[k] = int32(len(s.dense)) - 1
|
||||
}
|
||||
|
||||
// setBit sets the v'th bit of k's value, where 0 <= v < 32
|
||||
|
|
@ -58,17 +58,17 @@ func (s *sparseMap) setBit(k ID, v uint) {
|
|||
panic("bit index too large.")
|
||||
}
|
||||
i := s.sparse[k]
|
||||
if i < len(s.dense) && s.dense[i].key == k {
|
||||
if i < int32(len(s.dense)) && s.dense[i].key == k {
|
||||
s.dense[i].val |= 1 << v
|
||||
return
|
||||
}
|
||||
s.dense = append(s.dense, sparseEntry{k, 1 << v})
|
||||
s.sparse[k] = len(s.dense) - 1
|
||||
s.sparse[k] = int32(len(s.dense)) - 1
|
||||
}
|
||||
|
||||
func (s *sparseMap) remove(k ID) {
|
||||
i := s.sparse[k]
|
||||
if i < len(s.dense) && s.dense[i].key == 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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue