[go] runtime: make nelems a constant in sizespecializedmalloc

3 views
Skip to first unread message

Michael Matloob (Gerrit)

unread,
May 17, 2026, 2:39:59 PM (2 days ago) May 17
to goph...@pubsubhelper.golang.org, Michael Pratt, golang...@luci-project-accounts.iam.gserviceaccount.com, golang-co...@googlegroups.com
Attention needed from Michael Pratt

New activity on the change

Open in Gerrit

Related details

Attention is currently required from:
  • Michael Pratt
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: I9071a64fad79dc80118aaf81cb83c59e6a6a6964
Gerrit-Change-Number: 776020
Gerrit-PatchSet: 18
Gerrit-Owner: Michael Matloob <mat...@golang.org>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
Gerrit-Attention: Michael Pratt <mpr...@google.com>
Gerrit-Comment-Date: Sun, 17 May 2026 18:39:54 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: No
unsatisfied_requirement
satisfied_requirement
open
diffy

Michael Matloob (Gerrit)

unread,
May 17, 2026, 9:29:07 PM (2 days ago) May 17
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
Attention needed from Michael Matloob and Michael Pratt

Michael Matloob uploaded new patchset

Michael Matloob uploaded patch set #20 to this change.
Open in Gerrit

Related details

Attention is currently required from:
  • Michael Matloob
  • Michael Pratt
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: newpatchset
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I9071a64fad79dc80118aaf81cb83c59e6a6a6964
    Gerrit-Change-Number: 776020
    Gerrit-PatchSet: 20
    Gerrit-Owner: Michael Matloob <mat...@golang.org>
    Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
    Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
    Gerrit-Attention: Michael Matloob <mat...@golang.org>
    Gerrit-Attention: Michael Pratt <mpr...@google.com>
    unsatisfied_requirement
    satisfied_requirement
    open
    diffy

    Michael Matloob (Gerrit)

    unread,
    May 18, 2026, 1:59:13 PM (18 hours ago) May 18
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
    Attention needed from Michael Matloob and Michael Pratt

    Michael Matloob uploaded new patchset

    Michael Matloob uploaded patch set #22 to this change.
    Open in Gerrit

    Related details

    Attention is currently required from:
    • Michael Matloob
    • Michael Pratt
    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: newpatchset
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I9071a64fad79dc80118aaf81cb83c59e6a6a6964
    Gerrit-Change-Number: 776020
    Gerrit-PatchSet: 22
    unsatisfied_requirement
    satisfied_requirement
    open
    diffy

    Michael Pratt (Gerrit)

    unread,
    May 18, 2026, 3:47:24 PM (16 hours ago) May 18
    to Michael Matloob, goph...@pubsubhelper.golang.org, golang...@luci-project-accounts.iam.gserviceaccount.com, Michael Pratt, golang-co...@googlegroups.com
    Attention needed from Michael Matloob

    Michael Pratt added 2 comments

    File src/runtime/malloc_stubs.go
    Line 442, Patchset 22 (Latest): const nbytes = npages_ * 8192
    const heapBitsReserve = (1 - noscanint_) * nbytes / int(goarch.PtrSize) / 8
    const gcInlineMarkBitsReserve = int(unsafe.Sizeof(spanInlineMarkBits{})) // same on 32 and 64-bit platforms
    var nelemsGreenTea = uintptr(nbytes-gcInlineMarkBitsReserve-heapBitsReserve) / elemsize
    Michael Pratt . unresolved

    Where did this come from? The calculation seems quite different from the base nextFreeFastTiny.

    Line 446, Patchset 22 (Latest): var nelemsNoGreenTea = uintptr(nbytes-heapBitsReserve) / elemsize
    Michael Pratt . resolved

    No need to rip it out now, but IMO don't even try to support sizespecializedmalloc for !greentea. Basically no one uses that path.

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Michael Matloob
    Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement is not 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: I9071a64fad79dc80118aaf81cb83c59e6a6a6964
      Gerrit-Change-Number: 776020
      Gerrit-PatchSet: 22
      Gerrit-Owner: Michael Matloob <mat...@golang.org>
      Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
      Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
      Gerrit-Attention: Michael Matloob <mat...@golang.org>
      Gerrit-Comment-Date: Mon, 18 May 2026 19:47:19 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      unsatisfied_requirement
      open
      diffy

      Michael Matloob (Gerrit)

      unread,
      May 18, 2026, 4:00:01 PM (16 hours ago) May 18
      to Michael Matloob, goph...@pubsubhelper.golang.org, golang...@luci-project-accounts.iam.gserviceaccount.com, Michael Pratt, golang-co...@googlegroups.com
      Attention needed from Michael Matloob

      Michael Matloob voted Code-Review+1

      Code-Review+1
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Michael Matloob
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement is not 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: I9071a64fad79dc80118aaf81cb83c59e6a6a6964
      Gerrit-Change-Number: 776020
      Gerrit-PatchSet: 22
      Gerrit-Owner: Michael Matloob <mat...@golang.org>
      Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
      Gerrit-Reviewer: Michael Matloob <mat...@google.com>
      Gerrit-Comment-Date: Mon, 18 May 2026 19:59:58 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      unsatisfied_requirement
      open
      diffy

      Michael Matloob (Gerrit)

      unread,
      May 18, 2026, 4:29:52 PM (16 hours ago) May 18
      to goph...@pubsubhelper.golang.org, Michael Matloob, golang...@luci-project-accounts.iam.gserviceaccount.com, Michael Pratt, golang-co...@googlegroups.com
      Attention needed from Michael Matloob and Michael Pratt

      Michael Matloob added 1 comment

      File src/runtime/malloc_stubs.go
      Line 442, Patchset 22: const nbytes = npages_ * 8192

      const heapBitsReserve = (1 - noscanint_) * nbytes / int(goarch.PtrSize) / 8
      const gcInlineMarkBitsReserve = int(unsafe.Sizeof(spanInlineMarkBits{})) // same on 32 and 64-bit platforms
      var nelemsGreenTea = uintptr(nbytes-gcInlineMarkBitsReserve-heapBitsReserve) / elemsize
      Michael Pratt . unresolved

      Where did this come from? The calculation seems quite different from the base nextFreeFastTiny.

      Michael Matloob

      The original code that does the calculation is in https://cs.opensource.google/go/go/+/master:src/runtime/mheap.go;l=1457;drc=d247ed00e498e9717fb7c80d126bee5a8afdb4e8:

      The calculation in the green tea case is:
      ```
      s.nelems = uint16((nbytes - reserve) / s.elemsize)
      ```

      and reserve has two components (note that gcUsersSpanInlineMarkBits(s.elemsize) and heapBitsInSpan(s.elemsize) are always true in the size specialized case- even in the tiny case we're using a span of size 16 bytes and we only specialize in cases where heapBitsInSpan is true) They are unsafe.Sizeof(spanInlineMarkBits) (always) and nbytes/goarch.PtrSize/8 (only in the scan case)

      so reserve = unsafe.Sizeof(spanInlineMarkBits) + (1-noscanint_)*(nbytes/goarch.PtrSize/8)

      and

      nelemsGreenTea = uint16(nbytes-gcInlineMarkBitsReserve-heapBitsReserve) / elemsize

      ------

      It's equivalent in the tiny case(though spelled a bit differently):

      • For tiny (and in fact all specialized classes) npages_ = 1 so it doesn't need to appear in the calculation. So nbytes is the same between them.
      • heapBitsReserve is also 0 for the tiny case becasue noscanint=1.
      • in the tiny calculation, we inlined gcInlineMarkBitsReserve. (i left it as an individual factor so that i could add the comment because it wasn't obvious to me that it would be the same size on 32 and 64 bit)
      • elemsize_ becomes elemsize to make things a bit easier when we add the slow path cases where it will be a variable and not a constant. But it's still a constant in the specialized fast path cases when it's inlined
      • So in the tiny case:
      ``` var nelemsGreenTea = uintptr(nbytes-gcInlineMarkBitsReserve-heapBitsReserve) / elemsize // new calculation
      = uintptr(nbytes-int(unsafe.Sizeof(spanInlineMarkBits{})-0) / elemsize_ // substitute gcInlineMarkBitsReserve=int(unsafe.Sizeof(spanInlineMarkBits{}), heapBitsReserve=0, elemsize = elemsize_
      = uint16(nbytes-unsafe.Sizeof(spanInlineMarkBits{}))/elemsize_ // (changing type conversions since sizeof span inline markbits fits into an int and nelems fits into a uint16)
      ```
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Michael Matloob
      • Michael Pratt
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement is not 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: I9071a64fad79dc80118aaf81cb83c59e6a6a6964
      Gerrit-Change-Number: 776020
      Gerrit-PatchSet: 23
      Gerrit-Owner: Michael Matloob <mat...@golang.org>
      Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
      Gerrit-Reviewer: Michael Matloob <mat...@google.com>
      Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
      Gerrit-Attention: Michael Matloob <mat...@golang.org>
      Gerrit-Attention: Michael Pratt <mpr...@google.com>
      Gerrit-Comment-Date: Mon, 18 May 2026 20:29:48 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      Comment-In-Reply-To: Michael Pratt <mpr...@google.com>
      unsatisfied_requirement
      open
      diffy

      Michael Matloob (Gerrit)

      unread,
      May 18, 2026, 4:29:52 PM (16 hours ago) May 18
      to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
      Attention needed from Michael Matloob and Michael Pratt

      Michael Matloob uploaded new patchset

      Michael Matloob uploaded patch set #23 to this change.
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Michael Matloob
      • Michael Pratt
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement is not 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: newpatchset
      unsatisfied_requirement
      open
      diffy

      Michael Matloob (Gerrit)

      unread,
      May 18, 2026, 4:31:11 PM (16 hours ago) May 18
      to goph...@pubsubhelper.golang.org, golang...@luci-project-accounts.iam.gserviceaccount.com, Michael Matloob, Michael Pratt, golang-co...@googlegroups.com
      Attention needed from Michael Pratt

      Michael Matloob added 1 comment

      File src/runtime/malloc_stubs.go
      Line 446, Patchset 22: var nelemsNoGreenTea = uintptr(nbytes-heapBitsReserve) / elemsize
      Michael Pratt . resolved

      No need to rip it out now, but IMO don't even try to support sizespecializedmalloc for !greentea. Basically no one uses that path.

      Michael Matloob

      Oh that will definitely make it simpler!

      Open in Gerrit

      Related details

      Attention is currently required from:
      • Michael Pratt
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement is not 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: I9071a64fad79dc80118aaf81cb83c59e6a6a6964
      Gerrit-Change-Number: 776020
      Gerrit-PatchSet: 23
      Gerrit-Owner: Michael Matloob <mat...@golang.org>
      Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
      Gerrit-Reviewer: Michael Matloob <mat...@google.com>
      Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
      Gerrit-Attention: Michael Pratt <mpr...@google.com>
      Gerrit-Comment-Date: Mon, 18 May 2026 20:31:08 +0000
      unsatisfied_requirement
      open
      diffy

      Michael Matloob (Gerrit)

      unread,
      May 18, 2026, 5:12:03 PM (15 hours ago) May 18
      to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
      Attention needed from Michael Matloob and Michael Pratt

      Michael Matloob uploaded new patchset

      Michael Matloob uploaded patch set #24 to this change.
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Michael Matloob
      • Michael Pratt
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement is not 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: newpatchset
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I9071a64fad79dc80118aaf81cb83c59e6a6a6964
      Gerrit-Change-Number: 776020
      Gerrit-PatchSet: 24
      Gerrit-Owner: Michael Matloob <mat...@golang.org>
      Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
      Gerrit-Reviewer: Michael Matloob <mat...@google.com>
      Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
      unsatisfied_requirement
      open
      diffy

      Michael Matloob (Gerrit)

      unread,
      May 18, 2026, 10:07:22 PM (10 hours ago) May 18
      to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
      Attention needed from Michael Matloob, Michael Matloob and Michael Pratt

      Michael Matloob uploaded new patchset

      Michael Matloob uploaded patch set #25 to this change.
      Following approvals got outdated and were removed:
      • Code-Review: +1 by Michael Matloob
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Michael Matloob
      • Michael Matloob
      • Michael Pratt
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement is not 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: newpatchset
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I9071a64fad79dc80118aaf81cb83c59e6a6a6964
      Gerrit-Change-Number: 776020
      Gerrit-PatchSet: 25
      Gerrit-Owner: Michael Matloob <mat...@golang.org>
      Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
      Gerrit-Reviewer: Michael Matloob <mat...@google.com>
      Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
      Gerrit-Attention: Michael Matloob <mat...@golang.org>
      Gerrit-Attention: Michael Matloob <mat...@google.com>
      Gerrit-Attention: Michael Pratt <mpr...@google.com>
      unsatisfied_requirement
      open
      diffy

      Michael Matloob (Gerrit)

      unread,
      May 18, 2026, 10:12:19 PM (10 hours ago) May 18
      to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
      Attention needed from Michael Matloob, Michael Matloob and Michael Pratt

      Michael Matloob uploaded new patchset

      Michael Matloob uploaded patch set #26 to this change.
      Following approvals got outdated and were removed:
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Michael Matloob
      • Michael Matloob
      • Michael Pratt
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement is not 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: newpatchset
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I9071a64fad79dc80118aaf81cb83c59e6a6a6964
      Gerrit-Change-Number: 776020
      Gerrit-PatchSet: 26
      unsatisfied_requirement
      open
      diffy

      Michael Matloob (Gerrit)

      unread,
      7:58 AM (11 minutes ago) 7:58 AM
      to goph...@pubsubhelper.golang.org, golang...@luci-project-accounts.iam.gserviceaccount.com, Michael Matloob, golang-co...@googlegroups.com
      Attention needed from Michael Matloob

      Michael Matloob added 1 comment

      Patchset-level comments
      File-level comment, Patchset 26 (Latest):
      Michael Matloob . resolved

      The logic is still right but somehow this is making performance worse. I'll take this out of review for now.

      Open in Gerrit

      Related details

      Attention is currently required from:
      • Michael Matloob
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement is not 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: I9071a64fad79dc80118aaf81cb83c59e6a6a6964
      Gerrit-Change-Number: 776020
      Gerrit-PatchSet: 26
      Gerrit-Owner: Michael Matloob <mat...@golang.org>
      Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
      Gerrit-Reviewer: Michael Matloob <mat...@google.com>
      Gerrit-Attention: Michael Matloob <mat...@google.com>
      Gerrit-Comment-Date: Tue, 19 May 2026 11:58:16 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      unsatisfied_requirement
      open
      diffy
      Reply all
      Reply to author
      Forward
      0 new messages