Hi guys, I am trying to implement an ARENA based lock-free skiplist, which is porting Dgraph's badger/skl at master · dgraph-io/badger · GitHub implementation. However, I meet some problems and want some helps and instructions on my code.
In original Go implementation, because Go has garbage collection, it is very simple to implement a growable lock-free ARENA (directly replace the buffer with a new one), but in Rust, it becomes extremely difficult and I have to use a Mutex.
func (s *Arena) allocate(sz uint32) uint32 {
offset := atomic.AddUint32(&s.n, sz)
if !s.shouldGrow {
y.AssertTrue(int(offset) <= len(s.buf))
return offset - sz
}
// We are keeping extra bytes in the end so that the checkptr doesn't fail. We apply some
// intelligence to reduce the size of the node by only keeping towers upto valid height and not
// maxHeight. This reduces the node's size, but checkptr doesn't know about its reduced size.
// checkptr tries to verify that the node of size MaxNodeSize resides on a single heap
// allocation which causes this error: checkptr:converted pointer straddles multiple allocations
if int(offset) > len(s.buf)-MaxNodeSize {
growBy := uint32(len(s.buf))
if growBy > 1<<30 {
growBy = 1 << 30
}
if growBy < sz {
growBy = sz
}
newBuf := make([]byte, len(s.buf)+int(growBy))
y.AssertTrue(len(s.buf) == copy(newBuf, s.buf))
s.buf = newBuf
}
return offset - sz
}
The Go version, lock-free growable ARENA source code is here:
- ARENA: badger/arena.go at master · dgraph-io/badger · GitHub
- Skiplist: badger/skl.go at master · dgraph-io/badger · GitHub
The Rust version lock-free(not lock-free in current implementation) growable ARENA source code is here:
- ARENA: https://github.com/al8n/skl-rs/blob/main/src/arena/epoch.rs
- Skiplist: https://github.com/al8n/skl-rs/blob/main/src/epoch.rs
I am a real newbie for writing lock-free data structure in Rust, really want someone can help me to complete this feature, or give me some instructions.
The repo is: GitHub - al8n/skl-rs: A lock-free thread-safe arena based Skiplist impelementation for building memtable.
Thank you very much!