Skip to content

optimize the generated code, introducing iter visitor #4914

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

Merged
merged 7 commits into from
Jan 18, 2021
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
31 changes: 16 additions & 15 deletions jscomp/core/j.ml
Original file line number Diff line number Diff line change
Expand Up @@ -39,38 +39,38 @@
*)


type mutable_flag = Js_op.mutable_flag

type label = string
type binop = Js_op.binop

and binop = Js_op.binop
type int_op = Js_op.int_op

and int_op = Js_op.int_op

and kind = Js_op.kind
type kind = Js_op.kind

and property = Js_op.property
type property = Js_op.property

and number = Js_op.number
type number = Js_op.number

and mutable_flag = Js_op.mutable_flag
type ident_info = Js_op.ident_info

and ident_info = Js_op.ident_info
type exports = Js_op.exports

and exports = Js_op.exports
type tag_info = Js_op.tag_info

and tag_info = Js_op.tag_info
type property_name = Js_op.property_name

type label = string

and required_modules = module_id list



and ident = Ident.t (* we override `method ident` *)

(** object literal, if key is ident, in this case, it might be renamed by
Google Closure optimizer,
currently we always use quote
*)
and property_name = Js_op.property_name
and ident = Ident.t


and module_id = {
id : ident; kind : Js_op.kind
}
Expand Down Expand Up @@ -350,3 +350,4 @@ and deps_program =
modules : required_modules ;
side_effect : string option (* None: no, Some reason *)
}
[@@deriving]
33 changes: 16 additions & 17 deletions jscomp/core/js_analyzer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,21 @@ let add_defined_idents (x : idents_stats) ident =
Note such shaking is done in the toplevel, so that it requires us to
flatten the statement first
*)
let free_variables (stats : idents_stats) : Js_fold.fold =
let free_variables (stats : idents_stats) : Js_iter.iter =
object (self)
inherit Js_fold.fold as super
inherit Js_iter.iter as super
method! variable_declaration st =
add_defined_idents stats st.ident;
match st.value with
| None
-> self
-> ()
| Some v
->
self # expression v
method! ident id =
(if not (Set_ident.mem stats.defined_idents id )then
stats.used_idents <- Set_ident.add stats.used_idents id);
self
stats.used_idents <- Set_ident.add stats.used_idents id)

method! expression exp =
match exp.expression_desc with
| Fun(_, _,_, env)
Expand All @@ -67,8 +67,8 @@ let free_variables (stats : idents_stats) : Js_fold.fold =
*)
->
stats.used_idents <-
Set_ident.union (Js_fun_env.get_unbounded env) stats.used_idents;
self
Set_ident.union (Js_fun_env.get_unbounded env) stats.used_idents


| _
->
Expand Down Expand Up @@ -139,32 +139,31 @@ and no_side_effect (x : J.expression) =

let no_side_effect_expression (x : J.expression) = no_side_effect x

let no_side_effect clean : Js_fold.fold =
let no_side_effect clean : Js_iter.iter =
object (self)
inherit Js_fold.fold as super
inherit Js_iter.iter as super
method! statement s =
if not !clean then self else
if !clean then
match s.statement_desc with
| Throw _
| Debugger
| Break
| Variable _
| Continue _ ->
clean := false ; self
clean := false
| Exp e -> self#expression e
| Int_switch _ | String_switch _ | ForRange _
| If _ | While _ | Block _ | Return _ | Try _ -> super#statement s
method! list f x =
if not !clean then self else super#list f x
if !clean then super#list f x
method! expression s =
(if !clean then
clean := no_side_effect_expression s);
self
(** only expression would cause side effec *)
if !clean then
clean := no_side_effect_expression s
(** only expression would cause side effec *)
end
let no_side_effect_statement st =
let clean = ref true in
let _ : Js_fold.fold = ((no_side_effect clean)#statement st) in
(no_side_effect clean)#statement st;
!clean

(* TODO: generate [fold2]
Expand Down
Loading