-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Refactoring of the memory pool (#323)
- Loading branch information
Showing
4 changed files
with
46 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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 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,2 @@ | ||
// Package mem implements the memory utility. | ||
package mem |
This file contains 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,31 @@ | ||
package mem | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
// Pool represents memory pool of T. | ||
type Pool[T any] struct { | ||
internal *sync.Pool | ||
} | ||
|
||
// NewPool returns a memory pool of T. | ||
func NewPool[T any](f func() *T) Pool[T] { | ||
return Pool[T]{ | ||
internal: &sync.Pool{ | ||
New: func() any { | ||
return f() | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
// Get gets instance of T from the memory pool. | ||
func (p Pool[T]) Get() *T { | ||
return p.internal.Get().(*T) | ||
} | ||
|
||
// Put puts the instance to the memory pool. | ||
func (p Pool[T]) Put(x *T) { | ||
p.internal.Put(x) | ||
} |
This file contains 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