diff --git a/server/filestore.go b/server/filestore.go index 4a5e7a8861a..2ea85b8e9d7 100644 --- a/server/filestore.go +++ b/server/filestore.go @@ -7104,19 +7104,19 @@ func (mb *msgBlock) flushPendingMsgsLocked() (*LostStreamData, error) { if ts := ats.AccessTime(); ts < mb.llts || (ts-mb.llts) <= int64(mb.cexp) { mb.cache.wp += lob } else { - if cap(mb.cache.buf) <= maxBufReuse { - buf = mb.cache.buf[:0] - } else { - recycleMsgBlockBuf(mb.cache.buf) - buf = nil - } - if moreBytes > 0 { - nbuf := mb.cache.buf[len(mb.cache.buf)-moreBytes:] - if moreBytes > (len(mb.cache.buf)/4*3) && cap(nbuf) <= maxBufReuse { - buf = nbuf + if moreBytes == 0 { + if cap(mb.cache.buf) <= maxBufReuse { + // Reuse the entire underlying buffer + buf = mb.cache.buf[:0] } else { - buf = append(buf, nbuf...) + recycleMsgBlockBuf(mb.cache.buf) + buf = nil } + } else { + // Move the additional bytes to the beginning of the buffer and re-slice so + // we will have more usable capacity at the end again for additional writes. + copy(mb.cache.buf[:moreBytes], mb.cache.buf[len(mb.cache.buf)-moreBytes:]) + buf = mb.cache.buf[:moreBytes] } // Update our cache offset. mb.cache.off = int(woff) @@ -7262,7 +7262,7 @@ func (mb *msgBlock) loadMsgsWithLock() error { checkCache: nchecks++ if nchecks > 8 { - return errCorruptState + return errNoCache } // Check to see if we have a full cache.