Pergunta

I am developing a clojure application which makes heavy use of STM. Is it better to use one global ref or many smaller refs in general. Each ref will be storing data like in a relational database table, so there will be several refs.

Foi útil?

Solução

A possible benefit of using fewer refs is that you it will be easier to comprehend what is happening in your presumably multi-threaded app.

A possible benefit of using more refs is that you will be locking less code at a time and increasing speed.

If you have a ref per table and you need to maintain data integrity between two tables, then you are going to be responsible for implementing that logic since the STM has no knowledge of how the data relates between the tables.

Your answer might lie in how often a change in one table will effect another and whether breaking your single ref out into one ref per table even gives you a notable performance increase at all.

Outras dicas

I've usually found it is better to minimise the number of refs.

Key reasons:

  • It's often helpful to treat large portions of application state as single immutable blocks (e.g. to enable easy snapshots for analysis, or passing to test functions)
  • There will be more overhead from using lots of small refs
  • It keeps your top-level namespace simpler

Of course, this will ultimately depend on your workload and concurrency demands, but my general advice would be to go with as few refs as possible until you are sure that you need more.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top