]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: use iterator instead of raw node for treap find
authorMichael Anthony Knyszek <mknyszek@google.com>
Mon, 11 Feb 2019 17:20:59 +0000 (17:20 +0000)
committerMichael Knyszek <mknyszek@google.com>
Mon, 8 Apr 2019 15:41:43 +0000 (15:41 +0000)
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>
src/runtime/mgclarge.go
src/runtime/mheap.go

index 7b01a117808fc4101b4d8d7420c5f9ef76a4de98..dba617c25da22e143674fd0e4c134c59503d16c5 100644 (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
index 8b7ed742c93fe1f372b798ec0c0ff054bd23664f..e9cd62d7d8f9c6250655ef16c15e7fe45c607b43 100644 (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
 }