Ruby 中 WeakRef 的成本是多少?

发布于 2024-10-01 18:43:20 字数 629 浏览 0 评论 0原文

我想知道使用 WeakRef 处理大数据的开销是多少放?

我想要执行的任务是这样的:

huge = get_array_of_weak_refs # 100000000 entries or more :)
result = huge.length * huge.inject(0) { |accum, it| accum += it.total } # much more complicated, just a sample

假设 get_array_of_weak_refs 不耗时并且具有 O(1) 复杂度。因此,唯一需要关心的是巨大数组的内存大小。

我目前也不关心计算结果所需的时间。

如果huge是一个普通数组,那么它当然可能无法装入内存。

但是如果将 WeakRef 用作该数组的元素会有帮助吗?这样,在我们迭代一个元素 x 后,它就可以被垃圾回收以释放一些内存。

这个场景的开销是多少?还有其他选择吗?

I am wondering what it the overhead of using WeakRef processing large data set?

The task I want to perform is something like this:

huge = get_array_of_weak_refs # 100000000 entries or more :)
result = huge.length * huge.inject(0) { |accum, it| accum += it.total } # much more complicated, just a sample

Assuming that get_array_of_weak_refs is not time consuming and has O(1) complexity. So that only concern is the memory size of the huge array.

I also don't care at the moment about the time it takes to compute the result.

If the huge is a normal array, then of course it might just not fit in the memory.

But would it help if WeakRef would be used as elements of that array? So that after we have iterated over an element x it can be garbage collected to free up some memory.

What is the overhead for this scenario? Any alternatives?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

新一帅帅 2024-10-08 18:43:20

WeakRef 的成本可能非常高。 WeakRef 扩展了 Delegator 类,并且在 1.8 实现中 Delegator 对象非常重。每次实例化 Delegator 时,包装对象中的每个方法都会被重新定义。使用 Delegator 包装字符串将分配约 2800 个对象并使用约 90K 内存。这使得 WeakRef 在许多情况下无法使用,因为它们的创建速度非常慢,并且可能使用比它们指向的对象更多的内存。

Delegator 已在 1.9 Ruby 代码中修复,但是,存在一个错误,WeakRef 可能最终指向错误的对象,因此它们的使用是不安全的。

如果你想使用弱引用,你可以使用 ref gem (https://rubygems.org/gems/ref)。这个 gem 将每个引用的内存开销降至 <1K。如果您使用 Jruby 或 Rubinius,实现会更加高效。

The cost of WeakRef can be very high. WeakRef extends the Delegator class and in the 1.8 implementation Delegator objects are very heavy. Every time you instantiate a Delegator, every method in the wrapped object gets redefined. Wrapping a String with a Delegator will allocate ~2800 objects and use ~90K of memory. This make WeakRef's unusable in many cases since they are very slow to create and may use more memory that the objects they are pointing to.

Delegator has been fixed in the 1.9 Ruby code, however, there is a bug where WeakRef's may end up pointing to the wrong objects, so their use is unsafe.

If you want to use weak references you can use the ref gem (https://rubygems.org/gems/ref). This gem gets the overhead down to <1K of memory per reference. If you use Jruby or Rubinius, the implementations are even more efficient.

小红帽 2024-10-08 18:43:20

为什么要在这里使用弱引用?它们不会有帮助,也不是为这种情况而设计的。

相反,设置一个迭代器(一个响应 each 的对象)来分块加载数据。

Why would you use weak references here? They won't help, and are not designed for such scenario.

Instead set up an iterator (an object that responds to each) that loads the data in chunks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文