Skip to content

fix(Buffer): uint32 -> uint64 in slice methods #323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions z/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,16 +253,16 @@ func (b *Buffer) AllocateOffset(n int) int {
}

func (b *Buffer) writeLen(sz int) {
buf := b.Allocate(4)
binary.BigEndian.PutUint32(buf, uint32(sz))
buf := b.Allocate(8)
binary.BigEndian.PutUint64(buf, uint64(sz))
}

// SliceAllocate would encode the size provided into the buffer, followed by a call to Allocate,
// hence returning the slice of size sz. This can be used to allocate a lot of small buffers into
// this big buffer.
// Note that SliceAllocate should NOT be mixed with normal calls to Write.
func (b *Buffer) SliceAllocate(sz int) []byte {
b.Grow(4 + sz)
b.Grow(8 + sz)
b.writeLen(sz)
return b.Allocate(sz)
}
Expand Down Expand Up @@ -394,7 +394,7 @@ func (s *sortHelper) merge(left, right []byte, start, end int) {
rs = rawSlice(right)

// We skip the first 4 bytes in the rawSlice, because that stores the length.
if s.less(ls[4:], rs[4:]) {
if s.less(ls[8:], rs[8:]) {
copyLeft()
} else {
copyRight()
Expand Down Expand Up @@ -467,8 +467,8 @@ func (b *Buffer) SortSliceBetween(start, end int, less LessFunc) {
}

func rawSlice(buf []byte) []byte {
sz := binary.BigEndian.Uint32(buf)
return buf[:4+int(sz)]
sz := binary.BigEndian.Uint64(buf)
return buf[:8+int(sz)]
}

// Slice would return the slice written at offset.
Expand All @@ -477,8 +477,8 @@ func (b *Buffer) Slice(offset int) ([]byte, int) {
return nil, -1
}

sz := binary.BigEndian.Uint32(b.buf[offset:])
start := offset + 4
sz := binary.BigEndian.Uint64(b.buf[offset:])
start := offset + 8
next := start + int(sz)
res := b.buf[start:next]
if next >= int(b.offset) {
Expand Down
6 changes: 3 additions & 3 deletions z/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ func TestBufferSort(t *testing.T) {
}

test := func(start, end int) {
start = buf.StartOffset() + 12*start
end = buf.StartOffset() + 12*end
start = buf.StartOffset() + 16*start
end = buf.StartOffset() + 16*end
buf.SortSliceBetween(start, end, func(ls, rs []byte) bool {
lhs := binary.BigEndian.Uint64(ls)
rhs := binary.BigEndian.Uint64(rs)
Expand All @@ -238,7 +238,7 @@ func TestBufferSort(t *testing.T) {
last = uid
count++
}
require.Equal(t, (end-start)/12, count)
require.Equal(t, (end-start)/16, count)
}
for i := 10; i <= N; i += 10 {
test(i-10, i)
Expand Down