[Erlangweb-users] implementation of edict
Brought to you by:
etcerlangweb,
paulgray
|
From: Ulf W. <ul...@wi...> - 2008-08-30 15:19:16
|
Just thought I'd think out loud about the implementation of edict.
Note that I've just been browsing the code on the web, so this
is just from a quick read-through.
The data representation seems a bit inefficient. For example:
fset(Key, Value) ->
case ets:lookup(?MODULE, self()) of
[{_, Dict}] -> ets:insert(?MODULE, {self(), dict:store(Key, Value, Dict)});
[] -> exit(no_dict_attached)
end.
This strategy obviously scales very poorly if a large number of keys
is inserted.
An alternative strategy would be:
fset(Key, Value) ->
case ets:member(?MODULE, self()} of
true ->
ets:insert(?MODULE, {{self(),Key}, Value});
false ->
exit(no_dict_attached)
end.
...and so on.
The penalty will be incurred on terminate_state(), which will
look something like this:
terminate_state() ->
ets:delete(?MODULE, self()),
ets:match_delete(?MODULE, {self(),'_'},'_'}).
(the ets table being an ordered_set, of course.)
Since it would appear as if this module will be called many times while
building a page, I would think that this ought to give better characteristics.
One more thing - I believe this function to be wrong:
fset(List, Key, Value) ->
case ets:lookup(?MODULE, self()) of
[{_, Dict}] -> ets:insert(?MODULE, {self(), dict:store(List, [{Key,
Value}|dict_fetch(List, Dict)], Dict)});
[] -> exit(no_dict_attached)
end.
The dict_fetch/2 function returns 'undefined' if there is no occurence
of the key List.
That would mean that calling e.g. fset("list", "person", "john")
before storing an
initial value for "list", would result in the term {"list",
[{"person","john"}|undefined]}
in the dictionary. It would be better to return an error or initialize
it to [] first.
BR,
Ulf W
|