Skip to content

cmd/compile: use mapaccess1_fast64 for small keys #66446

@dsnet

Description

@dsnet

Go version

go1.22

Output of go env in your module/workspace:

GOARCH=amd64
GOOS=linux

What did you do?

Compile the following program:

package main

type key struct {
	u16 uint16
	u8  bool
	u32 uint32
}

var map1 map[key]struct{}
var map2 map[uint64]struct{}
var key1 = key{}
var key2 = uint64(0)
var sink struct{}

func main() {
	sink = map1[key1]
	sink = map2[key2]
}

What did you see happen?

The compiler output:

0x0023 00035 (main.go:17)	CALL	runtime.mapaccess1(SB)
...
0x0040 00064 (main.go:18)	CALL	runtime.mapaccess1_fast64(SB)

What did you expect to see?

runtime.mapaccess1_fast64 being used on both since the key type is smaller than a uint64.

That said, there is some complications. For example, through unsafe, someone could writes values to the padding in-between the bool field and the uint32. Also, someone using unsafe could write a bit pattern to the bool that isn't 0 or 1.
It isn't clear to me that this is something that someone can rely on.

However, even if we make the key struct exactly 64-bits wide with no padding and no bools, it still doesn't use runtime.mapaccess1_fast64.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.compiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    Status

    Todo

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions