Skip to content

Commit 4aa5792

Browse files
authored
Fix compilation on 32 bit architectures (#936)
This PR fixes a bug where Zoekt would not compile on 32-bit architectures. It also takes the opportunity to start using the `math` library everywhere instead of our own constants like `maxUInt32` to help prevent this sort of issue in the future by encouraging devs to select the most accurate "max" type for their specific situation. Closes #935
1 parent 5cad1f7 commit 4aa5792

File tree

8 files changed

+38
-34
lines changed

8 files changed

+38
-34
lines changed

cmd/zoekt-sourcegraph-indexserver/index.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (o *indexArgs) BuildOptions() *index.Options {
115115
// nothing needs to be done.
116116
RepositoryDescription: zoekt.Repository{
117117
TenantID: o.TenantID,
118-
ID: uint32(o.IndexOptions.RepoID),
118+
ID: o.IndexOptions.RepoID,
119119
Name: o.Name,
120120
Branches: o.Branches,
121121
RawConfig: map[string]string{

index/bits_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ package index
1717
import (
1818
"encoding/binary"
1919
"log"
20+
"math"
2021
"math/rand"
2122
"reflect"
23+
"slices"
2224
"strconv"
2325
"testing"
2426
"testing/quick"
2527

26-
"slices"
27-
2828
"github.com/google/go-cmp/cmp"
2929
)
3030

@@ -99,7 +99,7 @@ func TestNextFileIndex(t *testing.T) {
9999
ends []uint32
100100
want uint32
101101
}{
102-
{maxUInt32, 0, []uint32{34}, 1},
102+
{math.MaxUint32, 0, []uint32{34}, 1},
103103
{9, 0, []uint32{34}, 0},
104104
{450, 0, []uint32{100, 200, 300, 400, 500, 600}, 4},
105105
} {
@@ -150,7 +150,7 @@ func TestCompressedPostingIterator(t *testing.T) {
150150

151151
var nums []uint32
152152
i := newCompressedPostingIterator(data, stringToNGram("abc"))
153-
for i.first() != maxUInt32 {
153+
for i.first() != math.MaxUint32 {
154154
nums = append(nums, i.first())
155155
i.next(i.first())
156156
}

index/hititer.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@ package index
1717
import (
1818
"encoding/binary"
1919
"fmt"
20+
"math"
2021

2122
"github.com/sourcegraph/zoekt"
2223
)
2324

2425
// hitIterator finds potential search matches, measured in offsets of
2526
// the concatenation of all documents.
2627
type hitIterator interface {
27-
// Return the first hit, or maxUInt32 if none.
28+
// Return the first hit, or math.MaxUint32 if none.
2829
first() uint32
2930

30-
// Skip until past limit. The argument maxUInt32 should be
31+
// Skip until past limit. The argument math.MaxUint32 should be
3132
// treated specially.
3233
next(limit uint32)
3334

@@ -52,8 +53,8 @@ func (i *distanceHitIterator) findNext() {
5253
var p1, p2 uint32
5354
p1 = i.i1.first()
5455
p2 = i.i2.first()
55-
if p1 == maxUInt32 || p2 == maxUInt32 {
56-
i.i1.next(maxUInt32)
56+
if p1 == math.MaxUint32 || p2 == math.MaxUint32 {
57+
i.i1.next(math.MaxUint32)
5758
break
5859
}
5960

@@ -85,7 +86,7 @@ func (i *distanceHitIterator) next(limit uint32) {
8586
l2 := limit + i.distance
8687

8788
if l2 < limit { // overflow.
88-
l2 = maxUInt32
89+
l2 = math.MaxUint32
8990
}
9091
i.i2.next(l2)
9192
i.findNext()
@@ -158,14 +159,14 @@ func (i *inMemoryIterator) first() uint32 {
158159
if len(i.postings) > 0 {
159160
return i.postings[0]
160161
}
161-
return maxUInt32
162+
return math.MaxUint32
162163
}
163164

164-
func (i *inMemoryIterator) updateStats(s *zoekt.Stats) {
165+
func (i *inMemoryIterator) updateStats(_ *zoekt.Stats) {
165166
}
166167

167168
func (i *inMemoryIterator) next(limit uint32) {
168-
if limit == maxUInt32 {
169+
if limit == math.MaxUint32 {
169170
i.postings = nil
170171
}
171172

@@ -203,9 +204,9 @@ func (i *compressedPostingIterator) first() uint32 {
203204
}
204205

205206
func (i *compressedPostingIterator) next(limit uint32) {
206-
if limit == maxUInt32 {
207+
if limit == math.MaxUint32 {
207208
i.blob = nil
208-
i._first = maxUInt32
209+
i._first = math.MaxUint32
209210
return
210211
}
211212

@@ -217,7 +218,7 @@ func (i *compressedPostingIterator) next(limit uint32) {
217218
}
218219

219220
if i._first <= limit && len(i.blob) == 0 {
220-
i._first = maxUInt32
221+
i._first = math.MaxUint32
221222
}
222223
}
223224

@@ -248,7 +249,7 @@ func (i *mergingIterator) updateStats(s *zoekt.Stats) {
248249
}
249250

250251
func (i *mergingIterator) first() uint32 {
251-
r := uint32(maxUInt32)
252+
r := uint32(math.MaxUint32)
252253
for _, j := range i.iters {
253254
f := j.first()
254255
if f < r {

index/indexdata.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"hash/crc64"
2222
"log"
23+
"math"
2324
"math/bits"
2425
"slices"
2526
"unicode/utf8"
@@ -354,12 +355,10 @@ func findSelectiveNgrams(ngramOffs []runeNgramOff, indexMap []int, frequencies [
354355
return
355356
}
356357

357-
const maxUInt32 = 0xffffffff
358-
359358
func minFrequencyNgramOffsets(ngramOffs []runeNgramOff, frequencies []uint32) (first, last runeNgramOff) {
360359
// Find the two lowest frequency ngrams.
361360
idx0, idx1 := 0, 0
362-
min0, min1 := uint32(maxUInt32), uint32(maxUInt32)
361+
min0, min1 := uint32(math.MaxUint32), uint32(math.MaxUint32)
363362
for i, x := range frequencies {
364363
if x <= min0 {
365364
idx0, idx1 = i, idx0

index/indexfile.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package index
1919
import (
2020
"fmt"
2121
"log"
22+
"math"
2223
"os"
2324

2425
"golang.org/x/sys/unix"
@@ -62,7 +63,7 @@ func NewIndexFile(f *os.File) (IndexFile, error) {
6263
}
6364

6465
sz := fi.Size()
65-
if sz >= maxUInt32 {
66+
if sz >= math.MaxUint32 {
6667
return nil, fmt.Errorf("file %s too large: %d", f.Name(), sz)
6768
}
6869
r := &mmapedIndexFile{

index/matchiter.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package index
1717
import (
1818
"bytes"
1919
"fmt"
20+
"math"
2021

2122
"github.com/sourcegraph/zoekt"
2223
)
@@ -95,7 +96,7 @@ func (t *noMatchTree) candidates() []*candidateMatch {
9596
}
9697

9798
func (t *noMatchTree) nextDoc() uint32 {
98-
return maxUInt32
99+
return math.MaxUint32
99100
}
100101

101102
func (t *noMatchTree) prepare(uint32) {}
@@ -148,7 +149,7 @@ func nextFileIndex(offset, f uint32, ends []uint32) uint32 {
148149
func (i *ngramDocIterator) nextDoc() uint32 {
149150
i.fileIdx = nextFileIndex(i.iter.first(), i.fileIdx, i.ends)
150151
if i.fileIdx >= uint32(len(i.ends)) {
151-
return maxUInt32
152+
return math.MaxUint32
152153
}
153154
return i.fileIdx
154155
}
@@ -190,7 +191,7 @@ func (i *ngramDocIterator) candidates() []*candidateMatch {
190191
var candidates []*candidateMatch
191192
for {
192193
p1 := i.iter.first()
193-
if p1 == maxUInt32 || p1 >= i.ends[i.fileIdx] {
194+
if p1 == math.MaxUint32 || p1 >= i.ends[i.fileIdx] {
194195
break
195196
}
196197
i.iter.next(p1)

index/matchtree.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"bytes"
1919
"fmt"
2020
"log"
21+
"math"
2122
"regexp/syntax"
2223
"strings"
2324
"unicode/utf8"
@@ -442,7 +443,7 @@ func (t *docMatchTree) nextDoc() uint32 {
442443
return i
443444
}
444445
}
445-
return maxUInt32
446+
return math.MaxUint32
446447
}
447448

448449
func (t *bruteForceMatchTree) nextDoc() uint32 {
@@ -464,7 +465,7 @@ func (t *andMatchTree) nextDoc() uint32 {
464465
}
465466

466467
func (t *orMatchTree) nextDoc() uint32 {
467-
min := uint32(maxUInt32)
468+
min := uint32(math.MaxUint32)
468469
for _, c := range t.children {
469470
m := c.nextDoc()
470471
if m < min {
@@ -497,7 +498,7 @@ func (t *branchQueryMatchTree) nextDoc() uint32 {
497498
return i
498499
}
499500
}
500-
return maxUInt32
501+
return math.MaxUint32
501502
}
502503

503504
// all String methods
@@ -672,7 +673,7 @@ func (t *andLineMatchTree) matches(cp *contentProvider, cost int, known map[matc
672673
// can return MatchesFound.
673674

674675
// find child with fewest candidates
675-
min := maxUInt32
676+
minCount := math.MaxInt
676677
fewestChildren := 0
677678
for ix, child := range t.children {
678679
v, ok := child.(*substrMatchTree)
@@ -681,8 +682,8 @@ func (t *andLineMatchTree) matches(cp *contentProvider, cost int, known map[matc
681682
if !ok || v.fileName {
682683
return matchesFound
683684
}
684-
if len(v.current) < min {
685-
min = len(v.current)
685+
if len(v.current) < minCount {
686+
minCount = len(v.current)
686687
fewestChildren = ix
687688
}
688689
}

index/matchtree_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package index
1616

1717
import (
18+
"math"
1819
"reflect"
1920
"regexp/syntax"
2021
"testing"
@@ -304,7 +305,7 @@ func TestRepoSet(t *testing.T) {
304305
}
305306
mt.prepare(nextDoc)
306307
}
307-
if mt.nextDoc() != maxUInt32 {
308+
if mt.nextDoc() != math.MaxUint32 {
308309
t.Fatalf("expected %d document, but got at least 1 more", len(want))
309310
}
310311
}
@@ -327,7 +328,7 @@ func TestRepo(t *testing.T) {
327328
}
328329
mt.prepare(nextDoc)
329330
}
330-
if mt.nextDoc() != maxUInt32 {
331+
if mt.nextDoc() != math.MaxUint32 {
331332
t.Fatalf("expect %d documents, but got at least 1 more", len(want))
332333
}
333334
}
@@ -360,7 +361,7 @@ func TestBranchesRepos(t *testing.T) {
360361
mt.prepare(nextDoc)
361362
}
362363

363-
if mt.nextDoc() != maxUInt32 {
364+
if mt.nextDoc() != math.MaxUint32 {
364365
t.Fatalf("expect %d documents, but got at least 1 more", len(want))
365366
}
366367
}
@@ -383,7 +384,7 @@ func TestRepoIDs(t *testing.T) {
383384
}
384385
mt.prepare(nextDoc)
385386
}
386-
if mt.nextDoc() != maxUInt32 {
387+
if mt.nextDoc() != math.MaxUint32 {
387388
t.Fatalf("expected %d document, but got at least 1 more", len(want))
388389
}
389390
}

0 commit comments

Comments
 (0)