[erlang-questions] process dictionary and get_keys/1

Ulf Wiger ulf@REDACTED
Tue Aug 6 15:35:09 CEST 2013


I pride myself of having a pretty good grasp of what's in OTP, but I confess that I had missed the introduction of the BIF get_keys(Value) in R15B.

What it does is pretty simple: it returns all keys in the process dictionary that correspond to Value - what amounts to a linear search.

Example:

1> [put(K,V) || {K,V} <- [{a,1}, {b,1}, {c,2}, {d,2}, {e,3}]].
[undefined,undefined,undefined,undefined,undefined]
2> [get_keys(V) || V <- [1, 2, 3]].
[[b,a], [c,d], [e]]

I benchmarked it against a few other ways to do a linear search on Value in a {Key, Value} set. Here are some results for extracting 5 matching keys out of 100,000:

- process_dictionary, get_keys: 431 us
- LC on a KV-list: 2,740 us
- gb_trees: 9,700 us
- dict: 14,600 us
- ets (set): 12,000 us

I know the process dictionary has its disadvantages, but when it fits the problem, it can be pretty amazing. :)

Exactly how to use this, I'm not sure yet, but I thought I'd share the info.

Can anyone shed some light on why it was introduced in the first place?

BR,
Ulf

Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.
http://feuerlabs.com






More information about the erlang-questions mailing list