-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
⚡ perf: Improve usage of sync.Pool #3767
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
5b2c796
aafe502
30c5795
42e60b4
98f436a
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 |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ package binder | |
|
|
||
| import ( | ||
| "errors" | ||
| "mime/multipart" | ||
| "sync" | ||
| ) | ||
|
|
||
|
|
@@ -71,6 +72,28 @@ var MsgPackBinderPool = sync.Pool{ | |
| }, | ||
| } | ||
|
|
||
| const ( | ||
| stringSliceMapDefaultCap = 8 | ||
| stringSliceMapMaxEntries = 128 | ||
| ) | ||
|
|
||
| var stringSliceMapPool = sync.Pool{ | ||
| New: func() any { | ||
| return make(map[string][]string, stringSliceMapDefaultCap) | ||
| }, | ||
| } | ||
|
|
||
| const ( | ||
| fileHeaderSliceMapDefaultCap = 4 | ||
| fileHeaderSliceMapMaxEntries = 64 | ||
| ) | ||
|
|
||
| var fileHeaderSliceMapPool = sync.Pool{ | ||
| New: func() any { | ||
| return make(map[string][]*multipart.FileHeader, fileHeaderSliceMapDefaultCap) | ||
| }, | ||
| } | ||
|
|
||
| // GetFromThePool retrieves a binder from the provided sync.Pool and panics if | ||
| // the stored value cannot be cast to the requested type. | ||
| func GetFromThePool[T any](pool *sync.Pool) T { | ||
|
|
@@ -86,3 +109,59 @@ func GetFromThePool[T any](pool *sync.Pool) T { | |
| func PutToThePool[T any](pool *sync.Pool, binder T) { | ||
| pool.Put(binder) | ||
| } | ||
|
|
||
| func acquireStringSliceMap() map[string][]string { | ||
| m, ok := stringSliceMapPool.Get().(map[string][]string) | ||
| if !ok { | ||
| panic(errors.New("failed to type-assert to map[string][]string")) | ||
| } | ||
| if m == nil { | ||
| return make(map[string][]string, stringSliceMapDefaultCap) | ||
| } | ||
| if len(m) > 0 { | ||
| clear(m) | ||
| } | ||
| return m | ||
| } | ||
|
|
||
| func releaseStringSliceMap(m map[string][]string) { | ||
| if m == nil { | ||
| return | ||
| } | ||
| used := len(m) | ||
| if used > 0 { | ||
| clear(m) | ||
| } | ||
|
Comment on lines
+132
to
+134
Contributor
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. The map is cleared here before being put back into the pool, but it's also cleared in |
||
| if used > stringSliceMapMaxEntries { | ||
| return | ||
| } | ||
| stringSliceMapPool.Put(m) | ||
| } | ||
|
|
||
| func acquireFileHeaderSliceMap() map[string][]*multipart.FileHeader { | ||
| m, ok := fileHeaderSliceMapPool.Get().(map[string][]*multipart.FileHeader) | ||
| if !ok { | ||
| panic(errors.New("failed to type-assert to map[string][]*multipart.FileHeader")) | ||
| } | ||
| if m == nil { | ||
| return make(map[string][]*multipart.FileHeader, fileHeaderSliceMapDefaultCap) | ||
| } | ||
|
Comment on lines
+146
to
+148
Contributor
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. |
||
| if len(m) > 0 { | ||
| clear(m) | ||
| } | ||
| return m | ||
| } | ||
|
|
||
| func releaseFileHeaderSliceMap(m map[string][]*multipart.FileHeader) { | ||
| if m == nil { | ||
| return | ||
| } | ||
| used := len(m) | ||
| if used > 0 { | ||
| clear(m) | ||
| } | ||
|
Comment on lines
+160
to
+162
Contributor
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. |
||
| if used > fileHeaderSliceMapMaxEntries { | ||
| return | ||
| } | ||
| fileHeaderSliceMapPool.Put(m) | ||
| } | ||
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.
This
if m == nilcheck appears to be redundant. ThestringSliceMapPoolis initialized with aNewfunction that always returns a non-nilmap[string][]string. Therefore,stringSliceMapPool.Get()will not returnnil, and this code block is unreachable.