]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: reduce element size of arrays in sparse{map,set}
authorDavid Chase <drchase@google.com>
Mon, 9 May 2016 18:59:25 +0000 (14:59 -0400)
committerRuss Cox <rsc@golang.org>
Wed, 18 May 2016 14:05:14 +0000 (14:05 +0000)
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>
src/cmd/compile/internal/ssa/sparsemap.go
src/cmd/compile/internal/ssa/sparseset.go

index 0211a70f09d2117db359736219020ec7550b5141..afb9f6049186cc7368d24054bdd3aafb32d92457 100644 (file)
@@ -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
index 66bebf139e66bfe2f6d755c45844374e9f0e0a0c..b5cabfb0cdf0835e4e9b9529531660030c02c9db 100644 (file)
@@ -9,13 +9,13 @@ package ssa
 
 type sparseSet struct {
        dense  []ID
-       sparse []int
+       sparse []int32
 }
 
 // newSparseSet returns a sparseSet that can represent
 // integers between 0 and n-1
 func newSparseSet(n int) *sparseSet {
-       return &sparseSet{nil, make([]int, n)}
+       return &sparseSet{dense: nil, sparse: make([]int32, n)}
 }
 
 func (s *sparseSet) cap() int {
@@ -28,16 +28,16 @@ func (s *sparseSet) size() int {
 
 func (s *sparseSet) contains(x ID) bool {
        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) {
        i := s.sparse[x]
-       if i < len(s.dense) && s.dense[i] == x {
+       if i < int32(len(s.dense)) && s.dense[i] == x {
                return
        }
        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) {
@@ -54,7 +54,7 @@ func (s *sparseSet) addAllValues(a []*Value) {
 
 func (s *sparseSet) remove(x ID) {
        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]
                s.dense[i] = y
                s.sparse[y] = i