Commit d35a4158 authored by David Chase's avatar David Chase Committed by Russ Cox

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: 's avatarKeith Randall <khr@golang.org>
Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
parent 2380a039
...@@ -14,13 +14,13 @@ type sparseEntry struct { ...@@ -14,13 +14,13 @@ type sparseEntry struct {
type sparseMap struct { type sparseMap struct {
dense []sparseEntry dense []sparseEntry
sparse []int sparse []int32
} }
// newSparseMap returns a sparseMap that can map // newSparseMap returns a sparseMap that can map
// integers between 0 and n-1 to int32s. // integers between 0 and n-1 to int32s.
func newSparseMap(n int) *sparseMap { func newSparseMap(n int) *sparseMap {
return &sparseMap{nil, make([]int, n)} return &sparseMap{dense: nil, sparse: make([]int32, n)}
} }
func (s *sparseMap) size() int { func (s *sparseMap) size() int {
...@@ -29,14 +29,14 @@ func (s *sparseMap) size() int { ...@@ -29,14 +29,14 @@ func (s *sparseMap) size() int {
func (s *sparseMap) contains(k ID) bool { func (s *sparseMap) contains(k ID) bool {
i := s.sparse[k] 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 // get returns the value for key k, or -1 if k does
// not appear in the map. // not appear in the map.
func (s *sparseMap) get(k ID) int32 { func (s *sparseMap) get(k ID) int32 {
i := s.sparse[k] 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 s.dense[i].val
} }
return -1 return -1
...@@ -44,12 +44,12 @@ func (s *sparseMap) get(k ID) int32 { ...@@ -44,12 +44,12 @@ func (s *sparseMap) get(k ID) int32 {
func (s *sparseMap) set(k ID, v int32) { func (s *sparseMap) set(k ID, v int32) {
i := s.sparse[k] 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 s.dense[i].val = v
return return
} }
s.dense = append(s.dense, sparseEntry{k, v}) 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 // 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) { ...@@ -58,17 +58,17 @@ func (s *sparseMap) setBit(k ID, v uint) {
panic("bit index too large.") panic("bit index too large.")
} }
i := s.sparse[k] 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 s.dense[i].val |= 1 << v
return return
} }
s.dense = append(s.dense, sparseEntry{k, 1 << v}) 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) { func (s *sparseMap) remove(k ID) {
i := s.sparse[k] 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] y := s.dense[len(s.dense)-1]
s.dense[i] = y s.dense[i] = y
s.sparse[y.key] = i s.sparse[y.key] = i
......
...@@ -9,13 +9,13 @@ package ssa ...@@ -9,13 +9,13 @@ package ssa
type sparseSet struct { type sparseSet struct {
dense []ID dense []ID
sparse []int sparse []int32
} }
// newSparseSet returns a sparseSet that can represent // newSparseSet returns a sparseSet that can represent
// integers between 0 and n-1 // integers between 0 and n-1
func newSparseSet(n int) *sparseSet { func newSparseSet(n int) *sparseSet {
return &sparseSet{nil, make([]int, n)} return &sparseSet{dense: nil, sparse: make([]int32, n)}
} }
func (s *sparseSet) cap() int { func (s *sparseSet) cap() int {
...@@ -28,16 +28,16 @@ func (s *sparseSet) size() int { ...@@ -28,16 +28,16 @@ func (s *sparseSet) size() int {
func (s *sparseSet) contains(x ID) bool { func (s *sparseSet) contains(x ID) bool {
i := s.sparse[x] i := s.sparse[x]
return i < len(s.dense) && s.dense[i] == x return i < int32(len(s.dense)) && s.dense[i] == x
} }
func (s *sparseSet) add(x ID) { func (s *sparseSet) add(x ID) {
i := s.sparse[x] i := s.sparse[x]
if i < len(s.dense) && s.dense[i] == x { if i < int32(len(s.dense)) && s.dense[i] == x {
return return
} }
s.dense = append(s.dense, x) s.dense = append(s.dense, x)
s.sparse[x] = len(s.dense) - 1 s.sparse[x] = int32(len(s.dense)) - 1
} }
func (s *sparseSet) addAll(a []ID) { func (s *sparseSet) addAll(a []ID) {
...@@ -54,7 +54,7 @@ func (s *sparseSet) addAllValues(a []*Value) { ...@@ -54,7 +54,7 @@ func (s *sparseSet) addAllValues(a []*Value) {
func (s *sparseSet) remove(x ID) { func (s *sparseSet) remove(x ID) {
i := s.sparse[x] i := s.sparse[x]
if i < len(s.dense) && s.dense[i] == x { if i < int32(len(s.dense)) && s.dense[i] == x {
y := s.dense[len(s.dense)-1] y := s.dense[len(s.dense)-1]
s.dense[i] = y s.dense[i] = y
s.sparse[y] = i s.sparse[y] = i
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment