Skip to content

Commit 2ff47b7

Browse files
committed
Merge pull request #49 from bloomberg/small_inline_continue
apply heruisitics for small functions
2 parents bc5023b + 0c733dd commit 2ff47b7

File tree

101 files changed

+2703
-2087
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+2703
-2087
lines changed

jscomp/compiler.mllib

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ ext_pp_scope
2525
ext_array
2626
ext_bytes
2727
ext_map
28+
ext_hashtbl
2829

30+
lam_mk
2931
lam_compile_env
3032
lam_dispatch_primitive
3133
lam_stats
@@ -56,6 +58,7 @@ lam_fold
5658
lam_register
5759
lam_analysis
5860
lam_group
61+
lam_current_unit
5962

6063
j
6164
js_program_loader

jscomp/ext_hashtbl.ml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
(* OCamlScript compiler
2+
* Copyright (C) 2015-2016 Bloomberg Finance L.P.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, with linking exception;
7+
* either version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17+
*)
18+
19+
(* Author: Hongbo Zhang *)
20+
21+
let of_list kvs =
22+
let map = Hashtbl.create 51 in
23+
List.iter (fun (k, v) -> Hashtbl.add map k v) kvs ;
24+
map
25+
26+
27+
let of_list2 ks vs =
28+
let map = Hashtbl.create 51 in
29+
List.iter2 (fun k v -> Hashtbl.add map k v) ks vs ;
30+
map
31+
32+
let add_list map kvs =
33+
List.iter (fun (k, v) -> Hashtbl.add map k v) kvs

jscomp/ext_hashtbl.mli

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
(* OCamlScript compiler
2+
* Copyright (C) 2015-2016 Bloomberg Finance L.P.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, with linking exception;
7+
* either version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17+
*)
18+
19+
(* Author: Hongbo Zhang *)
20+
21+
22+
val of_list : ('a * 'b) list -> ('a, 'b) Hashtbl.t
23+
24+
val of_list2 : 'a list -> 'b list -> ('a, 'b) Hashtbl.t
25+
26+
val add_list : ('a, 'b) Hashtbl.t -> ('a * 'b) list -> unit

jscomp/ext_ident.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ let convert (name : string) =
216216
| '@' -> Buffer.add_string buffer "$at"
217217
| '^' -> Buffer.add_string buffer "$caret"
218218
| '/' -> Buffer.add_string buffer "$slash"
219+
| '|' -> Buffer.add_string buffer "$pipe"
219220
| '.' -> Buffer.add_string buffer "$dot"
220221
| 'a'..'z' | 'A'..'Z'| '_'|'$' |'0'..'9'-> Buffer.add_char buffer c
221222
| _ -> Buffer.add_string buffer "$unknown"

jscomp/ext_log.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,20 @@ let ierr b str f v =
3636
let warn str f v =
3737
Format.fprintf Format.err_formatter ("WARN: %s " ^^ f) str v
3838

39+
40+
3941
let iwarn b str f v =
4042
if b then
4143
Format.fprintf Format.err_formatter ("WARN: %s " ^^ f) str v
4244
else
4345
Format.ifprintf Format.err_formatter ("WARN: %s " ^^ f) str v
4446

47+
let dwarn str f v =
48+
if Lam_current_unit.is_same_file () then
49+
Format.fprintf Format.err_formatter ("WARN: %s " ^^ f) str v
50+
else
51+
Format.ifprintf Format.err_formatter ("WARN: %s " ^^ f) str v
52+
4553
let info str f v =
4654
Format.fprintf Format.err_formatter ("INFO: %s " ^^ f) str v
4755

jscomp/ext_log.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@ val err : string -> ('a,'b) logging
3535
val ierr : bool -> string -> ('a,'b) logging
3636
val warn : string -> ('a,'b) logging
3737
val iwarn : bool -> string -> ('a,'b) logging
38+
val dwarn : string -> ('a,'b) logging
3839
val info : string -> ('a,'b) logging
3940
val iinfo : bool -> string -> ('a,'b) logging

jscomp/j.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ and vident =
6565
just print `A.length` - it's guarateed to be unique
6666
6767
when the third one is None, it means the whole module
68+
69+
TODO:
70+
invariant, when [kind] is [Runtime], then we can ignore [ident],
71+
since all [runtime] functions are unique, when do the
72+
pattern match we can ignore the first one for simplicity
73+
for example
74+
{[
75+
Qualified (_, Runtime, Some "caml_int_compare")
76+
]}
6877
*)
6978

7079
and exception_ident = ident

jscomp/js_fold.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ class virtual fold =
108108
just print `A.length` - it's guarateed to be unique
109109
110110
when the third one is None, it means the whole module
111+
112+
TODO:
113+
invariant, when [kind] is [Runtime], then we can ignore [ident],
114+
since all [runtime] functions are unique, when do the
115+
pattern match we can ignore the first one for simplicity
116+
for example
117+
{[
118+
Qualified (_, Runtime, Some "caml_int_compare")
119+
]}
111120
*)
112121
(* used in [js_create_array] primitive, note having
113122
uninitilized array is not as bad as in ocaml,

jscomp/js_helper.ml

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ module Exp = struct
412412
true_
413413
| _ ->
414414
to_ocaml_boolean {expression_desc = Bin(EqEqEq, e0,e1); comment}
415+
let int_equal = float_equal
415416
let rec string_equal ?comment (e0 : t) (e1 : t) : t =
416417
match e0.expression_desc, e1.expression_desc with
417418
| Str (_, a0), Str(_, b0)
@@ -514,27 +515,44 @@ module Exp = struct
514515
bit different from Javascript, so that we can change it in the future
515516
*)
516517
let rec and_ ?comment (e1 : t) (e2 : t) =
517-
match e1, e2 with
518-
| {expression_desc = Int_of_boolean e1;_} ,
519-
{expression_desc = Int_of_boolean e2;_} ->
518+
match e1.expression_desc, e2.expression_desc with
519+
| Int_of_boolean e1 , Int_of_boolean e2 ->
520520
and_ ?comment e1 e2
521-
| {expression_desc = Int_of_boolean e1; _} ,
522-
e2 -> and_ ?comment e1 e2
523-
| e1, {expression_desc = Int_of_boolean e2;_}
521+
| Int_of_boolean e1 , _ -> and_ ?comment e1 e2
522+
| _, Int_of_boolean e2
524523
-> and_ ?comment e1 e2
525-
| e1, e2 ->
524+
(* optimization if [e1 = e2], then and_ e1 e2 -> e2
525+
be careful for side effect
526+
*)
527+
| Var i, Var j when Js_op_util.same_vident i j
528+
->
529+
to_ocaml_boolean e1
530+
| Var i,
531+
(Bin (And, {expression_desc = Var j ; _}, _)
532+
| Bin (And , _, {expression_desc = Var j ; _}))
533+
when Js_op_util.same_vident i j
534+
->
535+
to_ocaml_boolean e2
536+
| _, _ ->
526537
to_ocaml_boolean @@ bin ?comment And e1 e2
527538

528539
let rec or_ ?comment (e1 : t) (e2 : t) =
529-
match e1, e2 with
530-
| {expression_desc = Int_of_boolean e1;_} ,
531-
{expression_desc = Int_of_boolean e2;_} ->
540+
match e1.expression_desc, e2.expression_desc with
541+
| Int_of_boolean e1 , Int_of_boolean e2
542+
->
532543
or_ ?comment e1 e2
533-
| {expression_desc = Int_of_boolean e1;_} ,
534-
e2 -> or_ ?comment e1 e2
535-
| e1, {expression_desc = Int_of_boolean e2;_}
544+
| Int_of_boolean e1 , _ -> or_ ?comment e1 e2
545+
| _, Int_of_boolean e2
536546
-> or_ ?comment e1 e2
537-
| e1, e2 ->
547+
| Var i, Var j when Js_op_util.same_vident i j
548+
->
549+
to_ocaml_boolean e1
550+
| Var i,
551+
(Bin (Or, {expression_desc = Var j ; _}, _)
552+
| Bin (Or , _, {expression_desc = Var j ; _}))
553+
when Js_op_util.same_vident i j
554+
-> to_ocaml_boolean e2
555+
| _, _ ->
538556
to_ocaml_boolean @@ bin ?comment Or e1 e2
539557

540558
let string_of_small_int_array ?comment xs : t =
@@ -594,8 +612,21 @@ module Exp = struct
594612

595613
let string_comp cmp ?comment e0 e1 =
596614
to_ocaml_boolean @@ bin ?comment cmp e0 e1
597-
let int_comp cmp ?comment e0 e1 =
598-
to_ocaml_boolean @@ bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1
615+
616+
617+
let rec int_comp (cmp : Lambda.comparison) ?comment (e0 : t) (e1 : t) =
618+
match cmp, e0.expression_desc, e1.expression_desc with
619+
| _, Call ({
620+
expression_desc =
621+
Var (Qualified
622+
(_, Runtime,
623+
Some ("caml_int_compare" | "caml_int32_compare"))); _},
624+
[l;r], _),
625+
Number (Int {i = 0})
626+
-> int_comp cmp l r (* = 0 > 0 < 0 *)
627+
| _ ->
628+
to_ocaml_boolean @@ bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1
629+
599630
let float_comp cmp ?comment e0 e1 =
600631
to_ocaml_boolean @@ bin ?comment (Lam_compile_util.jsop_of_comp cmp) e0 e1
601632

jscomp/js_helper.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ module Exp : sig
145145
val assign : binary_op
146146

147147
val triple_equal : binary_op
148+
(* TODO: reduce [triple_equal] use *)
149+
148150
val float_equal : binary_op
151+
val int_equal : binary_op
149152
val string_equal : binary_op
150153
val is_type_number : unary_op
151154
val typeof : unary_op

0 commit comments

Comments
 (0)