Skip to content

Turn on optimizations for unicode strings. #5457

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 1 commit into from
Jun 22, 2022
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
6 changes: 4 additions & 2 deletions jscomp/core/js_analyzer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ let rec eq_expression ({ expression_desc = x0 } : J.expression)
| _ -> false)
| Str {delim=a0; txt=b0} -> (
match y0 with Str {delim=a1; txt=b1} -> a0 = a1 && b0 = b1 | _ -> false)
| Unicode s0 -> (
match y0 with Unicode s1 -> s0 = s1 | _ -> false)
| Static_index (e0, p0, off0) -> (
match y0 with
| Static_index (e1, p1, off1) ->
Expand All @@ -203,7 +205,7 @@ let rec eq_expression ({ expression_desc = x0 } : J.expression)
eq_expression_list ls0 ls1 && flag0 = flag1 && eq_expression tag0 tag1
| _ -> false)
| Length _ | Is_null_or_undefined _ | String_append _ | Typeof _ | Js_not _
| Cond _ | FlatCall _ | New _ | Fun _ | Unicode _ | Raw_js_code _ | Array _
| Cond _ | FlatCall _ | New _ | Fun _ | Raw_js_code _ | Array _
| Caml_block_tag _ | Object _
| Number (Uint _) ->
false
Expand Down Expand Up @@ -268,6 +270,6 @@ let rev_toplevel_flatten block =

let rec is_okay_to_duplicate (e : J.expression) =
match e.expression_desc with
| Var _ | Bool _ | Str _ | Number _ -> true
| Var _ | Bool _ | Str _ | Unicode _ | Number _ -> true
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok to duplicate unicode strings

| Static_index (e, _s, _off) -> is_okay_to_duplicate e
| _ -> false
9 changes: 6 additions & 3 deletions jscomp/core/js_exp_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type t = J.expression
*)
let rec remove_pure_sub_exp (x : t) : t option =
match x.expression_desc with
| Var _ | Str _ | Number _ -> None (* Can be refined later *)
| Var _ | Str _ | Unicode _ | Number _ -> None (* Can be refined later *)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like same behaviour as strings: both pure.

| Array_index (a, b) ->
if is_pure_sub_exp a && is_pure_sub_exp b then None else Some x
| Array (xs, _mutable_flag) ->
Expand Down Expand Up @@ -173,7 +173,7 @@ module L = Literals
let typeof ?comment (e : t) : t =
match e.expression_desc with
| Number _ | Length _ -> str ?comment L.js_type_number
| Str _ -> str ?comment L.js_type_string
| Str _ | Unicode _ -> str ?comment L.js_type_string
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type of unicode is string.

| Array _ -> str ?comment L.js_type_object
| Bool _ -> str ?comment L.js_type_boolean
| _ -> { expression_desc = Typeof e; comment }
Expand Down Expand Up @@ -551,6 +551,8 @@ let rec triple_equal ?comment (e0 : t) (e1 : t) : t =
| Str {txt=x}, Str {txt=y} ->
(* CF*)
bool (Ext_string.equal x y)
| Unicode x, Unicode y ->
bool (Ext_string.equal x y)
| Number (Int { i = i0; _ }), Number (Int { i = i1; _ }) -> bool (i0 = i1)
| Optional_block (a, _), Optional_block (b, _) -> triple_equal ?comment a b
| Undefined, Optional_block _
Expand Down Expand Up @@ -810,7 +812,8 @@ let uint32 ?comment n : J.expression =

