Skip to content

Commit 9a03eb7

Browse files
bindings/go: Simplify type conversions & reuse buffer (#175)
1 parent 8a7b7a3 commit 9a03eb7

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

bindings/go/scip/parse.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,16 @@ func (pi *IndexVisitor) ParseStreaming(r io.Reader) error {
5555
return errors.Newf("expected LEN type tag for %s", indexFieldName(fieldNumber))
5656
}
5757
lenBuf = lenBuf[:0]
58-
dataLen, err := readVarint(r, lenBuf)
58+
dataLenUint, err := readVarint(r, &lenBuf)
59+
dataLen := int(dataLenUint)
5960
if err != nil {
6061
return errors.Wrapf(err, "failed to read length for %s", indexFieldName(fieldNumber))
6162
}
62-
if dataLen > uint64(cap(dataBuf)) {
63+
if dataLen > cap(dataBuf) {
6364
dataBuf = make([]byte, dataLen)
6465
} else {
6566
dataBuf = dataBuf[:0]
66-
for i := uint64(0); i < dataLen; i++ {
67+
for i := 0; i < dataLen; i++ {
6768
dataBuf = append(dataBuf, 0)
6869
}
6970
}
@@ -73,7 +74,7 @@ func (pi *IndexVisitor) ParseStreaming(r io.Reader) error {
7374
if err != nil {
7475
return errors.Wrapf(err, "failed to read data for %s", indexFieldName(fieldNumber))
7576
}
76-
if uint64(numRead) != dataLen {
77+
if numRead != dataLen {
7778
return errors.Newf(
7879
"expected to read %d bytes based on LEN but read %d bytes", dataLen, numRead)
7980
}
@@ -115,9 +116,9 @@ const (
115116
//
116117
// scratchBuf should be able to accommodate any varint size
117118
// based on its capacity, and be cleared before readVarint is called
118-
func readVarint(r io.Reader, scratchBuf []byte) (uint64, error) {
119+
func readVarint(r io.Reader, scratchBuf *[]byte) (uint64, error) {
119120
nextByteBuf := make([]byte, 1, 1)
120-
for i := 0; i < cap(scratchBuf); i++ {
121+
for i := 0; i < cap(*scratchBuf); i++ {
121122
numRead, err := r.Read(nextByteBuf)
122123
if err != nil {
123124
return 0, errors.Wrapf(err, "failed to read %d-th byte of Varint. soFar: %v", i, scratchBuf)
@@ -126,13 +127,13 @@ func readVarint(r io.Reader, scratchBuf []byte) (uint64, error) {
126127
return 0, errors.Newf("failed to read %d-th byte of Varint. soFar: %v", scratchBuf)
127128
}
128129
nextByte := nextByteBuf[0]
129-
scratchBuf = append(scratchBuf, nextByte)
130+
*scratchBuf = append(*scratchBuf, nextByte)
130131
if nextByte <= 127 { // https://protobuf.dev/programming-guides/encoding/#varints
131132
// Continuation bit is not set, so Varint must've ended
132133
break
133134
}
134135
}
135-
value, errCode := protowire.ConsumeVarint(scratchBuf)
136+
value, errCode := protowire.ConsumeVarint(*scratchBuf)
136137
if errCode < 0 {
137138
return value, protowire.ParseError(errCode)
138139
}

0 commit comments

Comments
 (0)