From: Sven Anderson Date: Thu, 18 May 2023 19:45:28 +0000 (+0200) Subject: runtime: let Pinner preallocate a reusable ref array X-Git-Tag: go1.21rc1~356 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=4bb38fe458cab82711b9a3c96c57a9ea106b044a;p=gostls13.git 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 Change-Id: Ibea0b9ee4d7e39b0341a1da9d8276a4283e4956d Reviewed-on: https://go-review.googlesource.com/c/go/+/496275 Reviewed-by: David Chase Reviewed-by: Michael Knyszek Auto-Submit: Michael Knyszek TryBot-Result: Gopher Robot Run-TryBot: Michael Knyszek --- diff --git a/src/runtime/pinner.go b/src/runtime/pinner.go index a507a5a3cc..94c9e92432 100644 --- a/src/runtime/pinner.go +++ b/src/runtime/pinner.go @@ -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 {