mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
runtime: let Pinner preallocate a reusable ref array
With this change a Pinner preallocates an array of 5 pointers for references to pinned objects. This reduces allocations when a pinner is reused with up to 5 pinned objects. This is a follow-up to CL 367296. Signed-off-by: Sven Anderson <sven@anderson.de> Change-Id: Ibea0b9ee4d7e39b0341a1da9d8276a4283e4956d Reviewed-on: https://go-review.googlesource.com/c/go/+/496275 Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> Auto-Submit: Michael Knyszek <mknyszek@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Michael Knyszek <mknyszek@google.com>
This commit is contained in:
parent
40bdc56180
commit
4bb38fe458
1 changed files with 11 additions and 3 deletions
|
|
@ -28,8 +28,9 @@ type Pinner struct {
|
|||
func (p *Pinner) Pin(pointer any) {
|
||||
if p.pinner == nil {
|
||||
p.pinner = new(pinner)
|
||||
p.refs = p.refStore[:0]
|
||||
SetFinalizer(p.pinner, func(i *pinner) {
|
||||
if i.refs != nil {
|
||||
if len(i.refs) != 0 {
|
||||
i.unpin() // only required to make the test idempotent
|
||||
pinnerLeakPanic()
|
||||
}
|
||||
|
|
@ -46,8 +47,14 @@ func (p *Pinner) Unpin() {
|
|||
p.pinner.unpin()
|
||||
}
|
||||
|
||||
const (
|
||||
pinnerSize = 64
|
||||
pinnerRefStoreSize = (pinnerSize - unsafe.Sizeof([]unsafe.Pointer{})) / unsafe.Sizeof(unsafe.Pointer(nil))
|
||||
)
|
||||
|
||||
type pinner struct {
|
||||
refs []unsafe.Pointer
|
||||
refs []unsafe.Pointer
|
||||
refStore [pinnerRefStoreSize]unsafe.Pointer
|
||||
}
|
||||
|
||||
func (p *pinner) unpin() {
|
||||
|
|
@ -58,7 +65,8 @@ func (p *pinner) unpin() {
|
|||
setPinned(p.refs[i], false)
|
||||
p.refs[i] = nil
|
||||
}
|
||||
p.refs = nil
|
||||
p.refStore = [pinnerRefStoreSize]unsafe.Pointer{}
|
||||
p.refs = p.refStore[:0]
|
||||
}
|
||||
|
||||
func pinnerGetPtr(i *any) unsafe.Pointer {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue