From 735647d92e839f9ac3a91864a2c34263338a35e6 Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Mon, 8 Mar 2021 15:36:28 -0800 Subject: [PATCH] runtime: add alignment info to sizeclasses.go comments I was curious about the minimum possible alignment for each size class and the minimum size to guarantee any particular alignment (e.g., to know at what class size you can start assuming heap bits are byte- or word-aligned). Change-Id: I205b750286e8914986533c4f60712c420c3e63e9 Reviewed-on: https://go-review.googlesource.com/c/go/+/299909 Run-TryBot: Matthew Dempsky TryBot-Result: Go Bot Reviewed-by: Michael Knyszek Trust: Matthew Dempsky --- src/runtime/mksizeclasses.go | 26 ++++++- src/runtime/sizeclasses.go | 145 +++++++++++++++++++---------------- 2 files changed, 101 insertions(+), 70 deletions(-) diff --git a/src/runtime/mksizeclasses.go b/src/runtime/mksizeclasses.go index b4a117d343..8b9bbe01e6 100644 --- a/src/runtime/mksizeclasses.go +++ b/src/runtime/mksizeclasses.go @@ -37,6 +37,7 @@ import ( "go/format" "io" "log" + "math/bits" "os" ) @@ -242,8 +243,9 @@ nextk: } func printComment(w io.Writer, classes []class) { - fmt.Fprintf(w, "// %-5s %-9s %-10s %-7s %-10s %-9s\n", "class", "bytes/obj", "bytes/span", "objects", "tail waste", "max waste") + fmt.Fprintf(w, "// %-5s %-9s %-10s %-7s %-10s %-9s %-9s\n", "class", "bytes/obj", "bytes/span", "objects", "tail waste", "max waste", "min align") prevSize := 0 + var minAligns [32]int for i, c := range classes { if i == 0 { continue @@ -252,8 +254,28 @@ func printComment(w io.Writer, classes []class) { objects := spanSize / c.size tailWaste := spanSize - c.size*(spanSize/c.size) maxWaste := float64((c.size-prevSize-1)*objects+tailWaste) / float64(spanSize) + alignBits := bits.TrailingZeros(uint(c.size)) + for i := range minAligns { + if i > alignBits { + minAligns[i] = 0 + } else if minAligns[i] == 0 { + minAligns[i] = c.size + } + } prevSize = c.size - fmt.Fprintf(w, "// %5d %9d %10d %7d %10d %8.2f%%\n", i, c.size, spanSize, objects, tailWaste, 100*maxWaste) + fmt.Fprintf(w, "// %5d %9d %10d %7d %10d %8.2f%% %9d\n", i, c.size, spanSize, objects, tailWaste, 100*maxWaste, 1<