Skip to content

Commit 9a315e8

Browse files
binary size optimization
1 parent 565a24d commit 9a315e8

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/runtime/gc_blocks.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ package runtime
3232

3333
import (
3434
"internal/task"
35-
"math/bits"
3635
"runtime/interrupt"
3736
"unsafe"
3837
)
@@ -80,6 +79,20 @@ const (
8079
// turned into variable and assigned using inline function.
8180
const blockStateByteAllTails = uint8(blockStateTail<<(stateBits*3) | blockStateTail<<(stateBits*2) | blockStateTail<<(stateBits*1) | blockStateTail<<(stateBits*0))
8281

82+
const len6tab = "" +
83+
"\x00\x01\x02\x02\x03\x03\x03\x03\x04\x04\x04\x04\x04\x04\x04\x04" +
84+
"\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05\x05" +
85+
"\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06" +
86+
"\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06\x06"
87+
88+
// leadingZeros6 returns the number of leading zero bits in x; the result is 6 for x == 0.
89+
func leadingZeros6(x uint8) int { return 6 - len6(x) }
90+
91+
// len6 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
92+
func len6(x uint8) int {
93+
return int(len6tab[x])
94+
}
95+
8396
// String returns a human-readable version of the block state, for debugging.
8497
func (s blockState) String() string {
8598
switch s {
@@ -151,7 +164,7 @@ func (b gcBlock) findHead() gcBlock {
151164
// at this point stateByte is set to the first state byte of the object that we encountered which is not all tails
152165
// and all tail bits in it are turned to zero. We count number of bytes that are 0 (tail) using LeadingZeros8
153166
// and divide it by stateBits to get the number of tail blocks in state bits.
154-
b -= gcBlock(bits.LeadingZeros8(stateByte) / stateBits)
167+
b -= gcBlock(leadingZeros6(stateByte>>2) / stateBits)
155168

156169
if gcAsserts {
157170
if b.state() != blockStateHead && b.state() != blockStateMark {

0 commit comments

Comments
 (0)