Skip to content

Instantly share code, notes, and snippets.

@davisp
Created July 8, 2012 22:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davisp/3073295 to your computer and use it in GitHub Desktop.
Save davisp/3073295 to your computer and use it in GitHub Desktop.
LoadQueue = fun(N) ->
lists:foreach(fun(_) ->
self() ! foo
end, lists:seq(1, N))
end.
RunTest = fun() ->
lists:foreach(fun(_) ->
Ref = termsend:ref(),
receive Ref -> ok end
end, lists:seq(1, 10000))
end.
RunTest(). % This is ~instant
LoadQueue(10000).
RunTest(). % This is ~30-60s
#include "erl_nif.h"
static ERL_NIF_TERM
repeat(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
ErlNifEnv* msg_env = enif_alloc_env();
ERL_NIF_TERM ref = enif_make_ref(env);
ERL_NIF_TERM msg = enif_make_copy(msg_env, ref);
ErlNifPid pid;
enif_self(env, &pid);
enif_send(env, &pid, msg_env, msg);
enif_free_env(msg_env);
return ref;
}
static ErlNifFunc nif_funcs[] = {
{"ref", 0, repeat}
};
ERL_NIF_INIT(termsend, nif_funcs, NULL, NULL, NULL, NULL);
@davisp
Copy link
Author

davisp commented Jul 8, 2012

I've also tried creating the Ref in Erlang and passing it as an argument to the NIF but it didn't change the behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment