hamstergem/hamster — pull request #123 by alexdowad (2014-08-09 10:47:13+00:00)
Title: Implement Stream using Atomic (rather than mutexes)

This is an idea about how to make `List` more efficient. I haven't benchmarked it, or tried testing it with multiple threads. At this point, it's just a quick "code sketch".

One thing that has bothered me about our lazy `List`s is that *every time* you traverse a `List`, the mutex for *each* `Stream` must be locked and unlocked. That means that in multi-threaded code which processes a `List`, the threads will contend *even when* the `List` has already been fully realized! To me, this is... not **totally** unacceptable, but pretty bad. I think we can do better.

The downside of this approach is that we need to pull in a runtime dependency on the `atomic` gem. (On the positive side, `atomic` does work on MRI, JRuby, RBX, and probably most other Ruby implementations.)

On a simple micro-benchmark, this patch doubles the speed of iterating over a lazy `List`.