1
0
mirror of https://github.com/golang/go synced 2024-09-25 03:10:12 -06: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))
}
// find searches for, finds, and returns the treap node containing the
// smallest span that can hold npages. If no span has at least npages
// it returns nil.
// find searches for, finds, and returns the treap iterator representing
// the position of the smallest span that can hold npages. If no span has
// at least npages it returns an invalid iterator.
// This is slightly more complicated than a simple binary tree search
// since if an exact match is not found the next larger node is
// returned.
func (root *mTreap) find(npages uintptr) *treapNode {
func (root *mTreap) find(npages uintptr) treapIter {
t := root.treap
for t != 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 {
t = t.left
} else {
return t
return treapIter{t}
}
}
return nil
return treapIter{}
}
// 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
// closer in size to the amount we requested (npage).
var s *mspan
if tf != nil && (ts == nil || tf.spanKey.npages <= ts.spanKey.npages) {
s = tf.spanKey
h.free.removeNode(tf)
} else if ts != nil && (tf == nil || tf.spanKey.npages > ts.spanKey.npages) {
s = ts.spanKey
h.scav.removeNode(ts)
if tf.valid() && (!ts.valid() || tf.span().npages <= ts.span().npages) {
s = tf.span()
h.free.erase(tf)
} else if ts.valid() && (!tf.valid() || tf.span().npages > ts.span().npages) {
s = ts.span()
h.scav.erase(ts)
}
return s
}