forked from vmihailenco/msgpack
-
Notifications
You must be signed in to change notification settings - Fork 0
perf: configurable pool buffer retention limit via SetPoolBufferLimit #74
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
Open
xe-nvdk
wants to merge
2
commits into
v6
Choose a base branch
from
perf/pool-buffer-limit
base: v6
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package msgpack | ||
|
|
||
| import "sync/atomic" | ||
|
|
||
| // defaultPoolBufferLimit is the default maximum capacity, in bytes, of | ||
| // internal buffers retained by pooled encoders and decoders. | ||
| const defaultPoolBufferLimit = 32 * 1024 | ||
|
|
||
| // poolBufferLimit holds the configured limit. Zero means "use the default"; | ||
| // it is read atomically so SetPoolBufferLimit is safe to call concurrently | ||
| // with PutEncoder/PutDecoder. | ||
| var poolBufferLimit atomic.Int64 | ||
|
|
||
| // SetPoolBufferLimit sets the maximum capacity, in bytes, of internal | ||
| // buffers retained by pooled encoders and decoders (GetEncoder/PutEncoder, | ||
| // GetDecoder/PutDecoder, and the package-level Marshal/MarshalAppend/ | ||
| // Unmarshal helpers that use them). Buffers whose capacity exceeds the | ||
| // limit are dropped when the encoder or decoder is returned to the pool, | ||
| // so one-off large operations don't pin memory. | ||
| // | ||
| // The default is 32 KiB. Workloads that consistently encode or decode | ||
| // larger payloads can raise the limit to avoid re-growing buffers on every | ||
| // pooled use. Values below the default are clamped to the default; | ||
| // n <= 0 restores the default. | ||
| func SetPoolBufferLimit(n int) { | ||
| if n <= 0 { | ||
| poolBufferLimit.Store(0) | ||
| return | ||
| } | ||
| if n < defaultPoolBufferLimit { | ||
| n = defaultPoolBufferLimit | ||
| } | ||
| poolBufferLimit.Store(int64(n)) | ||
| } | ||
|
|
||
| func getPoolBufferLimit() int { | ||
| if n := poolBufferLimit.Load(); n > 0 { | ||
| return int(n) | ||
| } | ||
| return defaultPoolBufferLimit | ||
| } | ||
|
|
||
| // poolBufOversized reports whether a pooled buffer of capacity c should be | ||
| // dropped rather than retained. The constant default check comes first so | ||
| // the common small-buffer case never pays for the atomic load; the | ||
| // configured limit is never below the default. | ||
| func poolBufOversized(c int) bool { | ||
| return c > defaultPoolBufferLimit && c > getPoolBufferLimit() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package msgpack | ||
|
|
||
| import "testing" | ||
|
|
||
| // These tests live in the msgpack package (not msgpack_test) because they | ||
| // inspect the unexported wbuf/buf fields to verify pool retention behavior. | ||
| // They rely on sync.Pool returning the just-Put item on the same goroutine, | ||
| // which holds absent a GC between Put and Get. | ||
|
|
||
| func TestPoolBufferLimitEncoder(t *testing.T) { | ||
| const big = 100 * 1024 | ||
|
|
||
| // Default limit: an oversized wbuf is dropped on Put. | ||
| enc := GetEncoder() | ||
| enc.wbuf = make([]byte, big) | ||
| PutEncoder(enc) | ||
| enc = GetEncoder() | ||
| if cap(enc.wbuf) > defaultPoolBufferLimit { | ||
| t.Fatalf("wbuf cap=%d retained above default limit", cap(enc.wbuf)) | ||
| } | ||
| PutEncoder(enc) | ||
|
|
||
| // Raised limit: the same buffer is retained. | ||
| SetPoolBufferLimit(256 * 1024) | ||
| defer SetPoolBufferLimit(0) | ||
|
|
||
| enc = GetEncoder() | ||
| enc.wbuf = make([]byte, big) | ||
| PutEncoder(enc) | ||
| enc = GetEncoder() | ||
| if cap(enc.wbuf) < big { | ||
| t.Fatalf("wbuf cap=%d, want >= %d retained under raised limit", cap(enc.wbuf), big) | ||
| } | ||
| PutEncoder(enc) | ||
| } | ||
|
|
||
| func TestPoolBufferLimitDecoder(t *testing.T) { | ||
| const big = 100 * 1024 | ||
|
|
||
| dec := GetDecoder() | ||
| dec.buf = make([]byte, big) | ||
| PutDecoder(dec) | ||
| dec = GetDecoder() | ||
| if cap(dec.buf) > defaultPoolBufferLimit { | ||
| t.Fatalf("buf cap=%d retained above default limit", cap(dec.buf)) | ||
| } | ||
| PutDecoder(dec) | ||
|
|
||
| SetPoolBufferLimit(256 * 1024) | ||
| defer SetPoolBufferLimit(0) | ||
|
|
||
| dec = GetDecoder() | ||
| dec.buf = make([]byte, big) | ||
| PutDecoder(dec) | ||
| dec = GetDecoder() | ||
| if cap(dec.buf) < big { | ||
| t.Fatalf("buf cap=%d, want >= %d retained under raised limit", cap(dec.buf), big) | ||
| } | ||
| PutDecoder(dec) | ||
| } | ||
|
|
||
| func TestSetPoolBufferLimitClamp(t *testing.T) { | ||
| SetPoolBufferLimit(-1) | ||
| if got := getPoolBufferLimit(); got != defaultPoolBufferLimit { | ||
| t.Fatalf("limit=%d after SetPoolBufferLimit(-1), want default %d", got, defaultPoolBufferLimit) | ||
| } | ||
| SetPoolBufferLimit(64 * 1024) | ||
| if got := getPoolBufferLimit(); got != 64*1024 { | ||
| t.Fatalf("limit=%d, want %d", got, 64*1024) | ||
| } | ||
| // Values below the default are clamped up to the default. | ||
| SetPoolBufferLimit(1024) | ||
| if got := getPoolBufferLimit(); got != defaultPoolBufferLimit { | ||
| t.Fatalf("limit=%d after SetPoolBufferLimit(1024), want clamped default %d", got, defaultPoolBufferLimit) | ||
| } | ||
| SetPoolBufferLimit(0) | ||
| if got := getPoolBufferLimit(); got != defaultPoolBufferLimit { | ||
| t.Fatalf("limit=%d after reset, want default %d", got, defaultPoolBufferLimit) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
These tests rely on
sync.Poolreturning the exact same encoder/decoder instance that was just put back. However,sync.Poolbehavior is non-deterministic: the pool can be cleared at any time by the garbage collector (GC), or it may return a new instance if the goroutine is rescheduled onto a different P or thread. When this happens,GetEncoder()orGetDecoder()will return a newly allocated instance with anilbuffer, causing the test to fail with a false positive (e.g.,wbuf cap=0, want >= 102400 retained under raised limit).To make the tests 100% deterministic and robust, we should test the underlying
poolBufOversizedfunction directly with various capacities and configured limits, rather than relying onsync.Poolside effects.