Skip to content

cmd/compile: static allocation should work with constant propagation and inlining #66671

@dsnet

Description

@dsnet

Go version

go1.22.0

Output of go env in your module/workspace:

GOOS=linux
GOARCH=amd64

What did you do?

Run the following benchmark:

package main

import "testing"

type Option interface{ isOption() }

type maxWindowSize uint64

func (maxWindowSize) isOption() {}

func MaxWindowSize(n uint64) Option { return maxWindowSize(n) }

var opt Option

func BenchmarkFunc(b *testing.B) {
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		opt = MaxWindowSize(1024)
	}
}

func BenchmarkDirect(b *testing.B) {
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		opt = maxWindowSize(1024)
	}
}

What did you see happen?

I see:

BenchmarkFunc-24      	109294134	        11.11 ns/op	       8 B/op	       1 allocs/op
BenchmarkDirect-24    	1000000000	         0.4708 ns/op	       0 B/op	       0 allocs/op

What did you expect to see?

I expect to see:

BenchmarkFunc-24      	1000000000	         0.4708 ns/op	       0 B/op	       0 allocs/op
BenchmarkDirect-24    	1000000000	         0.4708 ns/op	       0 B/op	       0 allocs/op

The MaxWindowSize function is inlineable, so I would expect it to be identical to declaring a maxWindowSize literal directly.

This seems to be an odd interaction between static allocations, constant propagation, and inlining. I'm not sure if the title properly reflects what's going on.

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

    No status

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions