Skip to content

Commit

Permalink
beam_bsm: Optimize btb_index()
Browse files Browse the repository at this point in the history
lists:dropwhile/2 and the fun in btb_index_1/2 shows up in the
top 10 list of eprof. Replace dropwhile with a special-purpose
function for a tiny increase in speed.
  • Loading branch information
bjorng committed Apr 22, 2015
1 parent de9c0ab commit 93ad33d
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/compiler/src/beam_bsm.erl
Expand Up @@ -20,7 +20,7 @@
-module(beam_bsm).
-export([module/2,format_error/1]).

-import(lists, [member/2,foldl/3,reverse/1,sort/1,all/2,dropwhile/2]).
-import(lists, [member/2,foldl/3,reverse/1,sort/1,all/2]).

%%%
%%% We optimize bit syntax matching where the tail end of a binary is
Expand Down Expand Up @@ -542,16 +542,13 @@ btb_context_regs_1(Regs, N, Tag, Acc) ->
%% a binary. MustSave is true if the function may pass the match
%% context to the bs_context_to_binary instruction (in which case
%% the current position in the binary must have saved into the
%% start position using "bs_save_2 Ctx start".
%% start position using "bs_save_2 Ctx start").

btb_index(Fs) ->
btb_index_1(Fs, []).

btb_index_1([{function,_,_,Entry,Is0}|Fs], Acc0) ->
[{label,Entry}|Is] =
dropwhile(fun({label,L}) when L =:= Entry -> false;
(_) -> true
end, Is0),
Is = drop_to_label(Is0, Entry),
Acc = btb_index_2(Is, Entry, false, Acc0),
btb_index_1(Fs, Acc);
btb_index_1([], Acc) -> gb_trees:from_orddict(sort(Acc)).
Expand All @@ -566,6 +563,9 @@ btb_index_2(Is0, Entry, _, Acc) ->
throw:none -> Acc
end.

drop_to_label([{label,L}|Is], L) -> Is;
drop_to_label([_|Is], L) -> drop_to_label(Is, L).

btb_index_find_start_match([{test,_,{f,F},_},{bs_context_to_binary,_}|Is]) ->
btb_index_find_label(Is, F);
btb_index_find_start_match(_) ->
Expand Down

0 comments on commit 93ad33d

Please sign in to comment.