[go] runtime: add freeSlice

12 views
Skip to first unread message

t hepudds (Gerrit)

unread,
Sep 8, 2025, 1:48:10 PMSep 8
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

t hepudds has uploaded the change for review

Commit message

runtime: add freeSlice

WIP.

This CL is part of a series of CLs to triangulate between the runtime,
compiler, and standard library to reduce how much work the GC must do.

This adds runtime.freeSlice as a convenience function over
runtime.freeSized.

It is exercised in the following CLs in the stack.
Change-Id: Ic116c736b2f84fdce72c44807b036fa0437d9437

Change diff

diff --git a/src/runtime/malloc.go b/src/runtime/malloc.go
index ea63883..5a3ff87 100644
--- a/src/runtime/malloc.go
+++ b/src/runtime/malloc.go
@@ -101,6 +101,7 @@
package runtime

import (
+ "internal/abi"
"internal/goarch"
"internal/goexperiment"
"internal/goos"
@@ -2040,6 +2041,41 @@
return true
}

+// freeSlice records that the backing array for a slice is reusable and
+// available for immediate reuse in a subsequent mallocgc allocation.
+// It is a convenience wrapper function over freeSized.
+//
+// sl must contain a pointer to a slice (that is, *[]T). sl is an
+// interface to aid with finding the type.
+//
+// The slice data pointer must point to the base of the heap object (that is,
+// not resliced to the middle of the heap object). See freeSized
+// for additional requirements.
+func freeSlice(sl any) {
+ i := efaceOf(&sl)
+
+ capacity := (*slice)(i.data).cap
+ if capacity == 0 {
+ return
+ }
+
+ // Find the slice element type.
+ typ := (*_type)(abi.NoEscape(unsafe.Pointer(i._type))) // runtime types are never freed
+ if typ.Kind_&abi.KindMask != abi.Pointer {
+ throw("slice result of non-ptr type")
+ }
+ typ = (*ptrtype)(unsafe.Pointer(typ)).Elem
+ if typ.Kind_&abi.KindMask != abi.Slice {
+ throw("slice of non-ptr-to-slice type")
+ }
+ typ = (*slicetype)(unsafe.Pointer(typ)).Elem // slice element type
+
+ // Free the backing array.
+ noscan := !typ.Pointers()
+ freeSized((*slice)(i.data).array, uintptr(capacity)*typ.Size_, noscan)
+ *((*slice)(i.data)) = slice{}
+}
+
// nextReusableNoScan returns the next reusable object for a noscan span,
// or 0 if no reusable object is found.
func (c *mcache) nextReusableNoScan(s *mspan, spc spanClass) (gclinkptr, *mspan) {
@@ -2299,6 +2335,16 @@
return freeSized(p, size, noscan)
}

+//go:linkname slices_freeSized slices.runtimefreesized
+func slices_freeSized(p unsafe.Pointer, size uintptr, noscan bool) bool {
+ return freeSized(p, size, noscan)
+}
+
+//go:linkname slices_freeSlice slices.runtimefreeslice
+func slices_freeSlice(sl any) {
+ freeSlice(sl)
+}
+
// reflect_unsafe_New is meant for package reflect,
// but widely used packages access it using linkname.
// Notable members of the hall of shame include:

Change information

Files:
  • M src/runtime/malloc.go
Change size: S
Delta: 1 file changed, 46 insertions(+), 0 deletions(-)
Open in Gerrit

Related details

Attention set is empty
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: Ic116c736b2f84fdce72c44807b036fa0437d9437
Gerrit-Change-Number: 701815
Gerrit-PatchSet: 1
Gerrit-Owner: t hepudds <thepud...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

t hepudds (Gerrit)

unread,
Sep 8, 2025, 9:01:46 PMSep 8
to goph...@pubsubhelper.golang.org, Carlos Amedee, Michael Knyszek, Michael Pratt, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Carlos Amedee, Michael Knyszek and Michael Pratt

t hepudds voted Commit-Queue+1

Commit-Queue+1
Open in Gerrit

Related details

Attention is currently required from:
  • Carlos Amedee
  • Michael Knyszek
  • 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: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ic116c736b2f84fdce72c44807b036fa0437d9437
Gerrit-Change-Number: 701815
Gerrit-PatchSet: 1
Gerrit-Owner: t hepudds <thepud...@gmail.com>
Gerrit-Reviewer: Carlos Amedee <car...@golang.org>
Gerrit-Reviewer: Michael Knyszek <mkny...@google.com>
Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
Gerrit-Reviewer: t hepudds <thepud...@gmail.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: Michael Knyszek <mkny...@google.com>
Gerrit-Attention: Michael Pratt <mpr...@google.com>
Gerrit-Attention: Carlos Amedee <car...@golang.org>
Gerrit-Comment-Date: Tue, 09 Sep 2025 01:01:42 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

