[erlang-questions] mochiglobal vs ets vs registered process

Garrett Smith <>
Tue May 22 20:39:23 CEST 2012

This is a followup on a recent question I had about "meta programming"
in Erlang. The gist of the post was on a best practice for extending
functionality in Erlang without using registered processes.

This is a short hand of the sort of use case:


do_something(Value) ->

It led me to experiment with some options for storing/retrieving the
"pluggable" functionality:

- mochiglobal
- ets
- registered process running under proc_lib

The source code for the tests is pasted at the end of this email.

The test was to simply read a "global" value 1M times, after having
first initialized the facility. Times are of the "run" portion only
and do not include the "init" time.


- ets was the fastest, baseline of 1x
- mochiglobal was 1.8x slower than ets
- registered process was 7.8x slower than ets

The registered process call (slowest) was, on average, ~2 microseconds
per call slower than ets (fastest). Note that's *micro* seconds.

My conclusion:

Provided the process doesn't block unnecessarily, using a process to
lookup "pluggable" functionality (e.g. an implementation module) is
perfectly good for almost any application. IMO it doesn't warrant the
use of ets.

This may not apply to systems under heavy load, where message passing
overhead could grow non-linearly. A number of tests could be run in
parallel to get a better feel for this.

mochiglobal's use of dynamically generated Erlang modules to serve
"global" values is novel, but it doesn't appear warranted on
performance grounds alone.

The raw test results:

26> timer:tc(tests, run_ets, []).
27> timer:tc(tests, run_ets, []).
28> timer:tc(tests, run_ets, []).
29> timer:tc(tests, run_ets, []).

30> timer:tc(tests, run_mochiglobal, []).
31> timer:tc(tests, run_mochiglobal, []).
32> timer:tc(tests, run_mochiglobal, []).
33> timer:tc(tests, run_mochiglobal, []).

34> timer:tc(tests, run_server, []).
35> timer:tc(tests, run_server, []).
36> timer:tc(tests, run_server, []).
37> timer:tc(tests, run_server, []).

The test code:



-define(KEY, mykey).
-define(VALUE, "My Value").
-define(READS, 1000000).

init_mochiglobal() ->
    mochiglobal:put(?KEY, ?VALUE).

run_mochiglobal() ->

run_mochiglobal(0) -> ok;
run_mochiglobal(N) when N > 0 ->
    ?VALUE = mochiglobal:get(mykey),
    run_mochiglobal(N - 1).

init_ets() ->
    ets:new(tests_ets, [named_table]),
    ets:insert(tests_ets, {?KEY, ?VALUE}).

run_ets() ->

run_ets(0) -> ok;
run_ets(N) when N > 0 ->
    [{_, ?VALUE}] = ets:lookup(tests_ets, ?KEY),
    run_ets(N - 1).

init_server() ->
    Server = proc_lib:spawn(fun() -> server_loop(?VALUE) end),
    register(server, Server).

server_loop(Value) ->
        {get, ?KEY, Src} ->
            Src ! {ok, Value},
        _ ->

server_get(Key) ->
    server ! {get, Key, self()},
        {ok, Value} -> Value
        3000 -> error(timeout)

run_server() ->

run_server(0) -> ok;
run_server(N) when N > 0 ->
    ?VALUE = server_get(?KEY),
    run_server(N - 1).

More information about the erlang-questions mailing list