From 541e87f3597523cb7d71f75fe0381fddb2af0ee0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= Date: Wed, 8 Apr 2015 07:14:17 +0200 Subject: debugger: Optimize evaluation of new map creation Make sure that we recognize map literals at load time, as we do for lists and tuples. Also use maps:from_list/1 to build a new map instead of building it up from scratch inserting one key/value pair at the time. --- lib/debugger/src/dbg_iload.erl | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'lib/debugger/src/dbg_iload.erl') diff --git a/lib/debugger/src/dbg_iload.erl b/lib/debugger/src/dbg_iload.erl index d47d412893..379ffe8ce4 100644 --- a/lib/debugger/src/dbg_iload.erl +++ b/lib/debugger/src/dbg_iload.erl @@ -290,8 +290,7 @@ gexpr({tuple,Anno,Es0}) -> Es1 = gexpr_list(Es0), {tuple,ln(Anno),Es1}; gexpr({map,Anno,Fs0}) -> - Fs1 = map_fields(Fs0, fun gexpr/1), - {map,ln(Anno),Fs1}; + new_map(Fs0, Anno, fun gexpr/1); gexpr({map,Anno,E0,Fs0}) -> E1 = gexpr(E0), Fs1 = map_fields(Fs0, fun gexpr/1), @@ -358,9 +357,8 @@ expr({cons,Anno,H0,T0}, _Lc) -> expr({tuple,Anno,Es0}, _Lc) -> Es1 = expr_list(Es0), {tuple,ln(Anno),Es1}; -expr({map,Anno,Fs0}, _Lc) -> - Fs1 = map_fields(Fs0), - {map,ln(Anno),Fs1}; +expr({map,Anno,Fs}, _Lc) -> + new_map(Fs, Anno, fun (E) -> expr(E, false) end); expr({map,Anno,E0,Fs0}, _Lc) -> E1 = expr(E0, false), Fs1 = map_fields(Fs0), @@ -538,6 +536,24 @@ fun_clauses([{clause,A,H,G,B}|Cs]) -> [{clause,ln(A),head(H),guard(G),exprs(B, true)}|fun_clauses(Cs)]; fun_clauses([]) -> []. + +new_map(Fs0, Anno, F) -> + Line = ln(Anno), + Fs1 = map_fields(Fs0, F), + Fs2 = [{ln(A),K,V} || {map_field_assoc,A,K,V} <- Fs1], + try + {value,Line,map_literal(Fs2, #{})} + catch + throw:not_literal -> + {map,Line,Fs2} + end. + +map_literal([{_,{value,_,K},{value,_,V}}|T], M) -> + map_literal(T, maps:put(K, V, M)); +map_literal([_|_], _) -> + throw(not_literal); +map_literal([], M) -> M. + map_fields(Fs) -> map_fields(Fs, fun (E) -> expr(E, false) end). -- cgit v1.2.3