Skip to content

Commit

Permalink
Merge pull request #9033 from bjorng/bjorn/use-zip-generators
Browse files Browse the repository at this point in the history
Use the new zip generators
  • Loading branch information
bjorng authored Nov 11, 2024
2 parents e270bdf + 1413289 commit 06d477a
Show file tree
Hide file tree
Showing 17 changed files with 89 additions and 96 deletions.
7 changes: 4 additions & 3 deletions lib/asn1/src/asn1ct_check.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2294,9 +2294,10 @@ use_maps(#state{options=Opts}) ->
lists:member(maps, Opts).

create_map_value(Components, ListOfVals) ->
Zipped = lists:zip(Components, ListOfVals),
#{Name => V || {#'ComponentType'{name=Name},V} <- Zipped,
V =/= asn1_NOVALUE}.
#{Name => V ||
#'ComponentType'{name=Name} <- Components &&
V <- ListOfVals,
V =/= asn1_NOVALUE}.

normalize_seq_or_set(SorS, S,
[{#seqtag{val=Cname},V}|Vs],
Expand Down
11 changes: 6 additions & 5 deletions lib/asn1/src/asn1ct_constructed_ber_bin_v2.erl
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,18 @@ enc_match_input(#gen{pack=record}, ValName, CompList) ->
enc_match_input(#gen{pack=map}, ValName, CompList) ->
Len = length(CompList),
Vars = [lists:concat(["Cindex",N]) || N <- lists:seq(1, Len)],
Zipped = lists:zip(CompList, Vars),
M = [[{asis,Name},":=",Var] ||
{#'ComponentType'{prop=mandatory,name=Name},Var} <- Zipped],
#'ComponentType'{prop=mandatory,name=Name} <- CompList &&
Var <- Vars],
case M of
[] ->
ok;
[_|_] ->
emit(["#{",lists:join(",", M),"} = ",ValName,com,nl])
end,
Os0 = [{Name,Var} ||
{#'ComponentType'{prop=Prop,name=Name},Var} <- Zipped,
#'ComponentType'{prop=Prop,name=Name} <- CompList &&
Var <- Vars,
Prop =/= mandatory],
F = fun({Name,Var}) ->
[Var," = case ",ValName," of\n"
Expand Down Expand Up @@ -316,8 +317,8 @@ dec_external(#gen{pack=map}, _RecordName) ->
Vars = asn1ct_name:all(term),
Names = ['direct-reference','indirect-reference',
'data-value-descriptor',encoding],
Zipped = lists:zip(Names, Vars),
MapInit = lists:join(",", [["'",N,"'=>",{var,V}] || {N,V} <- Zipped]),
MapInit = lists:join(",", [["'",N,"'=>",{var,V}] ||
N <- Names && V <- Vars]),
emit(["OldFormat = #{",MapInit,"}",com,nl,
"ASN11994Format =",nl,
{call,ext,transform_to_EXTERNAL1994_maps,
Expand Down
4 changes: 2 additions & 2 deletions lib/asn1/src/asn1ct_constructed_per.erl
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,8 @@ dec_external(#gen{pack=map}, _Typename) ->
Vars = asn1ct_name:all(term),
Names = ['direct-reference','indirect-reference',
'data-value-descriptor',encoding],
Zipped = lists:zip(Names, Vars),
MapInit = lists:join(",", [["'",N,"'=>",{var,V}] || {N,V} <- Zipped]),
MapInit = lists:join(",", [["'",N,"'=>",{var,V}] ||
N <- Names && V <- Vars]),
emit(["OldFormat = #{",MapInit,"}",com,nl,
"ASN11994Format =",nl,
{call,ext,transform_to_EXTERNAL1994_maps,
Expand Down
5 changes: 2 additions & 3 deletions lib/compiler/src/beam_core_to_ssa.erl
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@
map/2,mapfoldl/3,member/2,
keyfind/3,keysort/2,last/1,
partition/2,reverse/1,reverse/2,
sort/1,sort/2,splitwith/2,
zip/2]).
sort/1,sort/2,splitwith/2]).
-import(ordsets, [add_element/2,del_element/2,intersection/2,
subtract/2,union/2,union/1]).

Expand Down Expand Up @@ -321,7 +320,7 @@ expr(#c_let{vars=Cvs,arg=Ca,body=Cb}, Sub0, St0) ->
%% Break known multiple values into separate sets.
Sets = case Ka of
#ivalues{args=Kas} ->
[#iset{vars=[V],arg=Val} || {V,Val} <:- zip(Kps, Kas)];
[#iset{vars=[V],arg=Val} || V <- Kps && Val <- Kas];
_Other ->
[#iset{vars=Kps,arg=Ka}]
end,
Expand Down
21 changes: 11 additions & 10 deletions lib/compiler/src/beam_ssa_bc_size.erl
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ call_size_func(#b_set{op=call,args=[Name|Args],dst=Dst}, Bs) ->
StMap = map_get(st_map, Bs),
case StMap of
#{Name := #opt_st{ssa=Linear,args=Params}} ->
NewBs0 = setup_call_bs(Params, Args, Bs, #{}),
NewBs0 = setup_call_bs(Params, Args, Bs),
case any(fun({writable,_}) -> true;
(_) -> false
end, maps:values(NewBs0)) of
Expand Down Expand Up @@ -206,15 +206,16 @@ capture_generator_1(Dst, [_|T], Bs) ->
capture_generator_1(_, [], Bs) ->
Bs.

setup_call_bs([V|Vs], [A0|As], OldBs, NewBs) ->
A = case get_value(A0, OldBs) of
#b_literal{}=Lit -> {arg,Lit};
{writable,#b_literal{val=0}}=Wr -> Wr;
{arg,_}=Arg -> Arg;
_ -> any
end,
setup_call_bs(Vs, As, OldBs, NewBs#{V => A});
setup_call_bs([], [], #{}, NewBs) -> NewBs.
setup_call_bs(Vs, As, Bs) ->
#{V => setup_call_binding(A, Bs) || V <- Vs && A <- As}.

setup_call_binding(A, Bs) ->
case get_value(A, Bs) of
#b_literal{}=Lit -> {arg,Lit};
{writable,#b_literal{val=0}}=Wr -> Wr;
{arg,_}=Arg -> Arg;
_ -> any
end.

calc_size([{L,#b_blk{is=Is,last=Last}}|Blks], Map0) ->
case maps:take(L, Map0) of
Expand Down
5 changes: 2 additions & 3 deletions lib/compiler/src/beam_ssa_pp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,8 @@ format_instr_anno(#{arg_types:=Ts}=Anno0, FuncAnno, Args) ->

Iota = lists:seq(0, length(Args) - 1),
Formatted0 = [[format_arg(Arg, FuncAnno), " => ",
format_type(map_get(Idx, Ts),
Break)]
|| {Idx, Arg} <:- lists:zip(Iota, Args), is_map_key(Idx, Ts)],
format_type(map_get(Idx, Ts), Break)] ||
Idx <- Iota && Arg <- Args, is_map_key(Idx, Ts)],
Formatted = lists:join(Break, Formatted0),

[io_lib:format(" %% Argument types:~s~ts\n",
Expand Down
9 changes: 4 additions & 5 deletions lib/compiler/src/beam_ssa_pre_codegen.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1564,11 +1564,10 @@ recv_fix_common_1([V|Vs], [Rm|Rms], Msg, Blocks0) ->
recv_fix_common_1(Vs, Rms, Msg, Blocks);
recv_fix_common_1([], [], _Msg, Blocks) -> Blocks.

fix_exit_phi_args([V|Vs], [Rm|Rms], Exit, Blocks) ->
Path = beam_ssa:rpo([Rm], Blocks),
Preds = exit_predecessors(Path, Exit, Blocks),
[{V,Pred} || Pred <- Preds] ++ fix_exit_phi_args(Vs, Rms, Exit, Blocks);
fix_exit_phi_args([], [], _, _) -> [].
fix_exit_phi_args(Vs, Rms, Exit, Blocks) ->
[{V,Pred} ||
V <- Vs && Rm <- Rms,
Pred <- exit_predecessors(beam_ssa:rpo([Rm], Blocks), Exit, Blocks)].

exit_predecessors([L|Ls], Exit, Blocks) ->
Blk = map_get(L, Blocks),
Expand Down
20 changes: 8 additions & 12 deletions lib/compiler/src/beam_ssa_type.erl
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ opt_start_1([Id | Ids], ArgDb, StMap0, FuncDb0, MetaCache) ->
#{ Id := ArgTypes } ->
#opt_st{ssa=Linear0,args=Args} = St0 = map_get(Id, StMap0),

Ts = maps:from_list(zip(Args, ArgTypes)),
Ts = #{Arg => Type || Arg <- Args && Type <- ArgTypes},
{Linear, FuncDb} = opt_function(Linear0, Args, Id, Ts, FuncDb0, MetaCache),

St = St0#opt_st{ssa=Linear},
Expand Down Expand Up @@ -202,7 +202,7 @@ sig_function_1(Id, StMap, State0, FuncDb) ->
#opt_st{ssa=Linear,args=Args} = map_get(Id, StMap),

{ArgTypes, State1} = sig_commit_args(Id, State0),
Ts = maps:from_list(zip(Args, ArgTypes)),
Ts = #{Arg => Type || Arg <- Args && Type <- ArgTypes},

FakeCall = #b_set{op=call,args=[#b_remote{mod=#b_literal{val=unknown},
name=#b_literal{val=unknown},
Expand Down Expand Up @@ -427,7 +427,7 @@ opt_continue(Linear0, Args, Anno, FuncDb) when FuncDb =/= #{} ->
%% This is a local function and we're guaranteed to have visited
%% every call site at least once, so we know that the parameter
%% types are at least as narrow as the join of all argument types.
Ts = join_arg_types(Args, ArgTypes, #{}),
Ts = join_arg_types(Args, ArgTypes),
opt_function(Linear0, Args, Id, Ts, FuncDb);
#{ Id := #func_info{exported=true} } ->
%% We can't infer the parameter types of exported functions, but
Expand All @@ -443,11 +443,9 @@ opt_continue(Linear0, Args, Anno, _FuncDb) ->
{Linear, _} = opt_function(Linear0, Args, Id, Ts, #{}),
{Linear, #{}}.

join_arg_types([Arg | Args], [TypeMap | TMs], Ts) ->
Type = beam_types:join(maps:values(TypeMap)),
join_arg_types(Args, TMs, Ts#{ Arg => Type });
join_arg_types([], [], Ts) ->
Ts.
join_arg_types(Args, TypeMaps) ->
#{Arg => beam_types:join(maps:values(TypeMap)) ||
Arg <- Args && TypeMap <- TypeMaps}.

%%
%% Optimizes a function based on the type information inferred by signatures/2
Expand Down Expand Up @@ -2992,10 +2990,8 @@ subtract_types([{#b_var{}=V, T0}|Vs], Ts) ->
subtract_types([], Ts) ->
Ts.

parallel_join([A | As], [B | Bs]) ->
[beam_types:join(A, B) | parallel_join(As, Bs)];
parallel_join([], []) ->
[].
parallel_join(As, Bs) ->
[beam_types:join(A, B) || A <- As && B <- Bs].

gcd(A, B) ->
case A rem B of
Expand Down
13 changes: 5 additions & 8 deletions lib/compiler/src/v3_core.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1991,19 +1991,16 @@ preprocess_quals(Line, [{zip,Anno,Gens}|Qs], St, Acc) ->
Zip2 = Zip1#izip{skip_pats=[case NomatchMode of
skip -> NomatchPat;
_ -> AccPat
end ||
{NomatchMode, NomatchPat, AccPat} <:-
lists:zip3(Zip1#izip.nomatch_total,
Zip1#izip.nomatch_pats,
Zip1#izip.acc_pats)],
end || NomatchMode <- Zip1#izip.nomatch_total &&
NomatchPat <- Zip1#izip.nomatch_pats &&
AccPat <:-Zip1#izip.acc_pats],
tail_pats=[case {NomatchMode,AccPat} of
{skip,_} -> AccPat;
{_,#ibinary{}} -> AccPat#ibinary{segments=[]};
{_,_} -> AccPat
end ||
{NomatchMode, AccPat} <:-
lists:zip(Zip1#izip.nomatch_total,
Zip1#izip.tail_pats)],
NomatchMode <- Zip1#izip.nomatch_total &&
AccPat <- Zip1#izip.tail_pats],
nomatch_total=get_nomatch_total(Zip1#izip.nomatch_total)},
preprocess_quals(Line, Qs, St1, [Zip2|Acc]);
preprocess_quals(Line, [Q|Qs0], St0, Acc) ->
Expand Down
5 changes: 2 additions & 3 deletions lib/crypto/src/crypto.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2804,9 +2804,8 @@ generate_key(rsa, {ModulusSize, PublicExponent}, undefined) ->
[rsa,{ModulusSize,PublicExponent}]);
{Private, OldPrivate} when Private == OldPrivate ->
{lists:sublist(Private,2), Private};
{_Private, _OldPrivate} ->
Where = lists:map(fun({A,B}) -> A == B end,
lists:zip(_Private, _OldPrivate)),
{Private, OldPrivate} ->
Where = [A == B || A <- Private && B <- OldPrivate],
erlang:error({new_old_differ,Where},
[rsa,{ModulusSize,PublicExponent}]);
Private ->
Expand Down
30 changes: 15 additions & 15 deletions lib/parsetools/src/yecc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2339,9 +2339,10 @@ find_identical_shift_states(StateActions) ->
%% states so that they can be used from other states. Some space is
%% saved.
find_partial_shift_states(StateActionsL, StateReprs) ->
L = [{State, Actions} || {{State,Actions}, {State,State}} <-
lists:zip(StateActionsL, StateReprs),
shift_actions_only(Actions)],
L = [{State, Actions} ||
{State,Actions} <- StateActionsL &&
{State,State} <- StateReprs,
shift_actions_only(Actions)],
StateActions = sofs:family(L, [{state,[action]}]),
StateAction = sofs:family_to_relation(StateActions),
Expand All @@ -2366,10 +2367,9 @@ find_partial_shift_states(StateActionsL, StateReprs) ->
PartDataL = [#part_data{name = Nm, eq_state = EqS, actions = P,
n_actions = length(P),
states = ordsets:from_list(S)} ||
{{Nm,P}, {Nm,S}, {Nm,EqS}} <-
lists:zip3(PartNameL,
sofs:to_external(PartInStates),
sofs:to_external(PartStates))],
{Nm,P} <- PartNameL &&
{Nm,S} <- sofs:to_external(PartInStates) &&
{Nm,EqS} <- sofs:to_external(PartStates)],
true = length(PartDataL) =:= length(PartNameL),
Ps = select_parts(PartDataL),
Expand All @@ -2392,9 +2392,9 @@ find_partial_shift_states(StateActionsL, StateReprs) ->
R =
[{S, {Actions,jump_none}} || {S,Actions} <- sofs:to_external(NJS)]
++
[{S, {Actions--Part, {Tag,ToS,Part}}} ||
{{S,Actions}, {S,Part,{Tag,ToS}}} <-
lists:zip(sofs:to_external(JS), J)],
[{S, {Actions--Part, {Tag,ToS,Part}}} ||
{S,Actions} <- sofs:to_external(JS) &&
{S,Part,{Tag,ToS}} <- J],
true = length(StateActionsL) =:= length(R),
lists:keysort(1, R).
Expand Down Expand Up @@ -2450,8 +2450,8 @@ collect_some_state_info(StateActions, StateReprs) ->
true <- [RO], Repr =/= State],
#state_info{reduce_only = RO, state_repr = Repr, comment = C}
end} ||
{{State, LaActions}, {State, Repr}} <-
lists:zip(StateActions, StateReprs)],
{State, LaActions} <- StateActions &&
{State, Repr} <- StateReprs],
list_to_tuple(L).

conflict_error(Conflict, St0) ->
Expand Down Expand Up @@ -2731,9 +2731,9 @@ output_actions(St0, StateJumps, StateInfo) ->
Sel = [{S,true} || S <- ordsets:to_list(Y2CS)] ++
[{S,false} || S <- ordsets:to_list(NY2CS)],

SelS = [{State,Called} ||
{{State,_JActions}, {State,Called}} <-
lists:zip(StateJumps, lists:keysort(1, Sel))],
SelS = [{State,Called} ||
{State,_JActions} <- StateJumps &&
{State,Called} <- lists:keysort(1, Sel)],
St05 = output_nowarn(St0, yeccpars2, '', 7),
St10 = foldl(fun({State, Called}, St_0) ->
{State, #state_info{state_repr = IState}} =
Expand Down
6 changes: 2 additions & 4 deletions lib/runtime_tools/src/scheduler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,8 @@ enabled during the entire interval between the two samples.
Sample1 :: sched_sample(),
Sample2 :: sched_sample().
utilization({Stats, Ts0}, {Stats, Ts1}) ->
Diffs = lists:map(fun({{Tag, I, A0, T0}, {Tag, I, A1, T1}}) ->
{Tag, I, (A1 - A0), (T1 - T0)}
end,
lists:zip(Ts0,Ts1)),
Diffs = [{Tag, I, (A1 - A0), (T1 - T0)} ||
{Tag, I, A0, T0} <- Ts0 && {Tag, I, A1, T1} <- Ts1],

{Lst0, {A, T, N}} = lists:foldl(fun({Tag, I, Adiff, Tdiff}, {Lst, Acc}) ->
R = safe_div(Adiff, Tdiff),
Expand Down
2 changes: 1 addition & 1 deletion lib/stdlib/src/beam_lib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1278,7 +1278,7 @@ extract_literals(Chunk0) ->
Literals = [binary_to_term(Term) ||
<<N:32, Term:N/binary>> <:= Literals1],
NumLiterals = length(Literals), %Sanity check.
lists:zip(lists:seq(0, NumLiterals - 1), Literals).
lists:enumerate(0, Literals).

%%% Utils.

Expand Down
8 changes: 4 additions & 4 deletions lib/stdlib/src/qlc_pt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1796,7 +1796,7 @@ frames_to_columns(Fs, PatternVars, DerefFun, SelectorFun, Imp, CompOp) ->
BF = fun(_Op, Value) -> is_bindable(Value) end,
Fun = fun({_PatN, PatVar, PatSizes, Vars}, Frames) ->
[unify('=:=', pat_tuple(Sz, Vars), PatVar, Frame, BF, Imp) ||
{Sz, Frame} <- lists:zip(PatSizes, Frames)]
Sz <- PatSizes && Frame <- Frames]
end,
NFs = lists:foldl(Fun, Fs, SizesVarsL),
[frames2cols(NFs, PatN, PatSizes, Vars, DerefFun, SelectorFun, CompOp) ||
Expand All @@ -1805,8 +1805,8 @@ frames_to_columns(Fs, PatternVars, DerefFun, SelectorFun, Imp, CompOp) ->
frames2cols(Fs, PatN, PatSizes, Vars, DerefFun, SelectorFun, CompOp) ->
Rs = [ begin
RL = [{{PatN,Col},cons2tuple(element(2, Const))} ||
{V, Col} <- lists:zip(lists:sublist(Vars, PatSz),
lists:seq(1, PatSz)),
V <- lists:sublist(Vars, PatSz) &&
Col <- lists:seq(1, PatSz),
%% Do not handle the case where several
%% values compare equal, e.g. "X =:= 1
%% andalso X == 1.0". Looking up both
Expand All @@ -1826,7 +1826,7 @@ frames2cols(Fs, PatN, PatSizes, Vars, DerefFun, SelectorFun, CompOp) ->
tl(Consts = DerefFun(V, F)) =:= [],
(Const = (SelectorFun(F))(hd(Consts))) =/= no],
sofs:relation(RL) % possibly empty
end || {F,PatSz} <- lists:zip(Fs, PatSizes)],
end || F <- Fs && PatSz <- PatSizes],
Ss = sofs:from_sets(Rs),
%% D: columns occurring in every frame (path).
D = sofs:intersection(sofs:projection(fun(S) -> sofs:projection(1, S) end,
Expand Down
3 changes: 1 addition & 2 deletions lib/stdlib/src/rand.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1761,8 +1761,7 @@ exs1024_jump({L, RL}, AS, JL, J, N, TN) ->
{_, NS} = exs1024_next({L, RL}),
case ?MASK(1, J) of
1 ->
AS2 = lists:zipwith(fun(X, Y) -> X bxor Y end,
AS, L ++ lists:reverse(RL)),
AS2 = [X bxor Y || X <- AS && Y <- L ++ lists:reverse(RL)],
exs1024_jump(NS, AS2, JL, J bsr 1, N-1, TN-1);
0 ->
exs1024_jump(NS, AS, JL, J bsr 1, N-1, TN-1)
Expand Down
2 changes: 1 addition & 1 deletion lib/tools/src/lcnt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ summate_stats([S|Ss], #stats{ tries = Tries, colls = Colls, time = Time, nt = Nt
summate_histogram(Tup,undefined) when is_tuple(Tup) -> Tup;
summate_histogram(undefined,Tup) when is_tuple(Tup) -> Tup;
summate_histogram(Hs1,Hs2) ->
list_to_tuple([ A + B || {A,B} <- lists:zip(tuple_to_list(Hs1),tuple_to_list(Hs2))]).
list_to_tuple([A + B || A <- tuple_to_list(Hs1) && B <- tuple_to_list(Hs2)]).

%% manipulators
filter_locks_type(Locks, undefined) -> Locks;
Expand Down
Loading

0 comments on commit 06d477a

Please sign in to comment.