Skip to content

Commit 9239d10

Browse files
committed
Compiler: speedup wat output
1 parent 6b1510a commit 9239d10

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

compiler/lib-wasm/wat_output.ml

+19-14
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ type sexp =
115115

116116
let rec format_sexp f s =
117117
match s with
118-
| Atom s -> Format.fprintf f "%s" s
118+
| Atom s -> Format.pp_print_string f s
119119
| List l ->
120120
let has_comment =
121121
List.exists l ~f:(function
@@ -124,23 +124,28 @@ let rec format_sexp f s =
124124
in
125125
if has_comment
126126
then (* Ensure comments are on their own line *)
127-
Format.fprintf f "@[<v 2>("
128-
else Format.fprintf f "@[<2>(";
129-
Format.pp_print_list ~pp_sep:(fun f () -> Format.fprintf f "@ ") format_sexp f l;
127+
Format.pp_open_vbox f 2
128+
else Format.pp_open_box f 2;
129+
Format.pp_print_string f "(";
130+
Format.pp_print_list
131+
~pp_sep:(fun f () -> Format.pp_print_space f ())
132+
format_sexp
133+
f
134+
l;
130135
if
131136
has_comment
132-
&& List.fold_left
133-
~f:(fun _ i ->
134-
match i with
135-
| Comment _ -> true
136-
| _ -> false)
137-
~init:false
138-
l
137+
&&
138+
match List.last l with
139+
| Some (Comment _) -> true
140+
| Some _ | None -> false
139141
then
140142
(* Make sure there is a newline when a comment is at the very end. *)
141-
Format.fprintf f "@ ";
142-
Format.fprintf f ")@]"
143-
| Comment s -> Format.fprintf f ";;%s" s
143+
Format.pp_print_space f ();
144+
Format.pp_print_string f ")";
145+
Format.pp_close_box f ()
146+
| Comment s ->
147+
Format.pp_print_string f ";;";
148+
Format.pp_print_string f s
144149

145150
let index tbl x = Atom ("$" ^ Code.Var.Hashtbl.find tbl x)
146151

0 commit comments

Comments
 (0)