From 8da59ba9980710fe352c673ae0ad62f3c6d21067 Mon Sep 17 00:00:00 2001 From: Oliver Le Zhuang Date: Thu, 28 Oct 2021 14:41:51 -0700 Subject: [PATCH] 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. --- freelists.go | 2 +- internal/buffer/in_message.go | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/freelists.go b/freelists.go index 73358b8..8489e1f 100644 --- a/freelists.go +++ b/freelists.go @@ -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 diff --git a/internal/buffer/in_message.go b/internal/buffer/in_message.go index cfa1d31..64df2c6 100644 --- a/internal/buffer/in_message.go +++ b/internal/buffer/in_message.go @@ -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