-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
store: fix marshaling with sync.Pool #4593
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 |
---|---|---|
|
@@ -497,32 +497,45 @@ func (m *SeriesResponse) Marshal() (dAtA []byte, err error) { | |
|
||
var respBuf []byte | ||
|
||
// No pool defined, allocate directly. | ||
// Slow path with no sync.Pool. | ||
if m.respPool == nil { | ||
respBuf = make([]byte, size) | ||
} else { | ||
if m.respBuf == nil { | ||
poolBuf := m.respPool.Get() | ||
if poolBuf == nil { | ||
respBuf = make([]byte, size) | ||
m.respBuf = &respBuf | ||
} else { | ||
m.respBuf = poolBuf.(*[]byte) | ||
respBuf = *m.respBuf | ||
} | ||
m.respBuf = nil | ||
|
||
n, err := m.MarshalToSizedBuffer(respBuf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return respBuf[len(respBuf)-n:], nil | ||
} | ||
|
||
// Fast path with sync.Pool. | ||
// m.respBuf must not be nil so that it would be returned to the pool. | ||
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 non nil if we do the work below? Can we always do that and remove any outside call pool Get? 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'm not sure I understood this comment. The callers using the old function pass 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 know, i thought we can simplify, and allow this method to |
||
|
||
// If no pre-allocated buffer has been passed then try to get a new one. | ||
if m.respBuf == nil { | ||
poolBuf := m.respPool.Get() | ||
// No previous buffer found in the pool, try to allocate. | ||
if poolBuf == nil { | ||
respBuf = make([]byte, size) | ||
} else { | ||
if cap(*m.respBuf) < size { | ||
if m.respPool != nil { | ||
m.respPool.Put(m.respBuf) | ||
} | ||
respBuf = make([]byte, size) | ||
m.respBuf = &respBuf | ||
} else { | ||
respBuf = *m.respBuf | ||
} | ||
// Found something, let's see if it is big enough. | ||
respBuf = *(poolBuf.(*[]byte)) | ||
} | ||
} else { | ||
respBuf = *m.respBuf | ||
} | ||
|
||
// Last sanity check of the size before the marshaling. | ||
if cap(respBuf) < size { | ||
if m.respPool != nil { | ||
m.respPool.Put(&respBuf) | ||
} | ||
respBuf = make([]byte, size) | ||
} | ||
m.respBuf = &respBuf | ||
|
||
// Possibly trim it so that there wouldn't be left-over "garbage" in the slice. | ||
GiedriusS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
marshalBuf := respBuf[:size] | ||
n, err := m.MarshalToSizedBuffer(marshalBuf) | ||
if err != 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.
Is this line necessary?
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.
Yea, I don't think so