Skip to content

Commit

Permalink
runtime: optimize findHead
Browse files Browse the repository at this point in the history
This is similar to #3899, but
smaller and hopefully just as efficient.
  • Loading branch information
aykevl committed Oct 31, 2024
1 parent 0edeaf6 commit afd2900
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
6 changes: 3 additions & 3 deletions builder/sizes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ func TestBinarySize(t *testing.T) {
// This is a small number of very diverse targets that we want to test.
tests := []sizeTest{
// microcontrollers
{"hifive1b", "examples/echo", 4568, 280, 0, 2268},
{"microbit", "examples/serial", 2868, 388, 8, 2272},
{"wioterminal", "examples/pininterrupt", 6104, 1484, 116, 6832},
{"hifive1b", "examples/echo", 4588, 280, 0, 2268},
{"microbit", "examples/serial", 2888, 388, 8, 2272},
{"wioterminal", "examples/pininterrupt", 6124, 1484, 116, 6832},

// TODO: also check wasm. Right now this is difficult, because
// wasm binaries are run through wasm-opt and therefore the
Expand Down
16 changes: 14 additions & 2 deletions src/runtime/gc_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,20 @@ func (b gcBlock) address() uintptr {
// points to an allocated object. It returns the same block if this block
// already points to the head.
func (b gcBlock) findHead() gcBlock {
for b.state() == blockStateTail {
b--
const blockStateByteAllTails = uint8(blockStateTail<<(stateBits*3) | blockStateTail<<(stateBits*2) | blockStateTail<<(stateBits*1) | blockStateTail<<(stateBits*0))
for {
if b%blocksPerStateByte == blocksPerStateByte-1 {
stateByte := *(*uint8)(unsafe.Add(metadataStart, b/blocksPerStateByte))
if stateByte == blockStateByteAllTails {
b -= 4
continue
}
}
if b.state() == blockStateTail {
b--
continue
}
break
}
if gcAsserts {
if b.state() != blockStateHead && b.state() != blockStateMark {
Expand Down

0 comments on commit afd2900

Please sign in to comment.