I think the make_ref() needs to be within the fun()'s context to trigger selective receive optimization in the beam's runtime.

This commit is contained in:
Gregory Burd 2013-08-01 10:02:21 -04:00
parent f153509409
commit 05c8c615ef

View file

@ -22,7 +22,8 @@
%% ------------------------------------------------------------------- %% -------------------------------------------------------------------
-define(ASYNC_NIF_CALL(Fun, Args), -define(ASYNC_NIF_CALL(Fun, Args),
F = fun(F, A, R, T) -> F = fun(F, A, T) ->
R = erlang:make_ref(),
case erlang:apply(F, [R|A]) of case erlang:apply(F, [R|A]) of
{ok, enqueued} -> {ok, enqueued} ->
receive receive
@ -38,9 +39,9 @@
{error, eagain} -> {error, eagain} ->
SleepyTime = min(30, (T+1)*2), SleepyTime = min(30, (T+1)*2),
timer:sleep(SleepyTime), timer:sleep(SleepyTime),
F(F, A, R, SleepyTime); F(F, A, SleepyTime);
Other -> Other ->
Other Other
end end
end, end,
F(Fun, Args, erlang:make_ref(), 1)). F(Fun, Args, 0)).