Skip to content

Commit 411c250

Browse files
committed
runtime: add specialized malloc functions for sizes up to 512 bytes
This CL adds a generator function in runtime/_mkmalloc to generate specialized mallocgc functions for sizes up throuht 512 bytes. (That's the limit where it's possible to end up in the no header case when there are scan bits, and where the benefits of the specialized functions significantly diminish according to microbenchmarks). If the specializedmalloc GOEXPERIMENT is turned on, mallocgc will call one of these functions in the no header case. malloc_generated.go is the generated file containing the specialized malloc functions. malloc_stubs.go contains the templates that will be stamped to create the specialized malloc functions. malloc_tables_generated contains the tables that mallocgc will use to select the specialized function to call. I've had to update the two stdlib_test.go files to account for the new submodule mkmalloc is in. mprof_test accounts for the changes in the stacks since different functions can be called in some cases. I still need to investigate heapsampling.go. Change-Id: Ia0f68dccdf1c6a200554ae88657cf4d686ace819 Reviewed-on: https://go-review.googlesource.com/c/go/+/665835 Reviewed-by: Michael Knyszek <[email protected]> Reviewed-by: Michael Matloob <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent d7a38ad commit 411c250

File tree

15 files changed

+10860
-59
lines changed

15 files changed

+10860
-59
lines changed

src/cmd/compile/internal/types2/stdlib_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ func TestStdKen(t *testing.T) {
360360
var excluded = map[string]bool{
361361
"builtin": true,
362362
"cmd/compile/internal/ssa/_gen": true,
363+
"runtime/_mkmalloc": true,
363364
}
364365

365366
// printPackageMu synchronizes the printing of type-checked package files in

src/go/types/stdlib_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ func TestStdKen(t *testing.T) {
362362
var excluded = map[string]bool{
363363
"builtin": true,
364364
"cmd/compile/internal/ssa/_gen": true,
365+
"runtime/_mkmalloc": true,
365366
}
366367

367368
// printPackageMu synchronizes the printing of type-checked package files in

src/internal/runtime/gc/sizeclasses.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/runtime/_mkmalloc/constants.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2025 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
const (
8+
// Constants that we use and will transfer to the runtime.
9+
minHeapAlign = 8
10+
maxSmallSize = 32 << 10
11+
smallSizeDiv = 8
12+
smallSizeMax = 1024
13+
largeSizeDiv = 128
14+
pageShift = 13
15+
tinySize = 16
16+
17+
// Derived constants.
18+
pageSize = 1 << pageShift
19+
)
20+
21+
const (
22+
maxPtrSize = max(4, 8)
23+
maxPtrBits = 8 * maxPtrSize
24+
25+
// Maximum size smallScanNoHeader would be called for, which is the
26+
// maximum value gc.MinSizeForMallocHeader can have on any platform.
27+
// gc.MinSizeForMallocHeader is defined as goarch.PtrSize * goarch.PtrBits.
28+
smallScanNoHeaderMax = maxPtrSize * maxPtrBits
29+
)

src/runtime/_mkmalloc/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module runtime/_mkmalloc
2+
3+
go 1.24
4+
5+
require golang.org/x/tools v0.33.0

src/runtime/_mkmalloc/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
golang.org/x/tools v0.33.0 h1:4qz2S3zmRxbGIhDIAgjxvFutSvH5EfnsYrRBj0UI0bc=
2+
golang.org/x/tools v0.33.0/go.mod h1:CIJMaWEY88juyUfo7UbgPqbC8rU2OqfAV1h2Qp0oMYI=

0 commit comments

Comments
 (0)