[Elixir]ets vs Agent

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


以下の回答は、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.




WordPress.com ロゴ

WordPress.com アカウントを使ってコメントしています。 ログアウト / 変更 )

Twitter 画像

Twitter アカウントを使ってコメントしています。 ログアウト / 変更 )

Facebook の写真

Facebook アカウントを使ってコメントしています。 ログアウト / 変更 )

Google+ フォト

Google+ アカウントを使ってコメントしています。 ログアウト / 変更 )

%s と連携中