Skip to content

Commit 0b088ec

Browse files
committed
re-org primitive comparisons
1 parent 8a76d82 commit 0b088ec

18 files changed

+138
-121
lines changed

jscomp/core/js_runtime_modules.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ let hash = "Caml_hash"
3838
let oo = "Caml_oo"
3939
let curry = "Curry"
4040
let caml_oo_curry = "Caml_oo_curry"
41+
let caml_primitive = "Caml_primitive"
4142
let int64 = "Caml_int64"
4243
let md5 = "Caml_md5"
4344
let weak = "Caml_weak"

jscomp/core/lam_dispatch_primitive.ml

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ let translate loc (prim_name : string)
317317
| "caml_modf_float"
318318
| "caml_ldexp_float"
319319
| "caml_frexp_float"
320-
| "caml_float_compare"
320+
321321
| "caml_copysign_float"
322322
| "caml_expm1_float"
323323
| "caml_hypot_float"
@@ -392,8 +392,45 @@ let translate loc (prim_name : string)
392392
call Js_runtime_modules.string
393393
end
394394

395-
| "caml_string_get"
396-
| "caml_string_compare"
395+
| "caml_int_compare"
396+
| "caml_int32_compare"
397+
| "caml_nativeint_compare"
398+
| "caml_float_compare"
399+
| "caml_string_compare"
400+
->
401+
call Js_runtime_modules.caml_primitive
402+
403+
| "caml_int_min"
404+
| "caml_float_min"
405+
| "caml_string_min"
406+
| "caml_nativeint_min"
407+
| "caml_int32_min"
408+
409+
->
410+
begin match args with
411+
| [a;b] ->
412+
if Js_analyzer.is_okay_to_duplicate a && Js_analyzer.is_okay_to_duplicate b then
413+
E.econd (E.js_comp Clt a b) a b
414+
else
415+
call Js_runtime_modules.caml_primitive
416+
| _ -> assert false
417+
end
418+
| "caml_int_max"
419+
| "caml_float_max"
420+
| "caml_string_max"
421+
| "caml_nativeint_max"
422+
| "caml_int32_max"
423+
->
424+
begin match args with
425+
| [a;b] ->
426+
if Js_analyzer.is_okay_to_duplicate a && Js_analyzer.is_okay_to_duplicate b then
427+
E.econd (E.js_comp Cgt a b) a b
428+
else
429+
call Js_runtime_modules.caml_primitive
430+
| _ -> assert false
431+
end
432+
433+
| "caml_string_get"
397434
| "string_of_bytes"
398435
| "bytes_of_string"
399436

@@ -579,43 +616,11 @@ let translate loc (prim_name : string)
579616
| _ -> assert false
580617
end
581618

582-
| "caml_int_min"
583-
| "caml_float_min"
584-
| "caml_string_min"
585-
| "caml_nativeint_min"
586-
| "caml_int32_min"
587-
588-
->
589-
begin match args with
590-
| [a;b] ->
591-
if Js_analyzer.is_okay_to_duplicate a && Js_analyzer.is_okay_to_duplicate b then
592-
E.econd (E.js_comp Clt a b) a b
593-
else
594-
call Js_runtime_modules.obj_runtime
595-
| _ -> assert false
596-
end
597-
| "caml_int_max"
598-
| "caml_float_max"
599-
| "caml_string_max"
600-
| "caml_nativeint_max"
601-
| "caml_int32_max"
602-
->
603-
begin match args with
604-
| [a;b] ->
605-
if Js_analyzer.is_okay_to_duplicate a && Js_analyzer.is_okay_to_duplicate b then
606-
E.econd (E.js_comp Cgt a b) a b
607-
else
608-
call Js_runtime_modules.obj_runtime
609-
| _ -> assert false
610-
end
611619

612620
| "caml_obj_dup"
613621
| "caml_update_dummy"
614622
| "caml_obj_truncate"
615623
| "caml_lazy_make_forward"
616-
| "caml_int_compare"
617-
| "caml_int32_compare"
618-
| "caml_nativeint_compare"
619624
->
620625
call Js_runtime_modules.obj_runtime
621626

