Skip to content

Commit 28bd7e9

Browse files
committed
remove certainity which is useless
1 parent 99e81e2 commit 28bd7e9

File tree

5 files changed

+176
-204
lines changed

5 files changed

+176
-204
lines changed

jscomp/core/lam_arity.ml

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@
2424

2525

2626
type t =
27-
| Arity_info of bool * int list * bool
27+
| Arity_info of int list * bool
2828
(**
29-
when the first argument is true, it is for sure
3029
the last one means it can take any params later,
3130
for an exception: it is (Determin (true,[], true))
3231
1. approximation sound but not complete
@@ -39,12 +38,9 @@ let pp = Format.fprintf
3938
let print (fmt : Format.formatter) (x : t) =
4039
match x with
4140
| Arity_na -> pp fmt "?"
42-
| Arity_info (b,ls,tail) ->
41+
| Arity_info (ls,tail) ->
4342
begin
4443
pp fmt "@[";
45-
(if not b
46-
then
47-
pp fmt "~");
4844
pp fmt "[";
4945
Format.pp_print_list ~pp_sep:(fun fmt () -> pp fmt ",")
5046
(fun fmt x -> Format.pp_print_int fmt x)
@@ -68,58 +64,56 @@ let merge
6864
(n : int )
6965
(x : t) : t =
7066
match x with
71-
| Arity_na -> Arity_info (false, [n], false)
72-
| Arity_info (b,xs,tail) -> Arity_info (b, n :: xs, tail)
67+
| Arity_na -> Arity_info ( [n], false)
68+
| Arity_info (xs,tail) -> Arity_info ( n :: xs, tail)
7369

7470

7571
let non_function_arity_info =
76-
Arity_info (true, [], false)
72+
Arity_info ([], false)
7773

7874
let raise_arity_info =
79-
Arity_info (true,[],true)
75+
Arity_info ([],true)
8076

8177
let na = Arity_na
8278

83-
let info b0 args b1 =
84-
Arity_info (b0, args, b1)
79+
let info args b1 =
80+
Arity_info ( args, b1)
8581

8682

8783
let first_arity_na ( x : t ) =
8884
match x with
8985
| Arity_na
90-
| Arity_info (_, [], _) -> true
86+
| Arity_info ( [], _) -> true
9187
| _ -> false
9288

9389
let get_first_arity (x : t) =
9490
match x with
9591
| Arity_na
96-
| Arity_info (_, [], _) -> None
97-
| Arity_info (_, x::_, _) -> Some x
92+
| Arity_info ( [], _) -> None
93+
| Arity_info ( x::_, _) -> Some x
9894

9995
let extract_arity ( x : t) =
10096
match x with
10197
| Arity_na -> []
102-
| Arity_info(_,xs,_) -> xs
98+
| Arity_info(xs,_) -> xs
10399

104100
(* let update_arity (x : t) xs = *)
105101

106102
let rec
107103
merge_arities_aux
108104
(acc : int list)
109-
(unused_b : bool)
110105
(xs : int list) (ys : int list) (tail : bool) (tail2 : bool) =
111106
match xs,ys with
112107
| [], [] ->
113-
info unused_b (List.rev acc) (tail && tail2)
114-
(* tail && tail2 *)
108+
info (List.rev acc) (tail && tail2)
115109
| [], y::ys when tail ->
116-
merge_arities_aux (y::acc) unused_b [] ys tail tail2
110+
merge_arities_aux (y::acc) [] ys tail tail2
117111
| x::xs, [] when tail2 ->
118-
merge_arities_aux (x::acc) unused_b [] xs tail tail2
112+
merge_arities_aux (x::acc) [] xs tail tail2
119113
| x::xs, y::ys when x = y ->
120-
merge_arities_aux (y :: acc) unused_b xs ys tail tail2
114+
merge_arities_aux (y :: acc) xs ys tail tail2
121115
| _, _ ->
122-
info false (List.rev acc) false
116+
info (List.rev acc) false
123117

124-
let merge_arities b xs ys t t2 =
125-
merge_arities_aux [] b xs ys t t2
118+
let merge_arities xs ys t t2 =
119+
merge_arities_aux [] xs ys t t2

jscomp/core/lam_arity.mli

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@
2424

2525

2626
type t = private
27-
| Arity_info of bool * int list * bool
27+
| Arity_info of int list * bool
2828
(**
2929
when the first argument is true, it is for sure
3030
the last one means it can take any params later,
31-
for an exception: it is (Determin (true,[], true))
32-
1. approximation sound but not complete
33-
31+
for an exception: it is (Arity_info([], true))
32+
approximation sound but not complete
3433
*)
3534
| Arity_na
3635

@@ -48,7 +47,7 @@ val non_function_arity_info : t
4847
val raise_arity_info : t
4948

5049
val na : t
51-
val info : bool -> int list -> bool -> t
50+
val info : int list -> bool -> t
5251

5352
val first_arity_na : t -> bool
5453
val get_first_arity : t -> int option
@@ -57,7 +56,6 @@ val get_first_arity : t -> int option
5756
val extract_arity : t -> int list
5857

5958
val merge_arities :
60-
bool ->
6159
int list ->
6260
int list ->
6361
bool ->

jscomp/core/lam_arity_analysis.ml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,18 @@ let rec get_arity (meta : Lam_stats.t) (lam : Lam.t) : Lam_arity.t =
8282
let fn = get_arity meta app in
8383
begin match fn with
8484
| Arity_na -> Lam_arity.na
85-
| Arity_info (b, xs, tail ) ->
85+
| Arity_info ( xs, tail ) ->
8686
let rec take (arities : _ list) arg_length =
8787
match arities with
8888
| x :: yys ->
89-
if arg_length = x then Lam_arity.info b yys tail
89+
if arg_length = x then Lam_arity.info yys tail
9090
else if arg_length > x then
9191
take yys (arg_length - x)
92-
else Lam_arity.info b
92+
else Lam_arity.info
9393
((x - arg_length ) :: yys)
9494
tail
9595
| [] ->
96-
if tail then Lam_arity.info b [] tail
96+
if tail then Lam_arity.raise_arity_info
9797
else Lam_arity.na
9898
(* Actually, you can not have truly deministic arities
9999
for example [fun x -> x ]
@@ -140,12 +140,12 @@ and all_lambdas meta (xs : Lam.t list) =
140140
match acc, xs with
141141
| Arity_na, _ -> acc
142142
| _, [] -> acc
143-
| Arity_info(bbb, xxxs, tail), y::ys ->
143+
| Arity_info(xxxs, tail), y::ys ->
144144
match get_arity meta y with
145145
| Arity_na -> Lam_arity.na
146-
| Arity_info (u,yyys,tail2) ->
146+
| Arity_info (yyys,tail2) ->
147147
aux
148-
(Lam_arity.merge_arities ( u && bbb ) xxxs yyys tail tail2)
148+
(Lam_arity.merge_arities xxxs yyys tail tail2)
149149
ys
150150
in aux arity ys
151151

jscomp/core/lam_pass_collect.ml

Lines changed: 58 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -29,36 +29,30 @@
2929

3030

3131

32-
33-
let annotate (meta : Lam_stats.t)
34-
rec_flag
35-
(k:Ident.t) (v : Lam_arity.t) lambda =
36-
(* Ext_log.dwarn __LOC__ "%s/%d" k.name k.stamp; *)
32+
(** Check, it is shared across ident_tbl,
33+
Only [Lassign] will break such invariant,
34+
how about guarantee that [Lassign] only check the local ref
35+
and we track which ids are [Lassign]ed
36+
*)
37+
(**
38+
might not be the same due to refinement
39+
assert (old.arity = v)
40+
*)
41+
let annotate (meta : Lam_stats.t) rec_flag (k:Ident.t) (arity : Lam_arity.t) lambda =
3742
match Ident_hashtbl.find_opt meta.ident_tbl k with
3843
| None -> (** FIXME: need do a sanity check of arity is NA or Determin(_,[],_) *)
39-
Ident_hashtbl.add meta.ident_tbl k
40-
(FunctionId {
41-
arity = v; lambda; rec_flag})
44+
Ident_hashtbl.add meta.ident_tbl k
45+
(FunctionId {arity; lambda; rec_flag})
4246
| Some (FunctionId old) ->
43-
(** Check, it is shared across ident_tbl,
44-
Only [Lassign] will break such invariant,
45-
how about guarantee that [Lassign] only check the local ref
46-
and we track which ids are [Lassign]ed
47-
*)
48-
(**
49-
might not be the same due to refinement
50-
assert (old.arity = v)
51-
*)
52-
old.arity <- v (* due to we keep refining arity analysis after each round*)
53-
5447

48+
old.arity <- arity (* due to we keep refining arity analysis after each round*)
5549
| _ -> assert false (* TODO -- avoid exception *)
5650

5751

5852
(** it only make senses recording arities for
5953
function definition,
6054
alias propgation - and toplevel identifiers, this needs to be exported
61-
*)
55+
*)
6256
let collect_helper (meta : Lam_stats.t) (lam : Lam.t) =
6357
let rec collect_bind rec_flag
6458
(kind : Lam.let_kind)
@@ -90,83 +84,83 @@ let collect_helper (meta : Lam_stats.t) (lam : Lam.t) =
9084
(Lam_util.kind_of_lambda_block Null_undefined ls )
9185
| Lglobal_module v
9286
->
93-
Lam_util.alias_ident_or_global meta ident v (Module v) kind;
87+
Lam_util.alias_ident_or_global meta ident v (Module v) kind;
9488
| Lvar v
9589
->
96-
(
97-
(* if Ident.global v then *)
98-
Lam_util.alias_ident_or_global meta ident v NA kind
99-
(* enven for not subsitution, it still propogate some properties *)
100-
(* else () *)
101-
)
102-
| Lfunction{ params; body = l}
103-
(** TODO record parameters ident ?, but it will be broken after inlining *)
90+
(
91+
(* if Ident.global v then *)
92+
Lam_util.alias_ident_or_global meta ident v NA kind
93+
(* enven for not subsitution, it still propogate some properties *)
94+
(* else () *)
95+
)
96+
| Lfunction{ params; body}
97+
(** TODO record parameters ident ?, but it will be broken after inlining *)
10498
->
105-
(** TODO could be optimized in one pass?
106-
-- since collect would iter everywhere,
107-
so -- it would still iterate internally
108-
*)
99+
(** TODO could be optimized in one pass?
100+
-- since collect would iter everywhere,
101+
so -- it would still iterate internally
102+
*)
109103

110104
List.iter (fun p -> Ident_hashtbl.add meta.ident_tbl p Parameter ) params;
111105
let arity = Lam_arity_analysis.get_arity meta lam in
112106
annotate meta rec_flag ident arity lam;
113-
collect l
107+
collect body
114108
| x ->
115-
collect x ;
116-
if Ident_set.mem ident meta.export_idents then
117-
annotate meta rec_flag ident (Lam_arity_analysis.get_arity meta x ) lam
109+
collect x ;
110+
if Ident_set.mem ident meta.export_idents then
111+
annotate meta rec_flag ident (Lam_arity_analysis.get_arity meta x ) lam
118112

119113

120114
and collect (lam : Lam.t) =
121115
match lam with
122116

123-
(** TODO:
124-
how about module aliases..
125-
record dependency
126-
--- tricky -- if we inlining,
127-
is it safe to remove it? probably not...
128-
*)
117+
(** TODO:
118+
how about module aliases..
119+
record dependency
120+
--- tricky -- if we inlining,
121+
is it safe to remove it? probably not...
122+
*)
129123
| Lconst _ -> ()
130124
| Lvar _ -> ()
131125
| Lapply{fn = l1; args = ll; _} ->
132-
collect l1; List.iter collect ll
126+
collect l1; List.iter collect ll
133127
| Lfunction { params; body = l} -> (* functor ? *)
134-
List.iter (fun p -> Ident_hashtbl.add meta.ident_tbl p Parameter ) params;
135-
collect l
128+
List.iter (fun p -> Ident_hashtbl.add meta.ident_tbl p Parameter ) params;
129+
collect l
136130
| Llet (kind,ident,arg,body) ->
137-
collect_bind Non_rec kind ident arg ; collect body
131+
collect_bind Non_rec kind ident arg ; collect body
138132
| Lletrec (bindings, body) ->
139-
List.iter (fun (ident,arg) -> collect_bind Rec Strict ident arg ) bindings;
140-
collect body
133+
List.iter (fun (ident,arg) -> collect_bind Rec Strict ident arg ) bindings;
134+
collect body
141135
| Lglobal_module _ -> ()
142136
| Lprim {args; _} -> List.iter collect args
143137
| Lswitch(l, {sw_failaction; sw_consts; sw_blocks}) ->
144-
collect l;
145-
List.iter (fun (_, l) -> collect l) sw_consts;
146-
List.iter (fun (_, l) -> collect l) sw_blocks;
147-
begin match sw_failaction with
138+
collect l;
139+
List.iter (fun (_, l) -> collect l) sw_consts;
140+
List.iter (fun (_, l) -> collect l) sw_blocks;
141+
begin match sw_failaction with
148142
| None -> ()
149143
| Some x -> collect x
150-
end
144+
end
151145
| Lstringswitch(l, sw, d) ->
152-
collect l ;
153-
List.iter (fun (_, l) -> collect l) sw ;
154-
begin match d with
146+
collect l ;
147+
List.iter (fun (_, l) -> collect l) sw ;
148+
begin match d with
155149
| Some d -> collect d
156150
| None -> ()
157-
end
151+
end
158152
| Lstaticraise (code,ls) ->
159-
List.iter collect ls
153+
List.iter collect ls
160154
| Lstaticcatch(l1, (_,_), l2) -> collect l1; collect l2
161155
| Ltrywith(l1, _, l2) -> collect l1; collect l2
162156
| Lifthenelse(l1, l2, l3) -> collect l1; collect l2; collect l3
163157
| Lsequence(l1, l2) -> collect l1; collect l2
164158
| Lwhile(l1, l2) -> collect l1; collect l2
165159
| Lfor(_, l1, l2, dir, l3) -> collect l1; collect l2; collect l3
166160
| Lassign(v, l) ->
167-
(* Lalias-bound variables are never assigned, so don't increase
168-
v's refcollect *)
169-
collect l
161+
(* Lalias-bound variables are never assigned, so don't increase
162+
v's refcollect *)
163+
collect l
170164
| Lsend(_, m, o, ll, _) -> List.iter collect (m::o::ll)
171165
| Lifused(_, l) -> collect l in collect lam
172166

@@ -181,11 +175,11 @@ let count_alias_globals
181175
let meta : Lam_stats.t =
182176
{alias_tbl = Ident_hashtbl.create 31 ;
183177
ident_tbl = Ident_hashtbl.create 31;
184-
178+
185179
exports = export_idents;
186180
filename;
187181
env;
188182
export_idents = export_sets;
189-
} in
183+
} in
190184
collect_helper meta lam ;
191185
meta

0 commit comments

Comments
 (0)