Skip to content

Commit 7b33b62

Browse files
committed
runtime: introduce treapForSpan to reduce code duplication
Currently which treap a span should be inserted into/removed from is checked by looking at the span's properties. This logic is repeated in four places. As this logic gets more complex, it makes sense to de-duplicate this, so introduce treapForSpan instead which captures this logic by returning the appropriate treap for the span. For #30333. Change-Id: I4bd933d93dc50c5fc7c7c7f56ceb95194dcbfbcc Reviewed-on: https://go-review.googlesource.com/c/go/+/170857 Run-TryBot: Michael Knyszek <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Austin Clements <[email protected]>
1 parent d13a931 commit 7b33b62

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

src/runtime/mheap.go

+14-20
Original file line numberDiff line numberDiff line change
@@ -464,11 +464,7 @@ func (h *mheap) coalesce(s *mspan) {
464464

465465
// The size is potentially changing so the treap needs to delete adjacent nodes and
466466
// insert back as a combined node.
467-
if other.scavenged {
468-
h.scav.removeSpan(other)
469-
} else {
470-
h.free.removeSpan(other)
471-
}
467+
h.treapForSpan(other).removeSpan(other)
472468
other.state = mSpanDead
473469
h.spanalloc.free(unsafe.Pointer(other))
474470
}
@@ -486,11 +482,8 @@ func (h *mheap) coalesce(s *mspan) {
486482
return
487483
}
488484
// Since we're resizing other, we must remove it from the treap.
489-
if other.scavenged {
490-
h.scav.removeSpan(other)
491-
} else {
492-
h.free.removeSpan(other)
493-
}
485+
h.treapForSpan(other).removeSpan(other)
486+
494487
// Round boundary to the nearest physical page size, toward the
495488
// scavenged span.
496489
boundary := b.startAddr
@@ -507,11 +500,7 @@ func (h *mheap) coalesce(s *mspan) {
507500
h.setSpan(boundary, b)
508501

509502
// Re-insert other now that it has a new size.
510-
if other.scavenged {
511-
h.scav.insert(other)
512-
} else {
513-
h.free.insert(other)
514-
}
503+
h.treapForSpan(other).insert(other)
515504
}
516505

517506
// Coalesce with earlier, later spans.
@@ -1112,6 +1101,15 @@ func (h *mheap) setSpans(base, npage uintptr, s *mspan) {
11121101
}
11131102
}
11141103

1104+
// treapForSpan returns the appropriate treap for a span for
1105+
// insertion and removal.
1106+
func (h *mheap) treapForSpan(span *mspan) *mTreap {
1107+
if span.scavenged {
1108+
return &h.scav
1109+
}
1110+
return &h.free
1111+
}
1112+
11151113
// pickFreeSpan acquires a free span from internal free list
11161114
// structures if one is available. Otherwise returns nil.
11171115
// h must be locked.
@@ -1343,11 +1341,7 @@ func (h *mheap) freeSpanLocked(s *mspan, acctinuse, acctidle bool, unusedsince i
13431341
h.coalesce(s)
13441342

13451343
// Insert s into the appropriate treap.
1346-
if s.scavenged {
1347-
h.scav.insert(s)
1348-
} else {
1349-
h.free.insert(s)
1350-
}
1344+
h.treapForSpan(s).insert(s)
13511345
}
13521346

13531347
// scavengeLargest scavenges nbytes worth of spans in unscav

0 commit comments

Comments
 (0)