Skip to content

Commit 5dd4d1f

Browse files
committed
runtime: add predecessor method to treap
This change adds a method for computing a treap node's predecessor to the treap, which will simplify the implementation of algorithms used for heap growth scavenging. For #14045. Change-Id: Id203e4bd246db3504f2f0c5163ec36f4579167df Reviewed-on: https://go-review.googlesource.com/c/144717 Run-TryBot: Michael Knyszek <mknyszek@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Austin Clements <austin@google.com>
1 parent 85143d3 commit 5dd4d1f

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/runtime/mgclarge.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,38 @@ type treapNode struct {
4646
priority uint32 // random number used by treap algorithm to keep tree probabilistically balanced
4747
}
4848

49+
func (t *treapNode) pred() *treapNode {
50+
if t.left != nil {
51+
// If it has a left child, its predecessor will be
52+
// its right most left (grand)child.
53+
t = t.left
54+
for t.right != nil {
55+
t = t.right
56+
}
57+
return t
58+
}
59+
// If it has no left child, its predecessor will be
60+
// the first grandparent who's right child is its
61+
// ancestor.
62+
//
63+
// We compute this by walking up the treap until the
64+
// current node's parent is its parent's right child.
65+
//
66+
// If we find at any point walking up the treap
67+
// that the current node doesn't have a parent,
68+
// we've hit the root. This means that t is already
69+
// the left-most node in the treap and therefore
70+
// has no predecessor.
71+
for t.parent != nil && t.parent.right != t {
72+
if t.parent.left != t {
73+
println("runtime: predecessor t=", t, "t.spanKey=", t.spanKey)
74+
throw("node is not its parent's child")
75+
}
76+
t = t.parent
77+
}
78+
return t.parent
79+
}
80+
4981
// isSpanInTreap is handy for debugging. One should hold the heap lock, usually
5082
// mheap_.lock().
5183
func (t *treapNode) isSpanInTreap(s *mspan) bool {

0 commit comments

Comments
 (0)