From 07cf1627032933e566ea6029877c07e5c7f5be1f Mon Sep 17 00:00:00 2001 From: Ulf Norell Date: Fri, 23 Aug 2019 10:07:43 +0200 Subject: [PATCH] Fix performance problem in FATE optimiser caused by debug printing --- src/aeso_fcode_to_fate.erl | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/aeso_fcode_to_fate.erl b/src/aeso_fcode_to_fate.erl index 4b79fa0..eadd9dd 100644 --- a/src/aeso_fcode_to_fate.erl +++ b/src/aeso_fcode_to_fate.erl @@ -118,9 +118,12 @@ %% -- Debugging -------------------------------------------------------------- -debug(Tag, Options, Fmt, Args) -> +is_debug(Tag, Options) -> Tags = proplists:get_value(debug, Options, []), - case Tags == all orelse lists:member(Tag, Tags) of + Tags == all orelse lists:member(Tag, Tags). + +debug(Tag, Options, Fmt, Args) -> + case is_debug(Tag, Options) of true -> io:format(Fmt, Args); false -> ok end. @@ -976,7 +979,12 @@ apply_rules(Fuel, Rules, I, Code, Options) -> case apply_rules_once(Rules, I, Code) of false -> [I | Code]; {RName, New, Rest} -> - debug(opt_rules, Options, " Applied ~p:\n~s ==>\n~s\n", [RName, pp_ann(" ", [I | Code]), pp_ann(" ", New ++ Rest)]), + case is_debug(opt_rules, Options) of + true -> + {OldCode, NewCode} = drop_common_suffix([I | Code], New ++ Rest), + debug(opt_rules, Options, " Applied ~p:\n~s ==>\n~s\n", [RName, pp_ann(" ", OldCode), pp_ann(" ", NewCode)]); + false -> ok + end, lists:foldr(Cons, Rest, New) end. @@ -1556,3 +1564,11 @@ set_labels(_, I) -> I. with_ixs(Xs) -> lists:zip(lists:seq(0, length(Xs) - 1), Xs). +drop_common_suffix(Xs, Ys) -> + drop_common_suffix_r(lists:reverse(Xs), lists:reverse(Ys)). + +drop_common_suffix_r([X | Xs], [X | Ys]) -> + drop_common_suffix_r(Xs, Ys); +drop_common_suffix_r(Xs, Ys) -> + {lists:reverse(Xs), lists:reverse(Ys)}. +