1
0
mirror of https://github.com/golang/go synced 2024-11-12 07:30:25 -07:00

runtime: use iterator instead of raw node for treap find

Right now the mTreap structure exposes the treapNode structure through
only one interface: find. There's no reason (performance or otherwise)
for exposing this, and we get a cleaner abstraction through the
iterators this way. This change also makes it easier to make changes to
the mTreap implementation without violating its interface.

Change-Id: I5ef86b8ac81a47d05d8404df65af9ec5f419dc40
Reviewed-on: https://go-review.googlesource.com/c/go/+/164098
Reviewed-by: Austin Clements <austin@google.com>
This commit is contained in:
Michael Anthony Knyszek 2019-02-11 17:20:59 +00:00 committed by Michael Knyszek
parent 23d4c6cdd6
commit 7bb8fc1033
2 changed files with 12 additions and 12 deletions

View File

@ -295,13 +295,13 @@ func (root *mTreap) removeNode(t *treapNode) {
mheap_.treapalloc.free(unsafe.Pointer(t)) mheap_.treapalloc.free(unsafe.Pointer(t))
} }
// find searches for, finds, and returns the treap node containing the // find searches for, finds, and returns the treap iterator representing
// smallest span that can hold npages. If no span has at least npages // the position of the smallest span that can hold npages. If no span has
// it returns nil. // at least npages it returns an invalid iterator.
// This is slightly more complicated than a simple binary tree search // This is slightly more complicated than a simple binary tree search
// since if an exact match is not found the next larger node is // since if an exact match is not found the next larger node is
// returned. // returned.
func (root *mTreap) find(npages uintptr) *treapNode { func (root *mTreap) find(npages uintptr) treapIter {
t := root.treap t := root.treap
for t != nil { for t != nil {
if t.spanKey == nil { if t.spanKey == nil {
@ -312,10 +312,10 @@ func (root *mTreap) find(npages uintptr) *treapNode {
} else if t.left != nil && t.left.npagesKey >= npages { } else if t.left != nil && t.left.npagesKey >= npages {
t = t.left t = t.left
} else { } else {
return t return treapIter{t}
} }
} }
return nil return treapIter{}
} }
// removeSpan searches for, finds, deletes span along with // removeSpan searches for, finds, deletes span along with

View File

@ -1126,12 +1126,12 @@ func (h *mheap) pickFreeSpan(npage uintptr) *mspan {
// Note that we want the _smaller_ free span, i.e. the free span // Note that we want the _smaller_ free span, i.e. the free span
// closer in size to the amount we requested (npage). // closer in size to the amount we requested (npage).
var s *mspan var s *mspan
if tf != nil && (ts == nil || tf.spanKey.npages <= ts.spanKey.npages) { if tf.valid() && (!ts.valid() || tf.span().npages <= ts.span().npages) {
s = tf.spanKey s = tf.span()
h.free.removeNode(tf) h.free.erase(tf)
} else if ts != nil && (tf == nil || tf.spanKey.npages > ts.spanKey.npages) { } else if ts.valid() && (!tf.valid() || tf.span().npages > ts.span().npages) {
s = ts.spanKey s = ts.span()
h.scav.removeNode(ts) h.scav.erase(ts)
} }
return s return s
} }