Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

affected/package: runtime #51362

Closed
schors opened this issue Feb 25, 2022 · 1 comment
Closed

affected/package: runtime #51362

schors opened this issue Feb 25, 2022 · 1 comment

Comments

@schors
Copy link
Contributor

schors commented Feb 25, 2022

Go versions from 1.7 to 1.18rc1, amd64, linux

package main_test

import "testing"
import "fmt"

// int64, int32, string - doesn't matter
type fixT int

const sz=15

func BenchmarkMapSize(b *testing.B) {
        for n := 1; n <= sz; n++ {
                b.Run(fmt.Sprintf("%d", n), func(b *testing.B) {
                        b.ReportAllocs()
                        for i := 0; i < b.N; i++ {
                                m := make(map[fixT]bool)
                                for j := 0; j < n; j++ {
                                        m[fixT(j)] = true
                                }
                        }
                })
        }
}

There are 3 allocations at point 14 every test, int64, int32 and string key types. Steps 9 and 13 - ok, but what is at step number14? I can't explain this behavior. It looks like unwanted magic number. Perhaps this is a unwanted feature of the hash distribution. I don't know.

BenchmarkMapSize/1-4 	20000000	        57.1 ns/op	       0 B/op	       0 allocs/op
BenchmarkMapSize/2-4 	20000000	        82.6 ns/op	       0 B/op	       0 allocs/op
BenchmarkMapSize/3-4 	20000000	       107 ns/op	       0 B/op	       0 allocs/op
BenchmarkMapSize/4-4 	10000000	       132 ns/op	       0 B/op	       0 allocs/op
BenchmarkMapSize/5-4 	10000000	       157 ns/op	       0 B/op	       0 allocs/op
BenchmarkMapSize/6-4 	10000000	       182 ns/op	       0 B/op	       0 allocs/op
BenchmarkMapSize/7-4 	10000000	       207 ns/op	       0 B/op	       0 allocs/op
BenchmarkMapSize/8-4 	10000000	       233 ns/op	       0 B/op	       0 allocs/op
BenchmarkMapSize/9-4 	 3000000	       533 ns/op	     160 B/op	       1 allocs/op
BenchmarkMapSize/10-4         	 3000000	       571 ns/op	     162 B/op	       1 allocs/op
BenchmarkMapSize/11-4         	 2000000	       615 ns/op	     168 B/op	       1 allocs/op
BenchmarkMapSize/12-4         	 2000000	       653 ns/op	     179 B/op	       1 allocs/op
BenchmarkMapSize/13-4         	 2000000	       701 ns/op	     196 B/op	       2 allocs/op
BenchmarkMapSize/14-4         	 1000000	      1238 ns/op	     517 B/op	       3 allocs/op
BenchmarkMapSize/15-4         	 1000000	      1299 ns/op	     518 B/op	       3 allocs/op

P.S. Map with big keys, [128]byte for example, shows different behavior. It's ok

@seankhliao
Copy link
Member

See https://go.googlesource.com/go/+/refs/heads/master/src/runtime/map.go for implementation details.
Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

@golang golang locked and limited conversation to collaborators Feb 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants