Skip to content

Commit

Permalink
experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
HattoriHanzo031 committed Nov 6, 2024
1 parent 47f5d3c commit b5a6ca7
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions src/runtime/gc_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ package runtime

import (
"internal/task"
"math/bits"
"runtime/interrupt"
"unsafe"
)
Expand Down Expand Up @@ -125,29 +124,24 @@ func (b gcBlock) address() uintptr {
return addr
}

// findHead returns the head (first block) of an object, assuming the block
// points to an allocated object. It returns the same block if this block
// already points to the head.
func (b gcBlock) findHead() gcBlock {
stateBytePtr := (*uint8)(unsafe.Add(metadataStart, b/blocksPerStateByte))
//const blockStateByteAllTails = uint8(blockStateTail<<(stateBits*3) | blockStateTail<<(stateBits*2) | blockStateTail<<(stateBits*1) | blockStateTail<<(stateBits*0))

// XOR the stateByte with byte containing all tails to turn tail bits to 0 and
// mask out the bits that are not part of the object
otherObjectBlocks := int(blocksPerStateByte - (b%blocksPerStateByte + 1))
stateByte := ((*stateBytePtr) ^ blockStateByteAllTails) & (uint8(1<<(8-(otherObjectBlocks*stateBits))) - 1)
func (b gcBlock) findHead() gcBlock {
for {
rem := b % blocksPerStateByte
stateByte := *(*uint8)(unsafe.Add(metadataStart, b/blocksPerStateByte))
if stateByte == blockStateByteAllTails {
b -= rem + 1
continue
}

// loop until state byte is not all tails
for stateByte == 0 {
stateBytePtr = (*uint8)(unsafe.Add(unsafe.Pointer(stateBytePtr), -1))
stateByte = (*stateBytePtr) ^ blockStateByteAllTails
b -= blocksPerStateByte
state := blockState(stateByte>>(rem*stateBits)) & blockStateMask
if state != blockStateTail {
break
}
b--
}

// in the first state byte which is not all tails, count the number of leading bits that are 0 and
// divide it by stateBits to get the number of tail blocks. Subtract otherObjectBlocks to exclude
// blocks that are not part of the object
b -= gcBlock((bits.LeadingZeros8(stateByte) / stateBits) - otherObjectBlocks)

if gcAsserts {
if b.state() != blockStateHead && b.state() != blockStateMark {
runtimePanic("gc: found tail without head")
Expand Down

0 comments on commit b5a6ca7

Please sign in to comment.