diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8c5eeae..4d18e2e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,7 +24,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v2 with: - go-version: '~1.16.6' + go-version: '~1.17.9' id: go - name: Install utilities diff --git a/uint32_store.go b/uint32_store.go index a909d6b..81f150c 100644 --- a/uint32_store.go +++ b/uint32_store.go @@ -114,10 +114,13 @@ func (b *uint32Builder) alloc(nByteValues byte) []byteValue { *cur = (*cur)[: curLen+n : curCap] return (*cur)[curLen:] } - newCap := curCap - if newCap < maxBuildBufSize { + newCap := curCap * 2 + for newCap < n { newCap *= 2 } + if newCap > maxBuildBufSize { + newCap = maxBuildBufSize + } a := make([]byteValue, n, newCap) b.all = append(b.all, a) return a diff --git a/uint32_store_test.go b/uint32_store_test.go index 7ce8942..579eb9c 100644 --- a/uint32_store_test.go +++ b/uint32_store_test.go @@ -14,22 +14,22 @@ import ( ) func TestFastStringToUint32Empty(t *testing.T) { - ms := mapSliceN(nil, 0) - fm := faststringmap.NewUint32Store(ms) - if keys := ms.AppendKeys(nil); len(keys) != 0 { - t.Errorf("keys should be empty, got %#v", keys) - } - for _, k := range []string{"", "a", "foo", "ß"} { - if actV, ok := fm.LookupString(k); ok { - t.Errorf("%q present when not expected, got %d", k, actV) - } - } + ms := mapSliceN(map[string]uint32{"": 1, "a": 2, "foo": 3, "ß": 4}, 0) + checkWithMapSlice(t, ms) +} + +func TestFastStringToUint32BigSpan(t *testing.T) { + ms := mapSliceN(map[string]uint32{"a!": 1, "a~": 2}, 2) + checkWithMapSlice(t, ms) } func TestFastStringToUint32(t *testing.T) { const nStrs = 8192 m := randomSmallStrings(nStrs, 8) - ms := mapSliceN(m, len(m)/2) + checkWithMapSlice(t, mapSliceN(m, len(m)/2)) +} + +func checkWithMapSlice(t *testing.T, ms mapSlice) { fm := faststringmap.NewUint32Store(ms) for _, k := range ms.in {