Skip to content
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

Pool index buffer for read-write wal segments #523

Merged
merged 4 commits into from
Sep 23, 2024
Merged
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: 15 additions & 1 deletion server/wal/wal_rw_segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import (
"go.uber.org/multierr"
)

var bufferPool = sync.Pool{}

const initialIndexBufferCapacity = 16 * 1024

type ReadWriteSegment interface {
ReadOnlySegment

Expand Down Expand Up @@ -66,6 +70,13 @@ func newReadWriteSegment(basePath string, baseOffset int64, segmentSize uint32)
segmentSize: segmentSize,
}

if pooledBuffer, ok := bufferPool.Get().(*[]byte); ok {
ms.writingIdx = (*pooledBuffer)[:0]
} else {
// Start with empty slice, though with some initial capacity
ms.writingIdx = make([]byte, 0, initialIndexBufferCapacity)
}

txnPath := ms.path + txnExtension

var segmentExists bool
Expand Down Expand Up @@ -177,13 +188,16 @@ func (ms *readWriteSegment) Close() error {
ms.Lock()
defer ms.Unlock()

return multierr.Combine(
err := multierr.Combine(
ms.txnMappedFile.Unmap(),
ms.txnFile.Close(),

// Write index file
ms.writeIndex(),
)

bufferPool.Put(&ms.writingIdx)
return err
}

func (ms *readWriteSegment) Delete() error {
Expand Down
Loading