-
Notifications
You must be signed in to change notification settings - Fork 114
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
reuse allocated buffer #271
Changes from 1 commit
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 |
---|---|---|
|
@@ -50,7 +50,13 @@ func NewEncoder() *Encoder { | |
|
||
// Clean clean the Encoder (room) for a new object encoding. | ||
func (e *Encoder) Clean() { | ||
buffer := make([]byte, 64) | ||
var buffer []byte | ||
if len(e.buffer) <= 128 { | ||
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. does it need to check the buffer length? 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. i am sorry, i made a typo. if not check the buffer capacity. there is a risk of memory leak. for example, an encoder encodes an huge object (eg: 100K ), the allocated room of buffer is also so big. generally, most of object is not such huge. it causes most of buffer room is not used and not GC . it is
good idea |
||
// reuse buffer, avoid allocate | ||
buffer = e.buffer[:0] | ||
} else { | ||
buffer = make([]byte, 64) | ||
} | ||
e.classInfoList = nil | ||
e.buffer = buffer[:0] | ||
e.refMap = make(map[unsafe.Pointer]_refElem, 7) | ||
|
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.
@wongoo Making a slice of byte have an advantage: avoiding memory leak caused by growth of underlying array . but making it , when call method
Clean
every time, is unnecessary too. the underlying array can be reused.this PR, have two advantages: