[erlang-questions] Instrumentation

Tim Watson watson.timothy@REDACTED
Fri Dec 2 20:58:04 CET 2011

There have been a few conversations about instrumentation libraries on the
list, and I've been experimenting with the features Ulf recently added to
parse_trans. I've worked up a simple annotations library, that let's you
put attributes/annotations on specific functions and attach behaviour to
them in the form of an implementation module. The current framework is very
crude and relies on a compile time parse transform, though obviously it'll
be relatively trivial to implement runtime code changes as well.

The purpose of this prototyping was to play around with some of the ideas
in Aspect Oriented Programming, and the library allows you to write code
that executes before, after or around an annotated function. This is done
without macros, which feels a bit neater to me than the approach used in
erlang-web. In addition to the library, I've made a small reference project
which implements a simple function memoization annotation using ets as a
cache. The implementation of the annotation itself is very simple:

-annotation('function').  %% scoped to functions

init(Module) ->
    ets:new(Module, [ordered_set, public, named_table,
                     {read_concurrency,true}, compressed]),

around_advice(_A, M, F, Inputs) ->
    case ets:lookup(M, {F, Inputs}) of
        [] ->
            Result = annotation:call_advised(M, F, Inputs),
            true = ets:insert_new(M, {{F, Inputs}, Result}),
        [{_, Memoized}] ->

And the usage of the annotation is very simple too:

-export([start_memoize/0, fib/1]).

start_memoize() ->

fib(0) -> 0;
fib(1) -> 1;
fib(N) -> fib(N - 1) + fib(N - 2).


As I said, the implementation is very crude but I hope that the concepts
will trigger some discussion about whether this is a useful contribution
and in what directions it ought to go.

Code is here: https://github.com/hyperthunk/annotations
Memoize is here: https://github.com/nebularis/memoize
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20111202/21978550/attachment.htm>

More information about the erlang-questions mailing list