Skip to content

Commit fa8470a

Browse files
committed
runtime: make treap iteration more efficient
This change introduces a treapIterFilter type which represents the power set of states described by a treapIterType. This change then adds a treapIterFilter field to each treap node indicating the types of spans that live in that subtree. The field is maintained via the same mechanism used to maintain maxPages. This allows pred, succ, start, and end to be judicious about which subtrees it will visit, ensuring that iteration avoids traversing irrelevant territory. Without this change, repeated scavenging attempts can end up being N^2 as the scavenger walks over what it already scavenged before finding new spans available for scavenging. Finally, this change also only scavenges a span once it is removed from the treap. There was always an invariant that spans owned by the treap may not be mutated in-place, but with this change violating that invariant can cause issues with scavenging. For #30333. Change-Id: I8040b997e21c94a8d3d9c8c6accfe23cebe0c3d3 Reviewed-on: https://go-review.googlesource.com/c/go/+/174878 Run-TryBot: Michael Knyszek <mknyszek@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Austin Clements <austin@google.com>
1 parent 9baa430 commit fa8470a

File tree

4 files changed

+236
-150
lines changed

4 files changed

+236
-150
lines changed

src/runtime/export_test.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -546,15 +546,21 @@ func (s Span) Pages() uintptr {
546546
return s.mspan.npages
547547
}
548548

549-
type TreapIterType int
549+
type TreapIterType treapIterType
550550

551551
const (
552552
TreapIterScav TreapIterType = TreapIterType(treapIterScav)
553553
TreapIterBits = treapIterBits
554554
)
555555

556+
type TreapIterFilter treapIterFilter
557+
558+
func TreapFilter(mask, match TreapIterType) TreapIterFilter {
559+
return TreapIterFilter(treapFilter(treapIterType(mask), treapIterType(match)))
560+
}
561+
556562
func (s Span) MatchesIter(mask, match TreapIterType) bool {
557-
return s.mspan.matchesIter(treapIterType(mask), treapIterType(match))
563+
return treapFilter(treapIterType(mask), treapIterType(match)).matches(s.treapFilter())
558564
}
559565

560566
type TreapIter struct {
@@ -639,5 +645,5 @@ func (t *Treap) Size() int {
639645

640646
func (t *Treap) CheckInvariants() {
641647
t.mTreap.treap.walkTreap(checkTreapNode)
642-
t.mTreap.treap.validateMaxPages()
648+
t.mTreap.treap.validateInvariants()
643649
}

0 commit comments

Comments
 (0)