Skip to content

cmd/compile: extra allocation when returning a non-pointer value > 128KB #76703

@zigo101

Description

@zigo101

Go version

go version go1.26-devel_91267f0a70 Thu Dec 4 17:35:46 2025 -0800 linux/amd64

Output of go env in your module/workspace:

.

What did you do?

package main

import "testing"

type Data struct {
	v [1 << 17 + 1]byte
}

func NonPointerReturn() Data {
	return Data{}
}

var x byte
var n = 12

func useData(d *Data) {
	x = d.v[n]
}

func Benchmark_NonPointerReturn_3clause(b *testing.B) {
	for i := 0; i < b.N; i++ {
		var d = NonPointerReturn()
		useData(&d)
	}
}

func Benchmark_NonPointerReturn_range(b *testing.B) {
	for range b.N {
		var d = NonPointerReturn()
		useData(&d)
	}
}

func Benchmark_NonPointerReturn_1clause(b *testing.B) {
	for b.Loop() {
		var d = NonPointerReturn()
		useData(&d)
	}
}

//------

func Benchmark_NonPointerReturnB_3clause(b *testing.B) {
	for i := 0; i < b.N; i++ {
		var d = new(Data)
		*d = NonPointerReturn()
		useData(d)
	}
}

func Benchmark_NonPointerReturnB_range(b *testing.B) {
	for range b.N {
		var d = new(Data)
		*d = NonPointerReturn()
		useData(d)
	}
}

func Benchmark_NonPointerReturnB_1clause(b *testing.B) {
	for b.Loop() {
		var d = new(Data)
		*d = NonPointerReturn()
		useData(d)
	}
}

What did you see happen?

Benchmark_NonPointerReturn_3clause-4    	   26307	     51341 ns/op	  278528 B/op	       2 allocs/op
Benchmark_NonPointerReturn_range-4      	   23431	     50276 ns/op	  278528 B/op	       2 allocs/op
Benchmark_NonPointerReturn_1clause-4    	   22632	     51538 ns/op	  278528 B/op	       2 allocs/op
Benchmark_NonPointerReturnB_3clause-4   	   23570	     49242 ns/op	  278528 B/op	       2 allocs/op
Benchmark_NonPointerReturnB_range-4     	   24536	     49956 ns/op	  278528 B/op	       2 allocs/op
Benchmark_NonPointerReturnB_1clause-4   	   23439	     48533 ns/op	  278528 B/op	       2 allocs/op

What did you expect to see?

Benchmark_NonPointerReturn_3clause-4    	   26307	     51341 ns/op	  278528 B/op	       1 allocs/op
Benchmark_NonPointerReturn_range-4      	   23431	     50276 ns/op	  278528 B/op	       1 allocs/op
Benchmark_NonPointerReturn_1clause-4    	   22632	     51538 ns/op	  278528 B/op	       1 allocs/op
Benchmark_NonPointerReturnB_3clause-4   	   23570	     49242 ns/op	  278528 B/op	      1 allocs/op
Benchmark_NonPointerReturnB_range-4     	   24536	     49956 ns/op	  278528 B/op	       1 allocs/op
Benchmark_NonPointerReturnB_1clause-4   	   23439	     48533 ns/op	  278528 B/op	       1 allocs/op

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Performancecompiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    Status

    Todo

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions