[Elixir]ets vs Agent

on-memory vs processであるets vs Agentに関して、過去に以下の議論があったのでメモ。

https://groups.google.com/forum/#!topic/elixir-lang-talk/RyhiBcD_Zvw

以下の回答は、Elixir in Actionを書かれたErlang developerの方


There are some gotchas with ETS:

  • No garbage collection of individual rows. Memory is released when the owner process dies (and there’s no heir process).
  • Data is copied to / from ETS table. Usually not a problem, but might be when individual rows are huge.
  • Changing individual rows is concurrent safe. Changing many rows, or read-modify-write operations aren’t. You need to synchronize through some process(es) if you want to do that. Even when modifying individual rows, race condition may appear if two processes try to modify the same row.

That said, there are some occasions where I find ETS useful:

  • A structure which multiple processes access frequently (essentially, a shared state). Using processes for this may introduce a single process bottleneck, while ETS allows processes to read concurrently.
  • An in-memory store for preservation of a state which should survive a process crash.
  • Optimization of a HashDict. Having two element tuples as rows, and if both elements are small in size, ETS might work faster than HashDict, being implemented in pure C. Should be used only sparingly.

As said, it’s hard to give general conclusions. If you have multiple processes which need to share state, and access it frequently, then ETS seems like a good candidate.


ここまで。
違いがわかって良いですね。

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s