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

Optimization: Keep some requests between GC. #120

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 20 additions & 2 deletions fuse.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,9 +419,15 @@ var bufSize = maxRequestSize + maxWrite
//
// Messages in the pool are guaranteed to have conn and off zeroed,
// buf allocated and len==bufSize, and hdr set.
//
// Allocating buffer of bufSize is typically large enough to kick off GC,
// and GC flushes all pooled messages.
// To avoid too much GC kicking in, let 2 messages survive between GC.
var reqPool = sync.Pool{
New: allocMessage,
}
var minPooledReq = 2
var reqCh = make(chan *message, minPooledReq)

func allocMessage() interface{} {
m := &message{buf: make([]byte, bufSize)}
Expand All @@ -430,7 +436,13 @@ func allocMessage() interface{} {
}

func getMessage(c *Conn) *message {
m := reqPool.Get().(*message)
var m *message
select {
case m = <-reqCh:
break
default:
m = reqPool.Get().(*message)
}
m.conn = c
return m
}
Expand All @@ -439,7 +451,13 @@ func putMessage(m *message) {
m.buf = m.buf[:bufSize]
m.conn = nil
m.off = 0
reqPool.Put(m)

select {
case reqCh <- m:
break
default:
reqPool.Put(m)
}
}

// a message represents the bytes of a single FUSE message
Expand Down