Skip to content

Commit

Permalink
Try to call Less() fewer times when iterating
Browse files Browse the repository at this point in the history
Signed-off-by: Sylvain Rabot <[email protected]>
  • Loading branch information
a180285 authored and sylr committed Nov 24, 2023
1 parent d12ac89 commit 90e7ce7
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 11 deletions.
194 changes: 194 additions & 0 deletions atomic
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
mode: set
sylr.dev/btree/v2/btree.go:81.52,83.2 1 1
sylr.dev/btree/v2/btree.go:85.46,88.15 3 1
sylr.dev/btree/v2/btree.go:88.15,91.3 2 1
sylr.dev/btree/v2/btree.go:92.2,96.8 5 1
sylr.dev/btree/v2/btree.go:99.55,101.39 2 1
sylr.dev/btree/v2/btree.go:101.39,104.3 2 1
sylr.dev/btree/v2/btree.go:105.2,106.8 2 1
sylr.dev/btree/v2/btree.go:120.43,122.2 1 1
sylr.dev/btree/v2/btree.go:125.71,126.17 1 1
sylr.dev/btree/v2/btree.go:126.17,127.22 1 0
sylr.dev/btree/v2/btree.go:129.2,132.3 1 1
sylr.dev/btree/v2/btree.go:140.48,143.21 3 1
sylr.dev/btree/v2/btree.go:143.21,145.3 1 1
sylr.dev/btree/v2/btree.go:146.2,146.20 1 1
sylr.dev/btree/v2/btree.go:151.42,158.2 6 1
sylr.dev/btree/v2/btree.go:161.34,168.2 6 1
sylr.dev/btree/v2/btree.go:172.40,176.36 4 1
sylr.dev/btree/v2/btree.go:176.36,178.3 1 1
sylr.dev/btree/v2/btree.go:184.56,185.44 1 1
sylr.dev/btree/v2/btree.go:185.44,187.3 1 1
sylr.dev/btree/v2/btree.go:188.2,188.33 1 1
sylr.dev/btree/v2/btree.go:188.33,190.3 1 1
sylr.dev/btree/v2/btree.go:191.2,191.17 1 1
sylr.dev/btree/v2/btree.go:194.39,197.25 2 0
sylr.dev/btree/v2/btree.go:197.25,199.3 1 0
sylr.dev/btree/v2/btree.go:201.2,201.11 1 0
sylr.dev/btree/v2/btree.go:215.39,216.50 1 0
sylr.dev/btree/v2/btree.go:219.39,222.14 2 0
sylr.dev/btree/v2/btree.go:222.14,224.3 1 0
sylr.dev/btree/v2/btree.go:226.2,226.20 1 0
sylr.dev/btree/v2/btree.go:226.20,228.3 1 0
sylr.dev/btree/v2/btree.go:229.2,229.23 1 0
sylr.dev/btree/v2/btree.go:229.23,231.3 1 0
sylr.dev/btree/v2/btree.go:233.2,233.11 1 0
sylr.dev/btree/v2/btree.go:239.46,244.25 5 1
sylr.dev/btree/v2/btree.go:244.25,247.3 2 1
sylr.dev/btree/v2/btree.go:248.2,248.19 1 1
sylr.dev/btree/v2/btree.go:253.57,254.41 1 1
sylr.dev/btree/v2/btree.go:254.41,256.3 1 1
sylr.dev/btree/v2/btree.go:257.2,261.13 5 1
sylr.dev/btree/v2/btree.go:267.62,269.11 2 1
sylr.dev/btree/v2/btree.go:269.11,273.3 3 1
sylr.dev/btree/v2/btree.go:274.2,274.26 1 1
sylr.dev/btree/v2/btree.go:274.26,277.3 2 1
sylr.dev/btree/v2/btree.go:278.2,278.36 1 1
sylr.dev/btree/v2/btree.go:278.36,280.10 2 1
sylr.dev/btree/v2/btree.go:281.26,281.26 0 1
sylr.dev/btree/v2/btree.go:283.26,284.7 1 1
sylr.dev/btree/v2/btree.go:285.11,288.20 3 0
sylr.dev/btree/v2/btree.go:291.2,291.45 1 1
sylr.dev/btree/v2/btree.go:295.44,297.11 2 1
sylr.dev/btree/v2/btree.go:297.11,299.3 1 1
sylr.dev/btree/v2/btree.go:299.8,299.32 1 1
sylr.dev/btree/v2/btree.go:299.32,301.3 1 0
sylr.dev/btree/v2/btree.go:302.2,302.8 1 1
sylr.dev/btree/v2/btree.go:306.51,307.14 1 1
sylr.dev/btree/v2/btree.go:307.14,309.3 1 1
sylr.dev/btree/v2/btree.go:310.2,310.26 1 1
sylr.dev/btree/v2/btree.go:310.26,312.3 1 1
sylr.dev/btree/v2/btree.go:313.2,313.23 1 1
sylr.dev/btree/v2/btree.go:313.23,315.3 1 1
sylr.dev/btree/v2/btree.go:316.2,316.25 1 1
sylr.dev/btree/v2/btree.go:320.51,321.14 1 1
sylr.dev/btree/v2/btree.go:321.14,323.3 1 1
sylr.dev/btree/v2/btree.go:324.2,324.26 1 1
sylr.dev/btree/v2/btree.go:324.26,326.3 1 1
sylr.dev/btree/v2/btree.go:327.2,327.23 1 1
sylr.dev/btree/v2/btree.go:327.23,329.3 1 1
sylr.dev/btree/v2/btree.go:330.2,330.38 1 1
sylr.dev/btree/v2/btree.go:343.76,346.13 3 1
sylr.dev/btree/v2/btree.go:347.17,348.27 1 1
sylr.dev/btree/v2/btree.go:348.27,350.4 1 1
sylr.dev/btree/v2/btree.go:351.3,351.19 1 1
sylr.dev/btree/v2/btree.go:352.17,353.27 1 1
sylr.dev/btree/v2/btree.go:353.27,355.4 1 1
sylr.dev/btree/v2/btree.go:356.3,356.8 1 1
sylr.dev/btree/v2/btree.go:357.18,359.27 2 1
sylr.dev/btree/v2/btree.go:359.27,360.13 1 1
sylr.dev/btree/v2/btree.go:360.13,362.5 1 1
sylr.dev/btree/v2/btree.go:363.4,363.10 1 1
sylr.dev/btree/v2/btree.go:365.10,366.24 1 0
sylr.dev/btree/v2/btree.go:369.2,369.42 1 1
sylr.dev/btree/v2/btree.go:369.42,371.3 1 1
sylr.dev/btree/v2/btree.go:372.2,376.11 2 1
sylr.dev/btree/v2/btree.go:376.11,386.3 4 1
sylr.dev/btree/v2/btree.go:389.2,389.42 1 1
sylr.dev/btree/v2/btree.go:416.91,417.52 1 1
sylr.dev/btree/v2/btree.go:417.52,424.34 6 1
sylr.dev/btree/v2/btree.go:424.34,426.4 1 1
sylr.dev/btree/v2/btree.go:427.8,427.70 1 1
sylr.dev/btree/v2/btree.go:427.70,434.34 6 1
sylr.dev/btree/v2/btree.go:434.34,436.4 1 1
sylr.dev/btree/v2/btree.go:437.8,438.24 1 1
sylr.dev/btree/v2/btree.go:438.24,440.4 1 1
sylr.dev/btree/v2/btree.go:441.3,448.27 7 1
sylr.dev/btree/v2/btree.go:450.2,450.38 1 1
sylr.dev/btree/v2/btree.go:465.50,467.2 1 1
sylr.dev/btree/v2/btree.go:468.41,470.2 1 1
sylr.dev/btree/v2/btree.go:479.135,482.13 3 1
sylr.dev/btree/v2/btree.go:483.14,484.17 1 1
sylr.dev/btree/v2/btree.go:484.17,486.4 1 1
sylr.dev/btree/v2/btree.go:486.9,488.4 1 1
sylr.dev/btree/v2/btree.go:489.3,489.38 1 1
sylr.dev/btree/v2/btree.go:489.38,490.27 1 1
sylr.dev/btree/v2/btree.go:490.27,491.88 1 1
sylr.dev/btree/v2/btree.go:491.88,493.6 1 1
sylr.dev/btree/v2/btree.go:495.4,495.76 1 1
sylr.dev/btree/v2/btree.go:495.76,497.13 2 0
sylr.dev/btree/v2/btree.go:499.4,500.25 2 1
sylr.dev/btree/v2/btree.go:500.25,502.5 1 1
sylr.dev/btree/v2/btree.go:504.3,504.26 1 1
sylr.dev/btree/v2/btree.go:504.26,505.95 1 1
sylr.dev/btree/v2/btree.go:505.95,507.5 1 0
sylr.dev/btree/v2/btree.go:509.15,510.18 1 1
sylr.dev/btree/v2/btree.go:510.18,512.14 2 1
sylr.dev/btree/v2/btree.go:512.14,514.5 1 1
sylr.dev/btree/v2/btree.go:515.9,517.4 1 1
sylr.dev/btree/v2/btree.go:518.3,518.17 1 1
sylr.dev/btree/v2/btree.go:518.17,520.13 2 1
sylr.dev/btree/v2/btree.go:520.13,522.5 1 1
sylr.dev/btree/v2/btree.go:523.9,525.4 1 1
sylr.dev/btree/v2/btree.go:526.3,526.39 1 1
sylr.dev/btree/v2/btree.go:526.39,527.51 1 1
sylr.dev/btree/v2/btree.go:527.51,528.60 1 1
sylr.dev/btree/v2/btree.go:528.60,529.14 1 0
sylr.dev/btree/v2/btree.go:532.4,532.27 1 1
sylr.dev/btree/v2/btree.go:532.27,533.90 1 1
sylr.dev/btree/v2/btree.go:533.90,535.6 1 1
sylr.dev/btree/v2/btree.go:537.4,538.25 2 1
sylr.dev/btree/v2/btree.go:538.25,540.5 1 1
sylr.dev/btree/v2/btree.go:542.3,542.26 1 1
sylr.dev/btree/v2/btree.go:542.26,543.95 1 1
sylr.dev/btree/v2/btree.go:543.95,545.5 1 1
sylr.dev/btree/v2/btree.go:548.2,548.18 1 1
sylr.dev/btree/v2/btree.go:554.49,556.31 2 0
sylr.dev/btree/v2/btree.go:556.31,558.3 1 0
sylr.dev/btree/v2/btree.go:575.64,576.32 1 0
sylr.dev/btree/v2/btree.go:576.32,578.3 1 0
sylr.dev/btree/v2/btree.go:580.2,580.9 1 0
sylr.dev/btree/v2/btree.go:583.41,591.2 5 0
sylr.dev/btree/v2/btree.go:598.35,600.2 1 1
sylr.dev/btree/v2/btree.go:604.35,606.2 1 1
sylr.dev/btree/v2/btree.go:608.43,612.2 3 1
sylr.dev/btree/v2/btree.go:614.41,620.2 4 1
sylr.dev/btree/v2/btree.go:627.58,628.19 1 1
sylr.dev/btree/v2/btree.go:628.19,633.3 4 1
sylr.dev/btree/v2/btree.go:633.8,634.40 1 1
sylr.dev/btree/v2/btree.go:634.40,640.4 5 1
sylr.dev/btree/v2/btree.go:642.2,643.11 2 1
sylr.dev/btree/v2/btree.go:643.11,645.3 1 1
sylr.dev/btree/v2/btree.go:646.2,646.18 1 1
sylr.dev/btree/v2/btree.go:651.45,653.2 1 1
sylr.dev/btree/v2/btree.go:657.42,660.2 2 1
sylr.dev/btree/v2/btree.go:664.42,667.2 2 1
sylr.dev/btree/v2/btree.go:669.67,670.45 1 1
sylr.dev/btree/v2/btree.go:670.45,672.3 1 1
sylr.dev/btree/v2/btree.go:673.2,674.56 2 1
sylr.dev/btree/v2/btree.go:674.56,678.3 3 1
sylr.dev/btree/v2/btree.go:679.2,679.10 1 1
sylr.dev/btree/v2/btree.go:679.10,681.3 1 1
sylr.dev/btree/v2/btree.go:682.2,682.18 1 1
sylr.dev/btree/v2/btree.go:687.86,688.19 1 1
sylr.dev/btree/v2/btree.go:688.19,690.3 1 0
sylr.dev/btree/v2/btree.go:691.2,691.93 1 1
sylr.dev/btree/v2/btree.go:696.70,697.19 1 1
sylr.dev/btree/v2/btree.go:697.19,699.3 1 0
sylr.dev/btree/v2/btree.go:700.2,700.77 1 1
sylr.dev/btree/v2/btree.go:705.76,706.19 1 1
sylr.dev/btree/v2/btree.go:706.19,708.3 1 0
sylr.dev/btree/v2/btree.go:709.2,709.76 1 1
sylr.dev/btree/v2/btree.go:714.53,715.19 1 1
sylr.dev/btree/v2/btree.go:715.19,717.3 1 0
sylr.dev/btree/v2/btree.go:718.2,718.72 1 1
sylr.dev/btree/v2/btree.go:723.87,724.19 1 1
sylr.dev/btree/v2/btree.go:724.19,726.3 1 0
sylr.dev/btree/v2/btree.go:727.2,727.94 1 1
sylr.dev/btree/v2/btree.go:732.74,733.19 1 1
sylr.dev/btree/v2/btree.go:733.19,735.3 1 0
sylr.dev/btree/v2/btree.go:736.2,736.77 1 1
sylr.dev/btree/v2/btree.go:741.74,742.19 1 1
sylr.dev/btree/v2/btree.go:742.19,744.3 1 0
sylr.dev/btree/v2/btree.go:745.2,745.78 1 1
sylr.dev/btree/v2/btree.go:750.54,751.19 1 1
sylr.dev/btree/v2/btree.go:751.19,753.3 1 0
sylr.dev/btree/v2/btree.go:754.2,754.73 1 1
sylr.dev/btree/v2/btree.go:759.45,760.19 1 1
sylr.dev/btree/v2/btree.go:760.19,762.3 1 0
sylr.dev/btree/v2/btree.go:763.2,763.24 1 1
sylr.dev/btree/v2/btree.go:767.40,769.2 1 1
sylr.dev/btree/v2/btree.go:772.40,774.2 1 1
sylr.dev/btree/v2/btree.go:777.36,780.2 2 0
sylr.dev/btree/v2/btree.go:783.30,785.2 1 1
sylr.dev/btree/v2/btree.go:808.51,810.2 1 0
29 changes: 18 additions & 11 deletions btree.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,13 +478,18 @@ func empty[T Item[T]]() optionalItem[T] {
// "greaterThan" or "lessThan" queries.
func (n *node[T]) iterate(dir direction, start, stop optionalItem[T], includeStart bool, hit bool, iter ItemIterator[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)
}
for i := index; i < len(n.items); i++ {
if stop.valid {
stopIndex, _ = n.items.find(stop.item)
} 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 @@ -495,15 +500,12 @@ func (n *node[T]) iterate(dir direction, start, stop optionalItem[T], includeSta
continue
}
hit = true
if stop.valid && !n.items[i].Less(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 @@ -516,7 +518,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)
if found {
stopIndex += 1
}
} else {
stopIndex = 0
}
for i := index; i >= stopIndex; i-- {
if start.valid && !n.items[i].Less(start.item) {
if !includeStart || hit || start.item.Less(n.items[i]) {
continue
Expand All @@ -527,16 +537,13 @@ func (n *node[T]) iterate(dir direction, start, stop optionalItem[T], includeSta
return hit, false
}
}
if stop.valid && !stop.item.Less(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

0 comments on commit 90e7ce7

Please sign in to comment.