Skip to content

Commit 18995b0

Browse files
committed
Use block pools for write-through caches, reduce allocations in writeMsgRecord
Signed-off-by: Neil Twigg <neil@nats.io>
1 parent eb329f9 commit 18995b0

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

server/filestore.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -3785,6 +3785,10 @@ func (mb *msgBlock) setupWriteCache(buf []byte) {
37853785
}
37863786

37873787
// Setup simple cache.
3788+
if buf == nil {
3789+
bsz := int(mb.fs.fcfg.BlockSize)
3790+
buf = getMsgBlockBuf(bsz)
3791+
}
37883792
mb.cache = &cache{buf: buf}
37893793
// Make sure we set the proper cache offset if we have existing data.
37903794
var fi os.FileInfo
@@ -5714,23 +5718,23 @@ func (mb *msgBlock) writeMsgRecord(rl, seq uint64, subj string, mhdr, msg []byte
57145718
// With headers, high bit on total length will be set.
57155719
// total_len(4) sequence(8) timestamp(8) subj_len(2) subj hdr_len(4) hdr msg hash(8)
57165720

5717-
// First write header, etc.
57185721
var le = binary.LittleEndian
5719-
var hdr [msgHdrSize]byte
57205722

57215723
l := uint32(rl)
57225724
hasHeaders := len(mhdr) > 0
57235725
if hasHeaders {
57245726
l |= hbit
57255727
}
57265728

5729+
// Reserve space for the header on the underlying buffer.
5730+
mb.cache.buf = append(mb.cache.buf, make([]byte, msgHdrSize)...)
5731+
hdr := mb.cache.buf[len(mb.cache.buf)-msgHdrSize : len(mb.cache.buf)]
57275732
le.PutUint32(hdr[0:], l)
57285733
le.PutUint64(hdr[4:], seq)
57295734
le.PutUint64(hdr[12:], uint64(ts))
57305735
le.PutUint16(hdr[20:], uint16(len(subj)))
57315736

57325737
// Now write to underlying buffer.
5733-
mb.cache.buf = append(mb.cache.buf, hdr[:]...)
57345738
mb.cache.buf = append(mb.cache.buf, subj...)
57355739

57365740
if hasHeaders {
@@ -5744,13 +5748,12 @@ func (mb *msgBlock) writeMsgRecord(rl, seq uint64, subj string, mhdr, msg []byte
57445748
// Calculate hash.
57455749
mb.hh.Reset()
57465750
mb.hh.Write(hdr[4:20])
5747-
mb.hh.Write([]byte(subj))
5751+
mb.hh.Write(stringToBytes(subj))
57485752
if hasHeaders {
57495753
mb.hh.Write(mhdr)
57505754
}
57515755
mb.hh.Write(msg)
5752-
checksum := mb.hh.Sum(nil)
5753-
// Grab last checksum
5756+
checksum := mb.hh.Sum(mb.lchk[:0:highwayhash.Size64])
57545757
copy(mb.lchk[0:], checksum)
57555758

57565759
// Update write through cache.

0 commit comments

Comments
 (0)