-
Notifications
You must be signed in to change notification settings - Fork 24
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
fix: reduce memory held by deferred objects #96
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,15 @@ import ( | |
"errors" | ||
"fmt" | ||
"io" | ||
"sync" | ||
) | ||
|
||
var deferredBufferPool = sync.Pool{ | ||
New: func() any { | ||
return bytes.NewBuffer(nil) | ||
}, | ||
} | ||
|
||
type Deferred struct { | ||
Raw []byte | ||
} | ||
|
@@ -24,10 +31,12 @@ func (d *Deferred) MarshalCBOR(w io.Writer) error { | |
} | ||
|
||
func (d *Deferred) UnmarshalCBOR(br io.Reader) (err error) { | ||
// Reuse any existing buffers. | ||
reusedBuf := d.Raw[:0] | ||
d.Raw = nil | ||
buf := bytes.NewBuffer(reusedBuf) | ||
buf := deferredBufferPool.Get().(*bytes.Buffer) | ||
|
||
defer func() { | ||
buf.Reset() | ||
deferredBufferPool.Put(buf) | ||
}() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
But doing that needs a bit more refactoring which seems to be beyond the scope of this PR. Please feel free to address this in a separate PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
// Allocate some scratch space. | ||
scratch := make([]byte, maxHeaderSize) | ||
|
@@ -90,6 +99,9 @@ func (d *Deferred) UnmarshalCBOR(br io.Reader) (err error) { | |
return fmt.Errorf("unhandled deferred cbor type: %d", maj) | ||
} | ||
} | ||
d.Raw = buf.Bytes() | ||
// Reuse existing buffer. Also, copy to "give back" the allocation in the byte buffer (which | ||
// is likely significant). | ||
d.Raw = d.Raw[:0] | ||
d.Raw = append(d.Raw, buf.Bytes()...) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checking for understanding: In the old code the assignment of Is this correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, no. I'm keeping any overallocation from Note: in most cases, The issue with |
||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checking for understanding:
In the old code
reusedBuf
was overallocated compared to the data ind.Raw
, i.e. capacity was greater than length. When we assignreusedBuf
to the slice ofRaw
the over capacity is carried along with it. Then when we passreusedBuf
tobytes.NewBuffer
it further overallocates more capacity than is needed byreusedBuf
, amplifying the allocation further.Is this correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly the second part. In the case we care about,
reusedBuf
was always empty. Butbytes.Buffer
generally over-allocates for better performance.