jscomp/others/.depend

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ bs_internalAVLtree.cmj :
2222
bs_internalMutableAVLSet.cmj : bs_internalAVLset.cmj
2323
bs_Hash.cmj : bs_Hash.cmi
2424
bs_Queue.cmj : bs_Array.cmj bs_Queue.cmi
25-
bs_List.cmj : bs_Array.cmj bs_List.cmi
25+
bs_List.cmj : js_json.cmj bs_Array.cmj bs_List.cmi
2626
bs_internalBucketsType.cmj : bs_Array.cmj
2727
bs_internalSetBuckets.cmj : bs_internalBucketsType.cmj bs_Array.cmj bs.cmj
2828
bs_internalBuckets.cmj : bs_internalBucketsType.cmj bs_Array.cmj
@@ -69,7 +69,7 @@ js_mapperRt.cmi :
6969
bs_Array.cmi :
7070
bs_Hash.cmi :
7171
bs_Queue.cmi :
72-
bs_List.cmi :
72+
bs_List.cmi : js_json.cmi
7373
bs_HashMap.cmi : bs_Hash.cmi bs_Bag.cmj
7474
bs_HashSet.cmi : bs_Hash.cmi bs_Bag.cmj
7575
bs_HashSetString.cmi :

jscomp/others/bs_List.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,12 +358,13 @@ let rec fillAuxMap arr i x f =
358358
Bs_Array.unsafe_set arr i (f h [@bs]) ;
359359
fillAuxMap arr (i + 1) t f
360360

361-
module J = Js.Json
361+
module J = Js_json
362+
type json = J.t
362363
let toJson x f =
363364
let len = length x in
364365
let arr = Bs_Array.makeUninitializedUnsafe len in
365366
fillAuxMap arr 0 x f;
366-
Js.Json.array arr
367+
J.array arr
367368

368369
(* TODO: best practice about raising excpetion
369370
1. raise OCaml exception, no stacktrace

jscomp/others/bs_List.mli

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ val length : 'a t -> int
5252

5353
val toArray : 'a t -> 'a array
5454

55-
val toJson : 'a t -> ('a -> Js.Json.t [@bs]) -> Js.Json.t
56-
val fromJson : Js.Json.t -> (Js.Json.t -> 'a [@bs]) -> 'a t
55+
type json = Js_json.t
56+
57+
val toJson : 'a t -> ('a -> json [@bs]) -> json
58+
val fromJson : json -> (json -> 'a [@bs]) -> 'a t
5759

5860
val revAppend : 'a t -> 'a t -> 'a t
5961

jscomp/others/bs_internalBuckets.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ let rec bucket_length accu buckets =
4646
| None -> accu
4747
| Some cell -> bucket_length (accu + 1) (next cell)
4848

49-
let max (m : int) n = if m > n then m else n
5049

5150

5251
let rec do_bucket_iter ~f buckets =
@@ -83,7 +82,9 @@ let fold0 f h init =
8382

8483
let logStats0 h =
8584
let mbl =
86-
Bs_Array.foldLeft (fun[@bs] m b -> max m (bucket_length 0 b)) 0 (C.buckets h) in
85+
Bs_Array.foldLeft (fun[@bs] m b ->
86+
let len = (bucket_length 0 b) in
87+
Pervasives.max m len) 0 (C.buckets h) in
8788
let histo = Bs_Array.make (mbl + 1) 0 in
8889
Bs_Array.iter
8990
(fun[@bs] b ->

jscomp/others/bs_internalMutableAVLSet.ml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type ('elt,'id) t0 = 'elt node Js.null
88

99
external unsafeCoerce : 'a Js.null -> 'a = "%identity"
1010

11-
let maxInt (x : int) y = if x > y then x else y
11+
1212

1313
let empty = N.empty0
1414
let isEmpty = N.isEmpty0
@@ -31,17 +31,21 @@ let rotateWithLeftChild k2 =
3131
let k1 = unsafeCoerce (N.left k2) in
3232
N.(leftSet k2 (right k1));
3333
N.(rightSet k1 (return k2 ));
34+
let hlk2, hrk2 = N.(height (left k2), (height (right k2))) in
3435
N.(hSet k2
35-
(maxInt (height (left k2)) (height (right k2)) + 1));
36-
N.(hSet k1 (maxInt (height (left k1)) (h k2) + 1));
36+
(Pervasives.max hlk2 hrk2 + 1));
37+
let hlk1, hk2 = N.(height (left k1), (h k2)) in
38+
N.(hSet k1 (Pervasives.max hlk1 hk2 + 1));
3739
k1
3840
(* right rotation *)
3941
let rotateWithRightChild k1 =
4042
let k2 = unsafeCoerce (N.right k1) in
4143
N.(rightSet k1 (left k2));
4244
N.(leftSet k2 (return k1));
43-
N.(hSet k1 (maxInt (height (left k1)) (height (right k1)) + 1));
44-
N.(hSet k2 (maxInt (height (right k2)) (h k1) + 1));
45+
let hlk1, hrk1 = N.((height (left k1)), (height (right k1))) in
46+
N.(hSet k1 (Pervasives.max hlk1 hrk1 + 1));
47+
let hrk2, hk1 = N.(height (right k2), (h k1)) in
48+
N.(hSet k2 (Pervasives.max hrk2 hk1 + 1));
4549
k2
4650

4751
(*
@@ -91,7 +95,8 @@ let rec add (x : key) (t : _ t0) =
9195
)
9296
end
9397
) in
98+
let hlt, hrt = N.(height (left t),(height (right t))) in
9499
N.hSet t
95-
N.(maxInt (height (left t)) (height (right t)) + 1);
100+
N.(Pervasives.max hlt hrt + 1);
96101
N.return t
97102
end

