October 16, 2020 • 2 minute read
A lot has been happening with the RxJS code base in the last couple of months. Ben has re-written almost all of it. It’s simpler and smaller.
You might wonder why it wasn’t simple and small in the first place. The answer is: performance.
With RxJS version 5, considerable effort went into not only making it at least as fast as the previous version, but also into making it as fast as possible. And that had a significant influence on it’s design.
When development on version 5 was taking place, Crankshaft — the V8 compiler at the time — had issues with closures (they were expensive) and one implication of this was that classes were used extensively.
Each operator had an Operator class — e.g.
MapOperator — and an operator-specific subscriber — e.g.
MapSubscriber. With lots of operators, that’s a lot of subscribers. And a lot of code.
These days, with TurboFan — V8’s optimizing compiler — closures don’t incur the same costs and Ben has taken advantage of this to make the operators much simpler. In fact, most share the same subscriber —
Not only is the library simpler and smaller — thanks to having far fewer subscriber classes — it’s also faster. The large number of subscriber classes affected the performance of property accesses within the base subscriber class. The gist of it it is that the optimizing compiler creates an inline cache of ‘shapes’ though which properties are accessed and many of the subscribers have different shapes. The inline cache is limited in size (only four shapes) and if the size is exceeded, the property access is deemed megamorphic and a global cache is used instead. And the global cache is subject to contention.
I’m planning on doing some performance profiling of the new codebase soon, so I’ll blog some more about the performance-related changes after that’s done. In the meantime, if performance tweaking is something you find interesting, you might want to check out Benedikt Meurer’s blog on megamorphism and frameworks.
With Ben’s rewriting of the codebase, there has been much to review. Coinccidentally, just prior to the rewrite, I started using conventional comments in my reviews. I wrote up some thoughts on them in my blog. I think they’ve been worthwhile. Ben and I are in wildly different timezones and I think conventional comments have helped make things more efficient.
Version 7 will also include some new TestScheduler features that you can read about in the linked blog post.
With the rewrite pretty much wrapped up, we’re now finishing off the types. TypeScript version 4 includes some new features — variadic tuples! — that will allow for the removal of most of the the overload signatures. I’m looking forward to that.
When it’s all done — which should be soon — it’ll be a smaller, simpler library, with simpler — but more flexible — types, too.