[go] math/big: only use pool for large allocations

7 views
Skip to first unread message

Keith Randall (Gerrit)

unread,
Jun 5, 2025, 9:19:15 PM6/5/25
to Russ Cox, goph...@pubsubhelper.golang.org, Keith Randall, golang-co...@googlegroups.com
Attention needed from Russ Cox

Keith Randall has uploaded the change for review

Keith Randall would like Russ Cox to review this change.

Commit message

math/big: only use pool for large allocations

The native allocator seems faster for small things.
Only start accessing the pool if we need something large.
Currently "large" is more than 4 words. It seems a reasonable
threshold, although I didn't do much experimentation to pick
a number.

Fixes #73999

1.24.2 to tip:

goos: darwin
goarch: arm64
pkg: github.com/dustin/go-humanize
cpu: Apple M2 Ultra
│ base │ pre │
│ sec/op │ sec/op vs base │
ParseBigBytes-24 625.0n ± 1% 665.8n ± 0% +6.53% (p=0.000 n=10)

1.24.2 to tip+this CL:

goos: darwin
goarch: arm64
pkg: github.com/dustin/go-humanize
cpu: Apple M2 Ultra
│ base │ post │
│ sec/op │ sec/op vs base │
ParseBigBytes-24 625.0n ± 1% 626.8n ± 0% ~ (p=0.470 n=10)
Change-Id: Ic071e6d82d4aa4a0d3a6ec6e026f513c83cb0b37

Change diff

