-
-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* rough cut buf * edits * edits * correctness for byte copying * fix bugs * simplify * correct simplification * page sized spool buffer * fix build * comments * bump timeout * bump timeout * fix race * try separate sleep error * vitess bump * see if sleep error masks a different error * add sleeps back * more error check where it won't hide other errors * remove handler test race * revert back to racey with sleeps * zach comments * [ga-format-pr] Run ./format_repo.sh to fix formatting --------- Co-authored-by: max-hoffman <max-hoffman@users.noreply.github.com>
- Loading branch information
1 parent
e44b780
commit 999a371
Showing
16 changed files
with
209 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2024 Dolthub, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package sql | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
const defaultByteBuffCap = 4096 | ||
|
||
var ByteBufPool = sync.Pool{ | ||
New: func() any { | ||
return NewByteBuffer(defaultByteBuffCap) | ||
}, | ||
} | ||
|
||
type ByteBuffer struct { | ||
i int | ||
buf []byte | ||
} | ||
|
||
func NewByteBuffer(initCap int) *ByteBuffer { | ||
buf := make([]byte, initCap) | ||
return &ByteBuffer{buf: buf} | ||
} | ||
|
||
// Grow records the latest used byte position. Callers | ||
// are responsible for accurately reporting which bytes | ||
// they expect to be protected. | ||
func (b *ByteBuffer) Grow(n int) { | ||
newI := b.i | ||
if b.i+n <= len(b.buf) { | ||
// Increment |b.i| if no alloc | ||
newI += n | ||
} | ||
if b.i+n >= len(b.buf) { | ||
// No more space, double. | ||
// An external allocation doubled the cap using the size of | ||
// the override object, which if used could lead to overall | ||
// shrinking behavior. | ||
b.Double() | ||
} | ||
b.i = newI | ||
} | ||
|
||
// Double expands the backing array by 2x. We do this | ||
// here because the runtime only doubles based on slice | ||
// length. | ||
func (b *ByteBuffer) Double() { | ||
buf := make([]byte, len(b.buf)*2) | ||
copy(buf, b.buf) | ||
b.buf = buf | ||
} | ||
|
||
// Get returns a zero length slice beginning at a safe | ||
// write position. | ||
func (b *ByteBuffer) Get() []byte { | ||
return b.buf[b.i:b.i] | ||
} | ||
|
||
func (b *ByteBuffer) Reset() { | ||
b.i = 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2024 Dolthub, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package sql | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGrowByteBuffer(t *testing.T) { | ||
b := NewByteBuffer(10) | ||
|
||
// grow less than boundary | ||
src1 := []byte{1, 1, 1} | ||
obj1 := append(b.Get(), src1...) | ||
b.Grow(len(src1)) | ||
|
||
require.Equal(t, 10, len(b.buf)) | ||
require.Equal(t, 3, b.i) | ||
require.Equal(t, 10, cap(obj1)) | ||
|
||
// grow to boundary | ||
src2 := []byte{0, 0, 0, 0, 0, 0, 0} | ||
obj2 := append(b.Get(), src2...) | ||
b.Grow(len(src2)) | ||
|
||
require.Equal(t, 20, len(b.buf)) | ||
require.Equal(t, 10, b.i) | ||
require.Equal(t, 7, cap(obj2)) | ||
|
||
src3 := []byte{2, 2, 2, 2, 2} | ||
obj3 := append(b.Get(), src3...) | ||
b.Grow(len(src3)) | ||
|
||
require.Equal(t, 20, len(b.buf)) | ||
require.Equal(t, 15, b.i) | ||
require.Equal(t, 10, cap(obj3)) | ||
|
||
// grow exceeds boundary | ||
|
||
src4 := []byte{3, 3, 3, 3, 3, 3, 3, 3} | ||
obj4 := append(b.Get(), src4...) | ||
b.Grow(len(src4)) | ||
|
||
require.Equal(t, 40, len(b.buf)) | ||
require.Equal(t, 15, b.i) | ||
require.Equal(t, 16, cap(obj4)) | ||
|
||
// objects are all valid after doubling | ||
require.Equal(t, src1, obj1) | ||
require.Equal(t, src2, obj2) | ||
require.Equal(t, src3, obj3) | ||
require.Equal(t, src4, obj4) | ||
|
||
// reset | ||
b.Reset() | ||
require.Equal(t, 40, len(b.buf)) | ||
require.Equal(t, 0, b.i) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.