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

Remove some polymorphic comparison functions #15

Merged
merged 1 commit into from
Aug 10, 2023
Merged
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
40 changes: 20 additions & 20 deletions bench/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ Apple M1, 16GB RAM
```sh
$ ./main.exe
name, major-allocated, minor-allocated, monotonic-clock
rtree/find 1000, 1.115794, 5053.123394, 8706.211211
rtree/find 3000, 1.617212, 8113.245138, 14296.064470
rtree/find 10000, 14.722597, 53473.133420, 99040.530336
rtree/find 50000, 100.259289, 293706.271410, 578093.647431
rtree/find 100000, 97.256923, 230683.006154, 574174.680000
rtree/find_load 1000, 0.065361, 590.829372, 1496.124458
rtree/find_load 3000, 0.032275, 180.012953, 1617.546699
rtree/find_load 10000, 0.000000, 0.000000, 2112.512840
rtree/find_load 50000, 0.000000, 0.000000, 2521.168250
rtree/find_load 100000, 0.000000, 0.000000, 2960.819519
rtree/insert 1000, 16995.593277, 2027380.880112, 3442481.980392
rtree/insert 3000, 58014.712281, 6752241.512281, 11343033.196491
rtree/insert 10000, 254249.145455, 24960718.309091, 44963400.000000
rtree/insert 50000, 1219452.600000, 141399595.800000, 247627341.600000
rtree/insert 100000, 2503677.000000, 299104560.000000, 505670500.000000
rtree/load 1000, 17346.738657, 830784.857641, 1072496.556214
rtree/load 3000, 86004.129839, 3243578.500806, 4275447.099194
rtree/load 10000, 420853.892857, 13655703.514286, 18325598.164286
rtree/load 50000, 3801156.071429, 89015807.928571, 127242285.714286
rtree/load 100000, 9059167.800000, 200276210.800000, 294776508.600000
rtree/find 1000, 0.196829, 1510.251383, 3477.069655
rtree/find 3000, 0.292019, 2461.633463, 5968.352882
rtree/find 10000, 3.213499, 16037.302631, 54628.218437
rtree/find 50000, 30.334282, 85764.795164, 701816.897582
rtree/find 100000, 18.457143, 54300.421429, 1095782.164286
rtree/find_load 1000, 0.000000, 0.000000, 755.479508
rtree/find_load 3000, 0.000000, 0.000000, 734.867158
rtree/find_load 10000, 0.000000, 0.000000, 947.658157
rtree/find_load 50000, 0.000000, 0.000000, 1173.860563
rtree/find_load 100000, 0.000000, 0.000000, 1413.278226
rtree/insert 1000, 16995.593277, 2027380.880112, 3427902.889636
rtree/insert 3000, 58014.712281, 6752241.512281, 11247354.392982
rtree/insert 10000, 254249.145455, 24960718.309091, 42015527.236364
rtree/insert 50000, 1219452.600000, 141399595.800000, 234103050.200000
rtree/insert 100000, 2503677.000000, 299104560.000000, 491386041.000000
rtree/load 1000, 17346.738657, 830784.857641, 1072649.987626
rtree/load 3000, 86004.129839, 3243578.500806, 4281628.390323
rtree/load 10000, 420853.892857, 13655703.514286, 18343860.735714
rtree/load 50000, 3801156.071429, 89015807.928571, 127481654.785714
rtree/load 100000, 9059167.800000, 200276210.800000, 295105141.800000
```

Hopefully from these benchmarks you can see that `load` is a pretty good optimisation strategy
Expand Down
8 changes: 6 additions & 2 deletions src/rectangle.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ let v ~x0 ~y0 ~x1 ~y1 =
Array.Floatarray.unsafe_set arr 3 y1;
arr

let ranges_intersect a b a' b' = a' <= b && a <= b'
let ranges_intersect a b a' b' =
Float.compare a' b <= 0 && Float.compare a b' <= 0

let intersects arr arr' =
let x0, x0' = (x0 arr, x0 arr') in
Expand Down Expand Up @@ -70,7 +71,10 @@ let contains arr arr' =
let x1, x1' = (x1 arr, x1 arr') in
let y0, y0' = (y0 arr, y0 arr') in
let y1, y1' = (y1 arr, y1 arr') in
x0 <= x0' && x1 >= x1' && y0 <= y0' && y1 >= y1'
Float.compare x0 x0' <= 0
&& Float.compare x1 x1' >= 0
&& Float.compare y0 y0' <= 0
&& Float.compare y1 y1' >= 0

let empty =
let arr = Array.Floatarray.create 4 in
Expand Down
10 changes: 6 additions & 4 deletions src/rtree.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ module Make (E : Envelope) (V : Value with type envelope = E.t) = struct
| ((e', _) as n) :: ns ->
let enlargement = enlargement_needed e e' in
let min, maxs, enlargement' = partition_by_min_enlargement e ns in
if enlargement < enlargement' then (n, min :: maxs, enlargement)
if Float.compare enlargement enlargement' < 0 then
(n, min :: maxs, enlargement)
else (min, n :: maxs, enlargement')
| [] -> raise (Invalid_argument "cannot partition an empty node")

Expand All @@ -57,7 +58,8 @@ module Make (E : Envelope) (V : Value with type envelope = E.t) = struct
| ((n, n') as pair) :: ns ->
let max_cost', pair' = max_cost ns in
let cost = cost n n' in
if cost > max_cost' then (cost, pair) else (max_cost', pair')
if Float.compare cost max_cost' > 0 then (cost, pair)
else (max_cost', pair')
| [] -> raise (Invalid_argument "can't compute split on empty list")
in
let _, groups = max_cost pairs in
Expand All @@ -72,7 +74,7 @@ module Make (E : Envelope) (V : Value with type envelope = E.t) = struct
| n :: ns ->
let diff', n' = max_difference ns in
let diff = diff n in
if diff > diff' then (diff, n) else (diff', n')
if Float.compare diff diff' > 0 then (diff, n) else (diff', n')
| [] -> raise (Invalid_argument "can't compute max diff on empty list")
in
let _, n = max_difference ns in
Expand All @@ -86,7 +88,7 @@ module Make (E : Envelope) (V : Value with type envelope = E.t) = struct
let rest' = List.filter (( != ) n) rest in
let enlargement_x = enlargement_needed e xs_envelope in
let enlargement_y = enlargement_needed e ys_envelope in
if enlargement_x < enlargement_y then
if Float.compare enlargement_x enlargement_y < 0 then
partition (n :: xs) (E.merge xs_envelope e) ys ys_envelope rest'
else partition xs xs_envelope (n :: ys) (E.merge ys_envelope e) rest'
in
Expand Down