diff --git a/src/math/big/nat.go b/src/math/big/nat.go
index 43e36d3..ca966ca 100644
--- a/src/math/big/nat.go
+++ b/src/math/big/nat.go
@@ -274,24 +274,24 @@
// so that they can handle trivial stack-free cases without forcing the
// caller to obtain and free a stack that will be unused. These functions
// document that they accept a nil *stack in their doc comments.
-type stack struct {
+type stackInner struct {
w []Word
}

-var stackPool sync.Pool
+var stackPool sync.Pool // pool of *stackInner

// getStack returns a temporary stack.
// The caller must call [stack.free] to give up use of the stack when finished.
-func getStack() *stack {
- s, _ := stackPool.Get().(*stack)
+func getStackInner() *stackInner {
+ s, _ := stackPool.Get().(*stackInner)
if s == nil {
- s = new(stack)
+ s = new(stackInner)
}
return s
}

// free returns the stack for use by another calculation.
-func (s *stack) free() {
+func (s *stackInner) free() {
s.w = s.w[:0]
stackPool.Put(s)
}
@@ -299,7 +299,7 @@
// save returns the current stack pointer.
// A future call to restore with the same value
// frees any temporaries allocated on the stack after the call to save.
-func (s *stack) save() int {
+func (s *stackInner) save() int {
return len(s.w)
}

@@ -310,12 +310,12 @@
//
// which makes sure to pop any temporaries allocated in the current function
// from the stack before returning.
-func (s *stack) restore(n int) {
+func (s *stackInner) restore(n int) {
s.w = s.w[:n]
}

// nat returns a nat of n words, allocated on the stack.
-func (s *stack) nat(n int) nat {
+func (s *stackInner) nat(n int) nat {
nr := (n + 3) &^ 3 // round up to multiple of 4
off := len(s.w)
s.w = slices.Grow(s.w, nr)
@@ -327,6 +327,56 @@
return x
}

+type stack struct {
+ si *stackInner
+}
+
+func getStack() *stack {
+ return &stack{}
+}
+func (s *stack) free() {
+ si := s.si
+ if si != nil {
+ si.free()
+ }
+}
+func (s *stack) save() int {
+ si := s.si
+ if si == nil {
+ return 0
+ }
+ return si.save()
+}
+func (s *stack) restore(n int) {
+ si := s.si
+ if si == nil {
+ return
+ }
+ si.restore(n)
+}
+func (s *stack) nat(n int) nat {
+ si := s.si
+ if si == nil {
+ if n <= 4 {
+ // For small allocations, just ask the allocator.
+ // It isn't worth pooling these allocations.
+ // See issue 73999.
+ r := slices.Grow(nat(nil), n)
+ r = r[:n]
+ if n > 0 {
+ r[0] = 0xabcdef
+ }
+ return r
+ }
+ si, _ = stackPool.Get().(*stackInner)
+ if si == nil {
+ si = new(stackInner)
+ }
+ s.si = si
+ }
+ return si.nat(n)
+}
+
// bitLen returns the length of x in bits.
// Unlike most methods, it works even if x is not normalized.
func (x nat) bitLen() int {

Change information

Files:
  • M src/math/big/nat.go
Change size: M
Delta: 1 file changed, 59 insertions(+), 9 deletions(-)
Open in Gerrit

Related details

Attention is currently required from:
  • Russ Cox
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ic071e6d82d4aa4a0d3a6ec6e026f513c83cb0b37
Gerrit-Change-Number: 679475
Gerrit-PatchSet: 1
Gerrit-Owner: Keith Randall <k...@golang.org>
Gerrit-Reviewer: Keith Randall <k...@golang.org>
Gerrit-Reviewer: Russ Cox <r...@golang.org>
Gerrit-Attention: Russ Cox <r...@golang.org>
unsatisfied_requirement
satisfied_requirement
open
diffy

Keith Randall (Gerrit)

unread,
Apr 27, 2026, 3:48:30 PMApr 27
to Keith Randall, goph...@pubsubhelper.golang.org, Michael Pratt, golang...@luci-project-accounts.iam.gserviceaccount.com, Russ Cox, golang-co...@googlegroups.com
Attention needed from Michael Pratt and Russ Cox

Keith Randall voted Commit-Queue+1

Commit-Queue+1
Open in Gerrit

Related details

Attention is currently required from:
  • Michael Pratt
  • Russ Cox
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ic071e6d82d4aa4a0d3a6ec6e026f513c83cb0b37
Gerrit-Change-Number: 679475
Gerrit-PatchSet: 2
Gerrit-Owner: Keith Randall <k...@golang.org>
Gerrit-Reviewer: Keith Randall <k...@golang.org>
Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
Gerrit-Reviewer: Russ Cox <r...@golang.org>
Gerrit-Attention: Russ Cox <r...@golang.org>
Gerrit-Attention: Michael Pratt <mpr...@google.com>
Gerrit-Comment-Date: Mon, 27 Apr 2026 19:48:26 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Keith Randall (Gerrit)

unread,
Apr 27, 2026, 4:07:02 PMApr 27
to Keith Randall, goph...@pubsubhelper.golang.org, golang...@luci-project-accounts.iam.gserviceaccount.com, Michael Pratt, Russ Cox, golang-co...@googlegroups.com
Gerrit-Comment-Date: Mon, 27 Apr 2026 20:06:55 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Keith Randall (Gerrit)

unread,
Apr 27, 2026, 4:39:45 PMApr 27
to Keith Randall, goph...@pubsubhelper.golang.org, golang...@luci-project-accounts.iam.gserviceaccount.com, Michael Pratt, Russ Cox, golang-co...@googlegroups.com
Gerrit-Comment-Date: Mon, 27 Apr 2026 20:39:41 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Keith Randall (Gerrit)

unread,
May 18, 2026, 4:17:19 PM (16 hours ago) May 18
to Keith Randall, goph...@pubsubhelper.golang.org, golang...@luci-project-accounts.iam.gserviceaccount.com, Michael Pratt, Russ Cox, golang-co...@googlegroups.com
Attention needed from Michael Pratt and Russ Cox

Keith Randall added 1 comment

Patchset-level comments
File-level comment, Patchset 2 (Latest):
Keith Randall . resolved

Ping, like to get this in.

Open in Gerrit

Related details

Attention is currently required from:
  • Michael Pratt
  • Russ Cox
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ic071e6d82d4aa4a0d3a6ec6e026f513c83cb0b37
    Gerrit-Change-Number: 679475
    Gerrit-PatchSet: 2
    Gerrit-Owner: Keith Randall <k...@golang.org>
    Gerrit-Reviewer: Keith Randall <k...@golang.org>
    Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
    Gerrit-Reviewer: Russ Cox <r...@golang.org>
    Gerrit-Attention: Russ Cox <r...@golang.org>
    Gerrit-Attention: Michael Pratt <mpr...@google.com>
    Gerrit-Comment-Date: Mon, 18 May 2026 20:17:15 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    satisfied_requirement
    open
    diffy

    Keith Randall (Gerrit)

    unread,
    May 18, 2026, 4:17:37 PM (16 hours ago) May 18
    to Keith Randall, goph...@pubsubhelper.golang.org, golang...@luci-project-accounts.iam.gserviceaccount.com, Michael Pratt, Russ Cox, golang-co...@googlegroups.com
    Attention needed from Michael Pratt and Russ Cox

    Keith Randall voted Commit-Queue+1

    Commit-Queue+1
    Open in Gerrit

    Related details

    Attention is currently required from:
    • Michael Pratt
    • Russ Cox
    Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: Ic071e6d82d4aa4a0d3a6ec6e026f513c83cb0b37
      Gerrit-Change-Number: 679475
      Gerrit-PatchSet: 3
      Gerrit-Owner: Keith Randall <k...@golang.org>
      Gerrit-Reviewer: Keith Randall <k...@golang.org>
      Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
      Gerrit-Reviewer: Russ Cox <r...@golang.org>
      Gerrit-Attention: Russ Cox <r...@golang.org>
      Gerrit-Attention: Michael Pratt <mpr...@google.com>
      Gerrit-Comment-Date: Mon, 18 May 2026 20:17:32 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Cuong Manh Le (Gerrit)

      unread,
      1:06 AM (7 hours ago) 1:06 AM
      to Keith Randall, goph...@pubsubhelper.golang.org, golang...@luci-project-accounts.iam.gserviceaccount.com, Michael Pratt, Russ Cox, golang-co...@googlegroups.com
      Attention needed from Keith Randall, Michael Pratt and Russ Cox

      Cuong Manh Le voted and added 1 comment

      Votes added by Cuong Manh Le

      Code-Review+2

      1 comment

      File src/math/big/nat.go
      Line 268, Patchset 3 (Latest):// A stack provides temporary storage for complex calculations
      Cuong Manh Le . unresolved

      This comment needs updating.

      Open in Gerrit

      Related details

      Attention is currently required from:
      • Keith Randall
      • Michael Pratt
      • Russ Cox
      Submit Requirements:
      • requirement satisfiedCode-Review
      • requirement is not satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: Ic071e6d82d4aa4a0d3a6ec6e026f513c83cb0b37
      Gerrit-Change-Number: 679475
      Gerrit-PatchSet: 3
      Gerrit-Owner: Keith Randall <k...@golang.org>
      Gerrit-Reviewer: Cuong Manh Le <cuong.m...@gmail.com>
      Gerrit-Reviewer: Keith Randall <k...@golang.org>
      Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
      Gerrit-Reviewer: Russ Cox <r...@golang.org>
      Gerrit-Attention: Keith Randall <k...@golang.org>
      Gerrit-Attention: Russ Cox <r...@golang.org>
      Gerrit-Attention: Michael Pratt <mpr...@google.com>
      Gerrit-Comment-Date: Tue, 19 May 2026 05:06:16 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: Yes
      satisfied_requirement
      unsatisfied_requirement
      open
      diffy
      Reply all
      Reply to author
      Forward
      0 new messages