InMessage is initialized with a storage allocated before reads (#110)

Fixes #109. In #102, the storage of the InMessage gets allocated every
time it's being read, which is expensive and caused issues like
https://github.com/GoogleCloudPlatform/gcsfuse/issues/563. So, this
change moves the allocation to its own function, called only once when
the struct is initialized before the reads.
zerocopy-examples
Oliver Le Zhuang 2021-10-28 14:41:51 -07:00 committed by GitHub
parent c75d3f26fc
commit 8da59ba998
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 2 deletions

View File

@ -31,7 +31,7 @@ func (c *Connection) getInMessage() *buffer.InMessage {
c.mu.Unlock()
if x == nil {
x = new(buffer.InMessage)
x = buffer.NewInMessage()
}
return x

View File

@ -44,11 +44,17 @@ type InMessage struct {
storage []byte
}
// NewInMessage creates a new InMessage with its storage initialized.
func NewInMessage() *InMessage {
return &InMessage{
storage: make([]byte, bufSize),
}
}
// Initialize with the data read by a single call to r.Read. The first call to
// Consume will consume the bytes directly after the fusekernel.InHeader
// struct.
func (m *InMessage) Init(r io.Reader) error {
m.storage = make([]byte, bufSize, bufSize)
n, err := r.Read(m.storage[:])
if err != nil {
return err