Skip to content

Commit 5451910

Browse files
KakaduDmitrii.Kosarev a.k.a. Kakadu
authored and
Dmitrii.Kosarev a.k.a. Kakadu
committed
ppx_fresh: specialize for 2,3,4,5 variables
Signed-off-by: Dmitrii Kosarev a.k.a. Kakadu <[email protected]> Using custom functions Fresh.{one,two,three} positively affects performance, code size and shouldn't change search order
1 parent 478b7ce commit 5451910

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

ppx/fresh/ppx_fresh.ml

+33-9
Original file line numberDiff line numberDiff line change
@@ -198,26 +198,50 @@ let mapper =
198198
in
199199
(match reconstruct_args args with
200200
| Some (xs : string list) ->
201-
let ans =
202-
List.fold_right
203-
xs
204-
~f:(fun ident acc ->
201+
let to_pat ident = Pat.var ~loc (Ast_builder.Default.Located.mk ident ~loc) in
202+
let rec loop = function
203+
| [] -> [%expr delay (fun () -> [%e new_body])]
204+
| ident1 :: ident2 :: ident3 :: ident4 :: ident5 :: tl ->
205+
[%expr
206+
Fresh.five
207+
(fun
208+
[%p to_pat ident1]
209+
[%p to_pat ident2]
210+
[%p to_pat ident3]
211+
[%p to_pat ident4]
212+
[%p to_pat ident5]
213+
-> [%e loop tl])]
214+
| ident1 :: ident2 :: ident3 :: ident4 :: tl ->
215+
[%expr
216+
Fresh.four
217+
(fun
218+
[%p to_pat ident1]
219+
[%p to_pat ident2]
220+
[%p to_pat ident3]
221+
[%p to_pat ident4]
222+
-> [%e loop tl])]
223+
| ident1 :: ident2 :: ident3 :: tl ->
224+
[%expr
225+
Fresh.three (fun [%p to_pat ident1] [%p to_pat ident2] [%p to_pat ident3] ->
226+
[%e loop tl])]
227+
| ident1 :: ident2 :: tl ->
228+
[%expr Fresh.two (fun [%p to_pat ident1] [%p to_pat ident2] -> [%e loop tl])]
229+
| ident :: tl ->
205230
[%expr
206231
Fresh.one
207232
(fun [%p Pat.var ~loc (Ast_builder.Default.Located.mk ident ~loc)] ->
208-
[%e acc])])
209-
~init:[%expr delay (fun () -> [%e new_body])]
233+
[%e loop tl])]
210234
in
211-
ans
235+
loop xs
212236
| None ->
213-
Stdlib.Format.eprintf "Can't reconstruct args of 'fresh'";
237+
Format.eprintf "Can't reconstruct args of 'fresh'";
214238
{ e with pexp_desc = Pexp_apply (e1, [ Nolabel, new_body ]) })
215239
| Pexp_apply (d, [ (_, body) ]) when is_defer d ->
216240
let ans = [%expr delay (fun () -> [%e self#expression body])] in
217241
ans
218242
| Pexp_apply (d, body) when is_unif d ->
219243
(* let loc_str =
220-
Stdlib.Format.asprintf "%a" Selected_ast.Ast.Location.print_compact e.pexp_loc;
244+
Caml.Format.asprintf "%a" Selected_ast.Ast.Location.print_compact e.pexp_loc;
221245
in
222246
let body = (Labelled "loc", Exp.constant (Pconst_string (loc_str,None))) :: body in *)
223247
Exp.apply ~loc:e.pexp_loc d body

0 commit comments

Comments
 (0)