Fl4m3Ph03n1x
Is :ets.update_counter efficient?
I have been reading the ETS documentation and upon checking :ets.update_counter (which is supposed to be atomic and isolated) I read the following:
(The update is done atomically, that is, no process can access the ETS table in the middle of the operation.)
Is this a typo?
Does this really mean that updating a single counter locks the entire table?
If this is true, then this is a huge setback to any concurrent system using ETS tables, right?
Marked As Solved
ericmj
ETS has many kinds of locks, read and write locks, table and row locks so you don’t necessarily lock the whole table for updates. You can tweak how the locks are used with the read_concurrency and write_concurrency options when you create the table [1]. This is fairly recent paper on the implementation of ETS [2] if you want to learn more.
[1] http://erlang.org/doc/man/ets.html#new-2
[2] http://winsh.me/papers/erlang_workshop_2013.pdf
Also Liked
bjorng
If you are looking for efficient counters, the counters module was introduced in OTP 21.2. I recently used the counters module to speed up cover.
bjorng
Thanks for pointing this out. We will update that part of the documentation in OTP 21.3. See Clarify the atomic guarantees for ets:update_counter().
OvermindDL1
As I recall, the :counters module is built on the :atomic module, which is restricted to only word-sized integers or so, and thus it uses hardware-level atomic instructions (CAS and so forth) to perform efficient updates, where :ets’s is more generically typed, unbounded integers, etc… etc…