let string_comp (cmp : J.binop) ?comment (e0 : t) (e1 : t) =
match (e0.expression_desc, e1.expression_desc) with
| Str {txt=a0}, Str {txt=b0} -> (
| Str {txt=a0}, Str {txt=b0}
| Unicode a0, Unicode b0 -> (
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Equality s1 == s2 should correspond to equal byte representation also in unicode.

match cmp with
| EqEqEq -> bool (a0 = b0)
| NotEqEq -> bool (a0 <> b0)
Expand Down
2 changes: 1 addition & 1 deletion jscomp/core/js_pass_flatten_and_mark_dead.ml
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ let subst_map (substitution : J.expression Hash_ident.t) =
let _, e, bindings =
Ext_list.fold_left ls (0, [], []) (fun (i, e, acc) x ->
match x.expression_desc with
| Var _ | Number _ | Str _ | J.Bool _ | Undefined ->
| Var _ | Number _ | Str _ | Unicode _ | J.Bool _ | Undefined ->
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what enables inlining from the relevant issue linked to this PR.

(* TODO: check the optimization *)
(i + 1, x :: e, acc)
| _ ->
Expand Down
2 changes: 1 addition & 1 deletion jscomp/core/js_pass_scope.ml
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ let record_scope_pass =
TODO:
*)
match x.expression_desc with
| Fun _ | Number _ | Str _ -> state
| Fun _ | Number _ | Str _ | Unicode _ -> state
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unicode strings are immutable.

| _ ->
(* if Set_ident.(is_empty @@ *)
(* inter self#get_mutable_values *)
Expand Down
3 changes: 2 additions & 1 deletion jscomp/core/js_stmt_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ let string_switch ?(comment : string option)
?(declaration : (J.property * Ident.t) option) ?(default : J.block option)
(e : J.expression) (clauses : (string * J.case_clause) list) : t =
match e.expression_desc with
| Str {txt} -> (
| Str {txt}
| Unicode txt -> (
let continuation =
match
Ext_list.find_opt clauses (fun (switch_case, x) ->
Expand Down
8 changes: 2 additions & 6 deletions jscomp/test/record_debug_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@ var c = [

console.log(" " + a + " " + c + " ");

var a_0 = "";

var a_1 = "a";

var a_2 = "" + 3;

var a_3 = "" + 3 + 3;
Expand All @@ -98,8 +94,8 @@ var a_4 = "" + 3 + 3 + 3;
var a_5 = " " + 3;

var a$1 = [
a_0,
a_1,
"",
"a",
a_2,
a_3,
a_4,
Expand Down
22 changes: 14 additions & 8 deletions lib/4.06.1/unstable/js_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -66649,6 +66649,8 @@ let rec eq_expression ({ expression_desc = x0 } : J.expression)
| _ -> false)
| Str {delim=a0; txt=b0} -> (
match y0 with Str {delim=a1; txt=b1} -> a0 = a1 && b0 = b1 | _ -> false)
| Unicode s0 -> (
match y0 with Unicode s1 -> s0 = s1 | _ -> false)
| Static_index (e0, p0, off0) -> (
match y0 with
| Static_index (e1, p1, off1) ->
Expand All @@ -66669,7 +66671,7 @@ let rec eq_expression ({ expression_desc = x0 } : J.expression)
eq_expression_list ls0 ls1 && flag0 = flag1 && eq_expression tag0 tag1
| _ -> false)
| Length _ | Is_null_or_undefined _ | String_append _ | Typeof _ | Js_not _
| Cond _ | FlatCall _ | New _ | Fun _ | Unicode _ | Raw_js_code _ | Array _
| Cond _ | FlatCall _ | New _ | Fun _ | Raw_js_code _ | Array _
| Caml_block_tag _ | Object _
| Number (Uint _) ->
false
Expand Down Expand Up @@ -66734,7 +66736,7 @@ let rev_toplevel_flatten block =

let rec is_okay_to_duplicate (e : J.expression) =
match e.expression_desc with
| Var _ | Bool _ | Str _ | Number _ -> true
| Var _ | Bool _ | Str _ | Unicode _ | Number _ -> true
| Static_index (e, _s, _off) -> is_okay_to_duplicate e
| _ -> false

Expand Down Expand Up @@ -67280,7 +67282,7 @@ type t = J.expression
*)
let rec remove_pure_sub_exp (x : t) : t option =
match x.expression_desc with
| Var _ | Str _ | Number _ -> None (* Can be refined later *)
| Var _ | Str _ | Unicode _ | Number _ -> None (* Can be refined later *)
| Array_index (a, b) ->
if is_pure_sub_exp a && is_pure_sub_exp b then None else Some x
| Array (xs, _mutable_flag) ->
Expand Down Expand Up @@ -67418,7 +67420,7 @@ module L = Literals
let typeof ?comment (e : t) : t =
match e.expression_desc with
| Number _ | Length _ -> str ?comment L.js_type_number
| Str _ -> str ?comment L.js_type_string
| Str _ | Unicode _ -> str ?comment L.js_type_string
| Array _ -> str ?comment L.js_type_object
| Bool _ -> str ?comment L.js_type_boolean
| _ -> { expression_desc = Typeof e; comment }
Expand Down Expand Up @@ -67796,6 +67798,8 @@ let rec triple_equal ?comment (e0 : t) (e1 : t) : t =
| Str {txt=x}, Str {txt=y} ->
(* CF*)
bool (Ext_string.equal x y)
| Unicode x, Unicode y ->
bool (Ext_string.equal x y)
| Number (Int { i = i0; _ }), Number (Int { i = i1; _ }) -> bool (i0 = i1)
| Optional_block (a, _), Optional_block (b, _) -> triple_equal ?comment a b
| Undefined, Optional_block _
Expand Down Expand Up @@ -68055,7 +68059,8 @@ let uint32 ?comment n : J.expression =

let string_comp (cmp : J.binop) ?comment (e0 : t) (e1 : t) =
match (e0.expression_desc, e1.expression_desc) with
| Str {txt=a0}, Str {txt=b0} -> (
| Str {txt=a0}, Str {txt=b0}
| Unicode a0, Unicode b0 -> (
match cmp with
| EqEqEq -> bool (a0 = b0)
| NotEqEq -> bool (a0 <> b0)
Expand Down Expand Up @@ -68913,7 +68918,8 @@ let string_switch ?(comment : string option)
?(declaration : (J.property * Ident.t) option) ?(default : J.block option)
(e : J.expression) (clauses : (string * J.case_clause) list) : t =
match e.expression_desc with
| Str {txt} -> (
| Str {txt}
| Unicode txt -> (
let continuation =
match
Ext_list.find_opt clauses (fun (switch_case, x) ->
Expand Down Expand Up @@ -79961,7 +79967,7 @@ let subst_map (substitution : J.expression Hash_ident.t) =
let _, e, bindings =
Ext_list.fold_left ls (0, [], []) (fun (i, e, acc) x ->
match x.expression_desc with
| Var _ | Number _ | Str _ | J.Bool _ | Undefined ->
| Var _ | Number _ | Str _ | Unicode _ | J.Bool _ | Undefined ->
(* TODO: check the optimization *)
(i + 1, x :: e, acc)
| _ ->
Expand Down Expand Up @@ -80616,7 +80622,7 @@ let record_scope_pass =
TODO:
*)
match x.expression_desc with
| Fun _ | Number _ | Str _ -> state
| Fun _ | Number _ | Str _ | Unicode _ -> state
| _ ->
(* if Set_ident.(is_empty @@ *)
(* inter self#get_mutable_values *)
Expand Down
22 changes: 14 additions & 8 deletions lib/4.06.1/unstable/js_playground_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -66649,6 +66649,8 @@ let rec eq_expression ({ expression_desc = x0 } : J.expression)
| _ -> false)
| Str {delim=a0; txt=b0} -> (
match y0 with Str {delim=a1; txt=b1} -> a0 = a1 && b0 = b1 | _ -> false)
| Unicode s0 -> (
match y0 with Unicode s1 -> s0 = s1 | _ -> false)
| Static_index (e0, p0, off0) -> (
match y0 with
| Static_index (e1, p1, off1) ->
Expand All @@ -66669,7 +66671,7 @@ let rec eq_expression ({ expression_desc = x0 } : J.expression)
eq_expression_list ls0 ls1 && flag0 = flag1 && eq_expression tag0 tag1
| _ -> false)
| Length _ | Is_null_or_undefined _ | String_append _ | Typeof _ | Js_not _
| Cond _ | FlatCall _ | New _ | Fun _ | Unicode _ | Raw_js_code _ | Array _
| Cond _ | FlatCall _ | New _ | Fun _ | Raw_js_code _ | Array _
| Caml_block_tag _ | Object _
| Number (Uint _) ->
false
Expand Down Expand Up @@ -66734,7 +66736,7 @@ let rev_toplevel_flatten block =

let rec is_okay_to_duplicate (e : J.expression) =
match e.expression_desc with
| Var _ | Bool _ | Str _ | Number _ -> true
| Var _ | Bool _ | Str _ | Unicode _ | Number _ -> true
| Static_index (e, _s, _off) -> is_okay_to_duplicate e
| _ -> false

Expand Down Expand Up @@ -67280,7 +67282,7 @@ type t = J.expression
*)
let rec remove_pure_sub_exp (x : t) : t option =
match x.expression_desc with
| Var _ | Str _ | Number _ -> None (* Can be refined later *)
| Var _ | Str _ | Unicode _ | Number _ -> None (* Can be refined later *)
| Array_index (a, b) ->
if is_pure_sub_exp a && is_pure_sub_exp b then None else Some x
| Array (xs, _mutable_flag) ->
Expand Down Expand Up @@ -67418,7 +67420,7 @@ module L = Literals
let typeof ?comment (e : t) : t =
match e.expression_desc with
| Number _ | Length _ -> str ?comment L.js_type_number
| Str _ -> str ?comment L.js_type_string
| Str _ | Unicode _ -> str ?comment L.js_type_string
| Array _ -> str ?comment L.js_type_object
| Bool _ -> str ?comment L.js_type_boolean
| _ -> { expression_desc = Typeof e; comment }
Expand Down Expand Up @@ -67796,6 +67798,8 @@ let rec triple_equal ?comment (e0 : t) (e1 : t) : t =
| Str {txt=x}, Str {txt=y} ->
(* CF*)
bool (Ext_string.equal x y)
| Unicode x, Unicode y ->
bool (Ext_string.equal x y)
| Number (Int { i = i0; _ }), Number (Int { i = i1; _ }) -> bool (i0 = i1)
| Optional_block (a, _), Optional_block (b, _) -> triple_equal ?comment a b
| Undefined, Optional_block _
Expand Down Expand Up @@ -68055,7 +68059,8 @@ let uint32 ?comment n : J.expression =

let string_comp (cmp : J.binop) ?comment (e0 : t) (e1 : t) =
match (e0.expression_desc, e1.expression_desc) with
| Str {txt=a0}, Str {txt=b0} -> (
| Str {txt=a0}, Str {txt=b0}
| Unicode a0, Unicode b0 -> (
match cmp with
| EqEqEq -> bool (a0 = b0)
| NotEqEq -> bool (a0 <> b0)
Expand Down Expand Up @@ -68913,7 +68918,8 @@ let string_switch ?(comment : string option)
?(declaration : (J.property * Ident.t) option) ?(default : J.block option)
(e : J.expression) (clauses : (string * J.case_clause) list) : t =
match e.expression_desc with
| Str {txt} -> (
| Str {txt}
| Unicode txt -> (
let continuation =
match
Ext_list.find_opt clauses (fun (switch_case, x) ->
Expand Down Expand Up @@ -79961,7 +79967,7 @@ let subst_map (substitution : J.expression Hash_ident.t) =
let _, e, bindings =
Ext_list.fold_left ls (0, [], []) (fun (i, e, acc) x ->
match x.expression_desc with
| Var _ | Number _ | Str _ | J.Bool _ | Undefined ->
| Var _ | Number _ | Str _ | Unicode _ | J.Bool _ | Undefined ->
(* TODO: check the optimization *)
(i + 1, x :: e, acc)
| _ ->
Expand Down Expand Up @@ -80616,7 +80622,7 @@ let record_scope_pass =
TODO:
*)
match x.expression_desc with
| Fun _ | Number _ | Str _ -> state
| Fun _ | Number _ | Str _ | Unicode _ -> state
| _ ->
(* if Set_ident.(is_empty @@ *)
(* inter self#get_mutable_values *)
Expand Down
22 changes: 14 additions & 8 deletions lib/4.06.1/whole_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -234811,6 +234811,8 @@ let rec eq_expression ({ expression_desc = x0 } : J.expression)
| _ -> false)
| Str {delim=a0; txt=b0} -> (
match y0 with Str {delim=a1; txt=b1} -> a0 = a1 && b0 = b1 | _ -> false)
| Unicode s0 -> (
match y0 with Unicode s1 -> s0 = s1 | _ -> false)
| Static_index (e0, p0, off0) -> (
match y0 with
| Static_index (e1, p1, off1) ->
Expand All @@ -234831,7 +234833,7 @@ let rec eq_expression ({ expression_desc = x0 } : J.expression)
eq_expression_list ls0 ls1 && flag0 = flag1 && eq_expression tag0 tag1
| _ -> false)
| Length _ | Is_null_or_undefined _ | String_append _ | Typeof _ | Js_not _
| Cond _ | FlatCall _ | New _ | Fun _ | Unicode _ | Raw_js_code _ | Array _
| Cond _ | FlatCall _ | New _ | Fun _ | Raw_js_code _ | Array _
| Caml_block_tag _ | Object _
| Number (Uint _) ->
false
Expand Down Expand Up @@ -234896,7 +234898,7 @@ let rev_toplevel_flatten block =

let rec is_okay_to_duplicate (e : J.expression) =
match e.expression_desc with
| Var _ | Bool _ | Str _ | Number _ -> true
| Var _ | Bool _ | Str _ | Unicode _ | Number _ -> true
| Static_index (e, _s, _off) -> is_okay_to_duplicate e
| _ -> false

Expand Down Expand Up @@ -235442,7 +235444,7 @@ type t = J.expression
*)
let rec remove_pure_sub_exp (x : t) : t option =
match x.expression_desc with
| Var _ | Str _ | Number _ -> None (* Can be refined later *)
| Var _ | Str _ | Unicode _ | Number _ -> None (* Can be refined later *)
| Array_index (a, b) ->
if is_pure_sub_exp a && is_pure_sub_exp b then None else Some x
| Array (xs, _mutable_flag) ->
Expand Down Expand Up @@ -235580,7 +235582,7 @@ module L = Literals
let typeof ?comment (e : t) : t =
match e.expression_desc with
| Number _ | Length _ -> str ?comment L.js_type_number
| Str _ -> str ?comment L.js_type_string
| Str _ | Unicode _ -> str ?comment L.js_type_string
| Array _ -> str ?comment L.js_type_object
| Bool _ -> str ?comment L.js_type_boolean
| _ -> { expression_desc = Typeof e; comment }
Expand Down Expand Up @@ -235958,6 +235960,8 @@ let rec triple_equal ?comment (e0 : t) (e1 : t) : t =
| Str {txt=x}, Str {txt=y} ->
(* CF*)
bool (Ext_string.equal x y)
| Unicode x, Unicode y ->
bool (Ext_string.equal x y)
| Number (Int { i = i0; _ }), Number (Int { i = i1; _ }) -> bool (i0 = i1)
| Optional_block (a, _), Optional_block (b, _) -> triple_equal ?comment a b
| Undefined, Optional_block _
Expand Down Expand Up @@ -236217,7 +236221,8 @@ let uint32 ?comment n : J.expression =

let string_comp (cmp : J.binop) ?comment (e0 : t) (e1 : t) =
match (e0.expression_desc, e1.expression_desc) with
| Str {txt=a0}, Str {txt=b0} -> (
| Str {txt=a0}, Str {txt=b0}
| Unicode a0, Unicode b0 -> (
match cmp with
| EqEqEq -> bool (a0 = b0)
| NotEqEq -> bool (a0 <> b0)
Expand Down Expand Up @@ -237075,7 +237080,8 @@ let string_switch ?(comment : string option)
?(declaration : (J.property * Ident.t) option) ?(default : J.block option)
(e : J.expression) (clauses : (string * J.case_clause) list) : t =
match e.expression_desc with
| Str {txt} -> (
| Str {txt}
| Unicode txt -> (
let continuation =
match
Ext_list.find_opt clauses (fun (switch_case, x) ->
Expand Down Expand Up @@ -241530,7 +241536,7 @@ let subst_map (substitution : J.expression Hash_ident.t) =
let _, e, bindings =
Ext_list.fold_left ls (0, [], []) (fun (i, e, acc) x ->
match x.expression_desc with
| Var _ | Number _ | Str _ | J.Bool _ | Undefined ->
| Var _ | Number _ | Str _ | Unicode _ | J.Bool _ | Undefined ->
(* TODO: check the optimization *)
(i + 1, x :: e, acc)
| _ ->
Expand Down Expand Up @@ -242185,7 +242191,7 @@ let record_scope_pass =
TODO:
*)
match x.expression_desc with
| Fun _ | Number _ | Str _ -> state
| Fun _ | Number _ | Str _ | Unicode _ -> state
| _ ->
(* if Set_ident.(is_empty @@ *)
(* inter self#get_mutable_values *)
Expand Down