Skip to content

Commit dbff2fc

Browse files
committed
take advantage of inline attribute
1 parent d8d583d commit dbff2fc

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

Diff for: jscomp/others/belt_internalAVLset.ml

+8-6
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,23 @@ let rec copy n =
5050
match n with
5151
| None -> n
5252
| Some n ->
53-
Some { left = (copy n.left) ; right = (copy n.right);
53+
Some { left = copy n.left ; right = copy n.right;
5454
value = n.value; height = n.height}
5555

5656
(* Creates a new node with leftGet son l, value v and right son r.
5757
We must have all elements of l < v < all elements of r.
5858
l and r must be balanced and | treeHeight l - treeHeight r | <= 2.
5959
Inline expansion of treeHeight for better speed. *)
6060

61+
let [@inline] calcHeight (hl : int) hr =
62+
(if hl >= hr then hl else hr) + 1
63+
6164
let create (l : _ t) v (r : _ t) =
6265
let hl = height l in
6366
let hr = height r in
64-
Some { left = l; value = v; right = r; height = (if hl >= hr then hl + 1 else hr + 1)}
67+
Some { left = l; value = v; right = r; height = calcHeight hl hr}
68+
6569

66-
6770
let singleton x = Some { left = None; value = x; right = None; height = 1}
6871

6972
let heightGe l r =
@@ -77,8 +80,7 @@ let heightGe l r =
7780
where no rebalancing is required. *)
7881
(* TODO: inline all [create] operation, save duplicated [heightGet] calcuation *)
7982
let bal l v r =
80-
let hl = match l with None -> 0 | Some n -> n.height in
81-
let hr = match r with None -> 0 | Some n -> n.height in
83+
let hl,hr = height l, height r in
8284
if hl > hr + 2 then begin
8385
match l with None -> assert false | Some ({left = ll; right = lr} as l) ->
8486
if heightGe ll lr then
@@ -94,7 +96,7 @@ let bal l v r =
9496
match rl with None -> assert false | Some rl ->
9597
create (create l v rl.left) rl.value (create rl.right r.value rr)
9698
else
97-
Some {left = l ; value = v ; right = r; height = (if hl >= hr then hl + 1 else hr + 1)}
99+
Some {left = l ; value = v ; right = r; height = calcHeight hl hr}
98100

99101

100102

Diff for: lib/es6/belt_internalAVLset.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ function create(l, v, r) {
2222
var hr = r !== undefined ? r.h : 0;
2323
return {
2424
v: v,
25-
h: hl >= hr ? hl + 1 | 0 : hr + 1 | 0,
25+
h: (
26+
hl >= hr ? hl : hr
27+
) + 1 | 0,
2628
l: l,
2729
r: r
2830
};
@@ -64,7 +66,9 @@ function bal(l, v, r) {
6466
if (hr <= (hl + 2 | 0)) {
6567
return {
6668
v: v,
67-
h: hl >= hr ? hl + 1 | 0 : hr + 1 | 0,
69+
h: (
70+
hl >= hr ? hl : hr
71+
) + 1 | 0,
6872
l: l,
6973
r: r
7074
};
@@ -366,7 +370,7 @@ function checkInvariantInternal(_v) {
366370
r !== undefined ? r.h : 0
367371
) | 0;
368372
if (!(diff <= 2 && diff >= -2)) {
369-
throw new Error("File \"belt_internalAVLset.ml\", line 288, characters 6-12");
373+
throw new Error("File \"belt_internalAVLset.ml\", line 290, characters 6-12");
370374
}
371375
checkInvariantInternal(l);
372376
_v = r;

Diff for: lib/js/belt_internalAVLset.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ function create(l, v, r) {
2222
var hr = r !== undefined ? r.h : 0;
2323
return {
2424
v: v,
25-
h: hl >= hr ? hl + 1 | 0 : hr + 1 | 0,
25+
h: (
26+
hl >= hr ? hl : hr
27+
) + 1 | 0,
2628
l: l,
2729
r: r
2830
};
@@ -64,7 +66,9 @@ function bal(l, v, r) {
6466
if (hr <= (hl + 2 | 0)) {
6567
return {
6668
v: v,
67-
h: hl >= hr ? hl + 1 | 0 : hr + 1 | 0,
69+
h: (
70+
hl >= hr ? hl : hr
71+
) + 1 | 0,
6872
l: l,
6973
r: r
7074
};
@@ -366,7 +370,7 @@ function checkInvariantInternal(_v) {
366370
r !== undefined ? r.h : 0
367371
) | 0;
368372
if (!(diff <= 2 && diff >= -2)) {
369-
throw new Error("File \"belt_internalAVLset.ml\", line 288, characters 6-12");
373+
throw new Error("File \"belt_internalAVLset.ml\", line 290, characters 6-12");
370374
}
371375
checkInvariantInternal(l);
372376
_v = r;

0 commit comments

Comments
 (0)