Skip to content

Commit 48dc0fc

Browse files
committed
runtime: optimize findHead
This is similar to #3899, but smaller and hopefully just as efficient.
1 parent 0edeaf6 commit 48dc0fc

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/runtime/gc_blocks.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,20 @@ func (b gcBlock) address() uintptr {
123123
// points to an allocated object. It returns the same block if this block
124124
// already points to the head.
125125
func (b gcBlock) findHead() gcBlock {
126-
for b.state() == blockStateTail {
127-
b--
126+
const blockStateByteAllTails = uint8(blockStateTail<<(stateBits*3) | blockStateTail<<(stateBits*2) | blockStateTail<<(stateBits*1) | blockStateTail<<(stateBits*0))
127+
for {
128+
if b%blocksPerStateByte == blocksPerStateByte-1 {
129+
stateByte := *(*uint8)(unsafe.Add(metadataStart, b/blocksPerStateByte))
130+
if stateByte == blockStateByteAllTails {
131+
b -= 4
132+
continue
133+
}
134+
}
135+
if b.state() == blockStateTail {
136+
b--
137+
continue
138+
}
139+
break
128140
}
129141
if gcAsserts {
130142
if b.state() != blockStateHead && b.state() != blockStateMark {

0 commit comments

Comments
 (0)