Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to call Less() fewer times when iterating #49

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions btree.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,13 +504,18 @@ const (
// "greaterThan" or "lessThan" queries.
func (n *node) iterate(dir direction, start, stop Item, includeStart bool, hit bool, iter ItemIterator) (bool, bool) {
var ok, found bool
var index int
var index, stopIndex int
switch dir {
case ascend:
if start != nil {
index, _ = n.items.find(start)
}
for i := index; i < len(n.items); i++ {
if stop != nil {
stopIndex, _ = n.items.find(stop)
} else {
stopIndex = len(n.items)
}
for i := index; i < stopIndex; i++ {
if len(n.children) > 0 {
if hit, ok = n.children[i].iterate(dir, start, stop, includeStart, hit, iter); !ok {
return hit, false
Expand All @@ -521,15 +526,12 @@ func (n *node) iterate(dir direction, start, stop Item, includeStart bool, hit b
continue
}
hit = true
if stop != nil && !n.items[i].Less(stop) {
return hit, false
}
if !iter(n.items[i]) {
return hit, false
}
}
if len(n.children) > 0 {
if hit, ok = n.children[len(n.children)-1].iterate(dir, start, stop, includeStart, hit, iter); !ok {
if hit, ok = n.children[stopIndex].iterate(dir, start, stop, includeStart, hit, iter); !ok {
return hit, false
}
}
Expand All @@ -542,6 +544,16 @@ func (n *node) iterate(dir direction, start, stop Item, includeStart bool, hit b
} else {
index = len(n.items) - 1
}

if stop != nil {
stopIndex, found = n.items.find(stop)
if found {
stopIndex += 1
}
} else {
stopIndex = 0
}

for i := index; i >= 0; i-- {
if start != nil && !n.items[i].Less(start) {
if !includeStart || hit || start.Less(n.items[i]) {
Expand All @@ -562,7 +574,7 @@ func (n *node) iterate(dir direction, start, stop Item, includeStart bool, hit b
}
}
if len(n.children) > 0 {
if hit, ok = n.children[0].iterate(dir, start, stop, includeStart, hit, iter); !ok {
if hit, ok = n.children[stopIndex].iterate(dir, start, stop, includeStart, hit, iter); !ok {
return hit, false
}
}
Expand Down
29 changes: 18 additions & 11 deletions btree_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,13 +499,18 @@ func empty[T any]() optionalItem[T] {
// "greaterThan" or "lessThan" queries.
func (n *node[T]) iterate(dir direction, start, stop optionalItem[T], includeStart bool, hit bool, iter ItemIteratorG[T]) (bool, bool) {
var ok, found bool
var index int
var index, stopIndex int
switch dir {
case ascend:
if start.valid {
index, _ = n.items.find(start.item, n.cow.less)
}
for i := index; i < len(n.items); i++ {
if stop.valid {
stopIndex, _ = n.items.find(stop.item, n.cow.less)
} else {
stopIndex = len(n.items)
}
for i := index; i < stopIndex; i++ {
if len(n.children) > 0 {
if hit, ok = n.children[i].iterate(dir, start, stop, includeStart, hit, iter); !ok {
return hit, false
Expand All @@ -516,15 +521,12 @@ func (n *node[T]) iterate(dir direction, start, stop optionalItem[T], includeSta
continue
}
hit = true
if stop.valid && !n.cow.less(n.items[i], stop.item) {
return hit, false
}
if !iter(n.items[i]) {
return hit, false
}
}
if len(n.children) > 0 {
if hit, ok = n.children[len(n.children)-1].iterate(dir, start, stop, includeStart, hit, iter); !ok {
if hit, ok = n.children[stopIndex].iterate(dir, start, stop, includeStart, hit, iter); !ok {
return hit, false
}
}
Expand All @@ -537,7 +539,15 @@ func (n *node[T]) iterate(dir direction, start, stop optionalItem[T], includeSta
} else {
index = len(n.items) - 1
}
for i := index; i >= 0; i-- {
if stop.valid {
stopIndex, found = n.items.find(stop.item, n.cow.less)
if found {
stopIndex += 1
}
} else {
stopIndex = 0
}
for i := index; i >= stopIndex; i-- {
if start.valid && !n.cow.less(n.items[i], start.item) {
if !includeStart || hit || n.cow.less(start.item, n.items[i]) {
continue
Expand All @@ -548,16 +558,13 @@ func (n *node[T]) iterate(dir direction, start, stop optionalItem[T], includeSta
return hit, false
}
}
if stop.valid && !n.cow.less(stop.item, n.items[i]) {
return hit, false // continue
}
hit = true
if !iter(n.items[i]) {
return hit, false
}
}
if len(n.children) > 0 {
if hit, ok = n.children[0].iterate(dir, start, stop, includeStart, hit, iter); !ok {
if hit, ok = n.children[stopIndex].iterate(dir, start, stop, includeStart, hit, iter); !ok {
return hit, false
}
}
Expand Down