Clojure 中的一个引用还是多个引用?
我正在开发一个大量使用 STM 的 Clojure 应用程序。一般来说,使用一个全局引用或许多较小的引用更好吗?每个引用都会像关系数据库表一样存储数据,因此会有多个引用。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用较少引用的一个可能的好处是,您将更容易理解多线程应用程序中发生的情况。
使用更多引用的一个可能的好处是,您将一次锁定更少的代码并提高速度。
如果每个表都有一个引用,并且需要维护两个表之间的数据完整性,那么您将负责实现该逻辑,因为 STM 不知道数据在表之间的关联方式。
您的答案可能在于一个表中的更改影响另一个表的频率,以及将单个引用分解为每个表一个引用是否会给您带来显着的性能提升。
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.
我通常发现尽量减少参考文献的数量会更好。
关键原因:
,这最终取决于您的工作负载和并发需求,但我的一般建议是使用尽可能少的参考,直到您确定需要更多。
I've usually found it is better to minimise the number of refs.
Key reasons:
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.