Skip to content

Commit

Permalink
separation of dynamic and static buffer pools
Browse files Browse the repository at this point in the history
  • Loading branch information
lxzan committed Sep 1, 2023
1 parent cba6bef commit dcb010b
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
8 changes: 4 additions & 4 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func BenchmarkConn_ReadMessage(b *testing.B) {
handler: upgrader.eventHandler,
}
for i := 0; i < b.N; i++ {
internal.ResetBuffer(reader, buf.Bytes())
internal.BufferReset(reader, buf.Bytes())
conn2.br.Reset(reader)
_ = conn2.readMessage()
}
Expand Down Expand Up @@ -108,7 +108,7 @@ func BenchmarkConn_ReadMessage(b *testing.B) {
decompressor: config.decompressors.Select(),
}
for i := 0; i < b.N; i++ {
internal.ResetBuffer(reader, buf.Bytes())
internal.BufferReset(reader, buf.Bytes())
conn2.br.Reset(reader)
_ = conn2.readMessage()
}
Expand Down Expand Up @@ -150,7 +150,7 @@ func BenchmarkStdDeCompress(b *testing.B) {
fr := flate.NewReader(nil)
src := bytes.NewBuffer(nil)
for i := 0; i < b.N; i++ {
internal.ResetBuffer(src, buffer.Bytes())
internal.BufferReset(src, buffer.Bytes())
_, _ = src.Write(internal.FlateTail)
resetter := fr.(flate.Resetter)
_ = resetter.Reset(src, nil)
Expand All @@ -168,7 +168,7 @@ func BenchmarkKlauspostDeCompress(b *testing.B) {
fr := klauspost.NewReader(nil)
src := bytes.NewBuffer(nil)
for i := 0; i < b.N; i++ {
internal.ResetBuffer(src, buffer.Bytes())
internal.BufferReset(src, buffer.Bytes())
_, _ = src.Write(internal.FlateTail)
resetter := fr.(klauspost.Resetter)
_ = resetter.Reset(src, nil)
Expand Down
4 changes: 3 additions & 1 deletion internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ type Buffer struct {
}

//go:nosplit
func ResetBuffer(b *bytes.Buffer, p []byte) {
func BufferReset(b *bytes.Buffer, p []byte) {
buffer := (*Buffer)(unsafe.Pointer(b))
buffer.buf = p
buffer.off = 0
buffer.lastRead = 0
}
2 changes: 1 addition & 1 deletion internal/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,6 @@ func TestResetBuffer(t *testing.T) {
var buf = bytes.NewBufferString("hello")
var p = buf.Bytes()
p = append(p, "world"...)
ResetBuffer(buf, p)
BufferReset(buf, p)
assert.Equal(t, "helloworld", buf.String())
}
2 changes: 1 addition & 1 deletion reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (c *Conn) readMessage() error {
}

if fin && opcode != OpcodeContinuation {
internal.ResetBuffer(buf, p)
internal.BufferReset(buf, p)
return c.emitMessage(&Message{index: index, Opcode: opcode, Data: buf, compressed: compressed})
}

Expand Down

0 comments on commit dcb010b

Please sign in to comment.