t hepudds (Gerrit)

unread,
Sep 8, 2025, 9:30:08 PMSep 8
to goph...@pubsubhelper.golang.org, Go LUCI, Carlos Amedee, Michael Knyszek, Michael Pratt, Gopher Robot, golang-co...@googlegroups.com
Gerrit-Reviewer: Michael Knyszek <mkny...@google.com>
Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
Gerrit-Reviewer: t hepudds <thepud...@gmail.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: Michael Knyszek <mkny...@google.com>
Gerrit-Attention: Michael Pratt <mpr...@google.com>
Gerrit-Attention: Carlos Amedee <car...@golang.org>
Gerrit-Comment-Date: Tue, 09 Sep 2025 01:30:03 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

t hepudds (Gerrit)

unread,
Sep 8, 2025, 10:04:06 PMSep 8
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
Attention needed from Carlos Amedee, Michael Knyszek and Michael Pratt

t hepudds uploaded new patchset

t hepudds uploaded patch set #2 to this change.
Open in Gerrit

Related details

Attention is currently required from:
  • Carlos Amedee
  • Michael Knyszek
  • 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: newpatchset
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ic116c736b2f84fdce72c44807b036fa0437d9437
    Gerrit-Change-Number: 701815
    Gerrit-PatchSet: 2
    unsatisfied_requirement
    satisfied_requirement
    open
    diffy

    t hepudds (Gerrit)

    unread,
    Sep 8, 2025, 10:05:00 PMSep 8
    to goph...@pubsubhelper.golang.org, Go LUCI, Carlos Amedee, Michael Knyszek, Michael Pratt, Gopher Robot, golang-co...@googlegroups.com
    Attention needed from Carlos Amedee, Michael Knyszek and Michael Pratt

    t hepudds added 1 comment

    Patchset-level comments
    File-level comment, Patchset 2 (Latest):
    t hepudds . resolved

    (Earlier trybot failure seems to be an existing flake.)

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Carlos Amedee
    • Michael Knyszek
    • 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: Ic116c736b2f84fdce72c44807b036fa0437d9437
    Gerrit-Change-Number: 701815
    Gerrit-PatchSet: 2
    Gerrit-Owner: t hepudds <thepud...@gmail.com>
    Gerrit-Reviewer: Carlos Amedee <car...@golang.org>
    Gerrit-Reviewer: Michael Knyszek <mkny...@google.com>
    Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
    Gerrit-Reviewer: t hepudds <thepud...@gmail.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Attention: Michael Knyszek <mkny...@google.com>
    Gerrit-Attention: Michael Pratt <mpr...@google.com>
    Gerrit-Attention: Carlos Amedee <car...@golang.org>
    Gerrit-Comment-Date: Tue, 09 Sep 2025 02:04:55 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    satisfied_requirement
    open
    diffy

    t hepudds (Gerrit)

    unread,
    Oct 26, 2025, 9:01:17 PM (10 days ago) Oct 26
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
    Attention needed from Carlos Amedee, Michael Knyszek and Michael Pratt

    t hepudds uploaded new patchset

    t hepudds uploaded patch set #6 to this change.
    Open in Gerrit

    Related details

    Attention is currently required from:
    • Carlos Amedee
    • Michael Knyszek
    • 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: Ic116c736b2f84fdce72c44807b036fa0437d9437
      Gerrit-Change-Number: 701815
      Gerrit-PatchSet: 6
      Gerrit-Owner: t hepudds <thepud...@gmail.com>
      Gerrit-Reviewer: Carlos Amedee <car...@golang.org>
      Gerrit-Reviewer: Michael Knyszek <mkny...@google.com>
      Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
      Gerrit-Reviewer: t hepudds <thepud...@gmail.com>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-Attention: Michael Pratt <mpr...@google.com>
      Gerrit-Attention: Carlos Amedee <car...@golang.org>
      Gerrit-Attention: Michael Knyszek <mkny...@google.com>
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      t hepudds (Gerrit)

      unread,
      Oct 27, 2025, 12:09:28 AM (10 days ago) Oct 27
      to goph...@pubsubhelper.golang.org, Go LUCI, Carlos Amedee, Michael Knyszek, Michael Pratt, Gopher Robot, golang-co...@googlegroups.com
      Attention needed from Carlos Amedee, Michael Knyszek and Michael Pratt

      t hepudds voted Commit-Queue+1

      Commit-Queue+1
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Carlos Amedee
      • Michael Knyszek
      • 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: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: Ic116c736b2f84fdce72c44807b036fa0437d9437
      Gerrit-Change-Number: 701815
      Gerrit-PatchSet: 6
      Gerrit-Owner: t hepudds <thepud...@gmail.com>
      Gerrit-Reviewer: Carlos Amedee <car...@golang.org>
      Gerrit-Reviewer: Michael Knyszek <mkny...@google.com>
      Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
      Gerrit-Reviewer: t hepudds <thepud...@gmail.com>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-Attention: Michael Pratt <mpr...@google.com>
      Gerrit-Attention: Carlos Amedee <car...@golang.org>
      Gerrit-Attention: Michael Knyszek <mkny...@google.com>
      Gerrit-Comment-Date: Mon, 27 Oct 2025 04:09:23 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      t hepudds (Gerrit)

      unread,
      Oct 27, 2025, 2:19:50 PM (10 days ago) Oct 27
      to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
      Attention needed from Carlos Amedee, Michael Knyszek and Michael Pratt

      t hepudds uploaded new patchset

      t hepudds uploaded patch set #8 to this change.
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Carlos Amedee
      • Michael Knyszek
      • 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: Ic116c736b2f84fdce72c44807b036fa0437d9437
      Gerrit-Change-Number: 701815
      Gerrit-PatchSet: 8
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      t hepudds (Gerrit)

      unread,
      Nov 3, 2025, 6:42:27 PM (2 days ago) Nov 3
      to goph...@pubsubhelper.golang.org, Go LUCI, Carlos Amedee, Michael Knyszek, Michael Pratt, Gopher Robot, golang-co...@googlegroups.com
      Attention needed from Carlos Amedee, Michael Knyszek and Michael Pratt

      t hepudds voted Commit-Queue+1

      Commit-Queue+1
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Carlos Amedee
      • Michael Knyszek
      • 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: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: Ic116c736b2f84fdce72c44807b036fa0437d9437
      Gerrit-Change-Number: 701815
      Gerrit-PatchSet: 12
      Gerrit-Owner: t hepudds <thepud...@gmail.com>
      Gerrit-Reviewer: Carlos Amedee <car...@golang.org>
      Gerrit-Reviewer: Michael Knyszek <mkny...@google.com>
      Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
      Gerrit-Reviewer: t hepudds <thepud...@gmail.com>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-Attention: Michael Pratt <mpr...@google.com>
      Gerrit-Attention: Carlos Amedee <car...@golang.org>
      Gerrit-Attention: Michael Knyszek <mkny...@google.com>
      Gerrit-Comment-Date: Mon, 03 Nov 2025 23:42:24 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      t hepudds (Gerrit)

      unread,
      Nov 3, 2025, 9:03:07 PM (2 days ago) Nov 3
      to goph...@pubsubhelper.golang.org, Go LUCI, Carlos Amedee, Michael Knyszek, Michael Pratt, Gopher Robot, golang-co...@googlegroups.com
      Attention needed from Carlos Amedee, Michael Knyszek and Michael Pratt

      t hepudds voted Commit-Queue+1

      Commit-Queue+1
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Carlos Amedee
      • Michael Knyszek
      • 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: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: Ic116c736b2f84fdce72c44807b036fa0437d9437
      Gerrit-Change-Number: 701815
      Gerrit-PatchSet: 13
      Gerrit-Owner: t hepudds <thepud...@gmail.com>
      Gerrit-Reviewer: Carlos Amedee <car...@golang.org>
      Gerrit-Reviewer: Michael Knyszek <mkny...@google.com>
      Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
      Gerrit-Reviewer: t hepudds <thepud...@gmail.com>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-Attention: Michael Pratt <mpr...@google.com>
      Gerrit-Attention: Carlos Amedee <car...@golang.org>
      Gerrit-Attention: Michael Knyszek <mkny...@google.com>
      Gerrit-Comment-Date: Tue, 04 Nov 2025 02:03:02 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      t hepudds (Gerrit)

      unread,
      Nov 4, 2025, 1:08:39 PM (2 days ago) Nov 4
      to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
      Attention needed from Carlos Amedee, Michael Knyszek and Michael Pratt

      t hepudds uploaded new patchset

      t hepudds uploaded patch set #15 to this change.
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Carlos Amedee
      • Michael Knyszek
      • 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: Ic116c736b2f84fdce72c44807b036fa0437d9437
      Gerrit-Change-Number: 701815
      Gerrit-PatchSet: 15
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy
      Reply all
      Reply to author
      Forward
      0 new messages