Skip to content

Commit 210c5c7

Browse files
committed
zach comments
1 parent 3a7e950 commit 210c5c7

File tree

2 files changed

+96
-7
lines changed

2 files changed

+96
-7
lines changed

sql/byte_buffer.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
// Copyright 2024 Dolthub, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
package sql
216

317
import (
@@ -26,15 +40,19 @@ func NewByteBuffer(initCap int) *ByteBuffer {
2640
// are responsible for accurately reporting which bytes
2741
// they expect to be protected.
2842
func (b *ByteBuffer) Grow(n int) {
29-
if b.i+n > len(b.buf) {
30-
// Runtime alloc'd into a separate backing array, but it chooses
31-
// the doubling cap using the non-optimal |cap(b.buf)-b.i|*2.
32-
// We do not need to increment |b.i| b/c the latest value is in
33-
// the other array.
43+
newI := b.i
44+
if b.i+n <= len(b.buf) {
45+
// Increment |b.i| if no alloc
46+
newI += n
47+
}
48+
if b.i+n >= len(b.buf) {
49+
// No more space, double.
50+
// An external allocation doubled the cap using the size of
51+
// the override object, which if used could lead to overall
52+
// shrinking behavior.
3453
b.Double()
35-
} else {
36-
b.i += n
3754
}
55+
b.i = newI
3856
}
3957

4058
// Double expands the backing array by 2x. We do this

sql/byte_buffer_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2024 Dolthub, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package sql
16+
17+
import (
18+
"github.com/stretchr/testify/require"
19+
"testing"
20+
)
21+
22+
func TestGrowByteBuffer(t *testing.T) {
23+
b := NewByteBuffer(10)
24+
25+
// grow less than boundary
26+
src1 := []byte{1, 1, 1}
27+
obj1 := append(b.Get(), src1...)
28+
b.Grow(len(src1))
29+
30+
require.Equal(t, 10, len(b.buf))
31+
require.Equal(t, 3, b.i)
32+
require.Equal(t, 10, cap(obj1))
33+
34+
// grow to boundary
35+
src2 := []byte{0, 0, 0, 0, 0, 0, 0}
36+
obj2 := append(b.Get(), src2...)
37+
b.Grow(len(src2))
38+
39+
require.Equal(t, 20, len(b.buf))
40+
require.Equal(t, 10, b.i)
41+
require.Equal(t, 7, cap(obj2))
42+
43+
src3 := []byte{2, 2, 2, 2, 2}
44+
obj3 := append(b.Get(), src3...)
45+
b.Grow(len(src3))
46+
47+
require.Equal(t, 20, len(b.buf))
48+
require.Equal(t, 15, b.i)
49+
require.Equal(t, 10, cap(obj3))
50+
51+
// grow exceeds boundary
52+
53+
src4 := []byte{3, 3, 3, 3, 3, 3, 3, 3}
54+
obj4 := append(b.Get(), src4...)
55+
b.Grow(len(src4))
56+
57+
require.Equal(t, 40, len(b.buf))
58+
require.Equal(t, 15, b.i)
59+
require.Equal(t, 16, cap(obj4))
60+
61+
// objects are all valid after doubling
62+
require.Equal(t, src1, obj1)
63+
require.Equal(t, src2, obj2)
64+
require.Equal(t, src3, obj3)
65+
require.Equal(t, src4, obj4)
66+
67+
// reset
68+
b.Reset()
69+
require.Equal(t, 40, len(b.buf))
70+
require.Equal(t, 0, b.i)
71+
}

0 commit comments

Comments
 (0)