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

Don't use string builder in subjString (it is slow) #3956

Merged
merged 1 commit into from Mar 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 7 additions & 9 deletions server/filestore.go
Expand Up @@ -32,7 +32,6 @@ import (
"os"
"path/filepath"
"sort"
"strings"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -4464,29 +4463,28 @@ func (mb *msgBlock) msgFromBuf(buf []byte, sm *StoreMsg, hh hash.Hash64) (*Store
// Given the `key` byte slice, this function will return the subject
// as a copy of `key` or a configured subject as to minimize memory allocations.
// Lock should be held.
func (mb *msgBlock) subjString(key []byte) string {
if len(key) == 0 {
func (mb *msgBlock) subjString(skey []byte) string {
if len(skey) == 0 {
return _EMPTY_
}
key := string(skey)

if lsubjs := len(mb.fs.cfg.Subjects); lsubjs > 0 {
if lsubjs == 1 {
// The cast for the comparison does not make a copy
if string(key) == mb.fs.cfg.Subjects[0] {
if key == mb.fs.cfg.Subjects[0] {
return mb.fs.cfg.Subjects[0]
}
} else {
for _, subj := range mb.fs.cfg.Subjects {
if string(key) == subj {
if key == subj {
return subj
}
}
}
}
// Copy here to not reference underlying buffer.
var sb strings.Builder
sb.Write(key)
return sb.String()

return key
}

// LoadMsg will lookup the message by sequence number and return it if found.
Expand Down