Skip to content

Commit a9b6916

Browse files
committed
Alter first buf size depending on map size
1 parent 562881d commit a9b6916

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

uint32_store.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func NewUint32Store(src Uint32Source) Uint32Store {
5252
// more efficient than continually using append.
5353
func uint32Build(keys []string, src Uint32Source) []byteValue {
5454
b := uint32Builder{
55-
all: [][]byteValue{make([]byteValue, 1, 256)},
55+
all: [][]byteValue{make([]byteValue, 1, firstBufSize(len(keys)))},
5656
src: src,
5757
len: 1,
5858
}
@@ -94,6 +94,16 @@ func (b *uint32Builder) makeByteValue(bv *byteValue, a []string, byteIndex int)
9494
}
9595
}
9696

97+
const maxBuildBufSize = 1 << 20
98+
99+
func firstBufSize(mapSize int) int {
100+
size := 1 << 4
101+
for size < mapSize && size < maxBuildBufSize {
102+
size <<= 1
103+
}
104+
return size
105+
}
106+
97107
// alloc will grab space in the current block if available or allocate a new one if not
98108
func (b *uint32Builder) alloc(nByteValues byte) []byteValue {
99109
n := int(nByteValues)
@@ -105,7 +115,7 @@ func (b *uint32Builder) alloc(nByteValues byte) []byteValue {
105115
return (*cur)[curLen:]
106116
}
107117
newCap := curCap
108-
if newCap < 1<<20 {
118+
if newCap < maxBuildBufSize {
109119
newCap *= 2
110120
}
111121
a := make([]byteValue, n, newCap)

0 commit comments

Comments
 (0)