jscomp/others/bs_internalSetBuckets.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ let rec bucket_length accu buckets =
4343
| None -> accu
4444
| Some cell -> bucket_length (accu + 1) (next cell)
4545

46-
let max (m : int) n = if m > n then m else n
4746

4847

4948
let rec do_bucket_iter ~f buckets =
@@ -100,7 +99,9 @@ let fold0 f h init =
10099

101100
let logStats0 h =
102101
let mbl =
103-
Bs_Array.foldLeft (fun[@bs] m b -> max m (bucket_length 0 b)) 0 (C.buckets h) in
102+
Bs_Array.foldLeft (fun[@bs] m b ->
103+
let len = (bucket_length 0 b) in
104+
max m len) 0 (C.buckets h) in
104105
let histo = Bs_Array.make (mbl + 1) 0 in
105106
Bs_Array.iter
106107
(fun[@bs] b ->

jscomp/runtime/.depend

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ caml_io.cmj : js_undefined.cmj js.cmj bs_string.cmj
1111
caml_float.cmj : js_typed_array.cmj js_float.cmj caml_float.cmi
1212
caml_lexer.cmj : caml_lexer.cmi
1313
caml_parser.cmj : caml_parser.cmi
14-
caml_primitive.cmj : caml_primitive.cmi
1514
caml_format.cmj : js_nativeint.cmj js_int64.cmj js_float.cmj caml_utils.cmj \
1615
bs_string.cmj caml_format.cmi
1716
caml_md5.cmj : bs_string.cmj caml_md5.cmi
@@ -48,7 +47,6 @@ caml_sys.cmi :
4847
caml_float.cmi :
4948
caml_lexer.cmi :
5049
caml_parser.cmi :
51-
caml_primitive.cmi :
5250
caml_format.cmi :
5351
caml_md5.cmi :
5452
caml_queue.cmi :

jscomp/runtime/Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ COMPILER=../../lib/bsc.exe
55
OTHERS= caml_array caml_string caml_bytes\
66
caml_obj caml_int64 \
77
caml_exceptions caml_utils caml_sys caml_io\
8-
caml_float caml_lexer caml_parser caml_primitive\
8+
caml_float caml_lexer caml_parser \
99
caml_format caml_md5 caml_queue caml_hash caml_weak\
1010
caml_backtrace caml_int32 caml_gc js_typed_array \
1111
js_primitive caml_basic caml_oo curry caml_oo_curry caml_module \
1212
caml_missing_polyfill\
1313
bs_string js_float js_exn bs_obj js_nativeint js_int js_null js_undefined
1414

15-
SOURCE_LIST= $(OTHERS) caml_builtin_exceptions block js js_unsafe js_internal
15+
SOURCE_LIST= $(OTHERS) caml_builtin_exceptions block js js_unsafe js_internal caml_primitive
1616

1717
caml_sys.cmj: js_undefined.cmj
1818
caml_oo.cmj : caml_array.cmj
@@ -23,10 +23,11 @@ caml_format.cmj: caml_int64.cmj caml_int32.cmj caml_utils.cmj caml_string.cmj
2323
caml_weak.cmj caml_module.cmj: caml_obj.cmj js_primitive.cmj
2424
caml_builtin_exceptions.cmj: caml_builtin_exceptions.cmi js_unsafe.cmi
2525
block.cmj: block.cmi
26+
caml_primitive.cmj: caml_primitive.cmi
2627
caml_int64.cmj : caml_obj.cmj
2728
# or we can do a post-processing to add missing cmj dependency manually
2829
js_exn.cmj : caml_exceptions.cmj
29-
$(addsuffix .cmj, $(OTHERS)): caml_builtin_exceptions.cmj block.cmj js.cmj js_unsafe.cmj
30+
$(addsuffix .cmj, $(OTHERS)): caml_builtin_exceptions.cmj block.cmj js.cmj js_unsafe.cmj caml_primitive.cmj
3031
$(addsuffix .cmj, $(OTHERS)) caml_builtin_exceptions.cmj block.cmj js.cmj js_unsafe.cmj : js_internal.cmi
3132
## since we use ppx
3233
$(addsuffix .cmi, $(OTHERS)): js.cmi js_unsafe.cmj js_unsafe.cmi js.cmj

0 commit comments

Comments
 (0)