Skip to content

Commit afd2900

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

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

builder/sizes_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ func TestBinarySize(t *testing.T) {
4141
// This is a small number of very diverse targets that we want to test.
4242
tests := []sizeTest{
4343
// microcontrollers
44-
{"hifive1b", "examples/echo", 4568, 280, 0, 2268},
45-
{"microbit", "examples/serial", 2868, 388, 8, 2272},
46-
{"wioterminal", "examples/pininterrupt", 6104, 1484, 116, 6832},
44+
{"hifive1b", "examples/echo", 4588, 280, 0, 2268},
45+
{"microbit", "examples/serial", 2888, 388, 8, 2272},
46+
{"wioterminal", "examples/pininterrupt", 6124, 1484, 116, 6832},
4747

4848
// TODO: also check wasm. Right now this is difficult, because
4949
// wasm binaries are run through wasm-opt and therefore the

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)