Skip to content
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
8 changes: 8 additions & 0 deletions common/buf/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,14 @@ func (b *Buffer) Cap() int32 {
return int32(len(b.v))
}

// Available returns the available capacity of the buffer content.
func (b *Buffer) Available() int32 {
if b == nil {
return 0
}
return int32(len(b.v)) - b.end
}

// IsEmpty returns true if the buffer is empty.
func (b *Buffer) IsEmpty() bool {
return b.Len() == 0
Expand Down
2 changes: 1 addition & 1 deletion common/buf/multi_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func Compact(mb MultiBuffer) MultiBuffer {

for i := 1; i < len(mb); i++ {
curr := mb[i]
if last.Len()+curr.Len() > Size {
if curr.Len() > last.Available() {
mb2 = append(mb2, last)
last = curr
} else {
Expand Down
23 changes: 23 additions & 0 deletions common/buf/multi_buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,29 @@ func TestCompact(t *testing.T) {
}
}

func TestCompactWithConsumed(t *testing.T) {
// make a consumed buffer (a.Start != 0)
a := New()
for range 8192 {
common.Must2(a.WriteString("a"))
}
a.Read(make([]byte, 2))

b := New()
for range 2 {
common.Must2(b.WriteString("b"))
}

mb := MultiBuffer{a, b}
cmb := Compact(mb)
mbc := &MultiBufferContainer{mb}
mbc.Read(make([]byte, 8190))

if w := cmb.String(); w != "bb" {
t.Error("unexpected Compact result ", w)
}
}

func BenchmarkSplitBytes(b *testing.B) {
var mb MultiBuffer
raw := make([]byte, Size)
Expand Down
Loading