Essential insights from Hacker News discussions

How I Made Ruby Faster Than Ruby

Here's a summary of the themes expressed in the Hacker News discussion:

Ruby's Performance Debate: Slow vs. Fast vs. "Fast Enough"

A central theme is the ongoing debate about Ruby's execution speed. Some users argue that Ruby is inherently a slow language, particularly when compared to lower-level languages like Rust. This perspective is often tied to the idea that Ruby prioritizes developer happiness and expressiveness over raw performance.

  • swombat, in challenging the author's conclusion, states, "I love Ruby, but it is still a slow language on most benchmarks. That's ok. For most webapps, the bottleneck is not execution-time performance, it's the speed and joy of writing the code." They further emphasize, "But there's no point in pretending Ruby, compared to, say, Rust, isn't a slow-as-molasses execution environment. It is. It's ok. It's optimised for developer happiness, not speed."

However, others argue that Ruby's performance is not a fixed attribute and can be significantly influenced by how the code is written and the optimizations applied. The advent of Just-In-Time (JIT) compilers like YJIT is seen as a significant factor in improving Ruby's performance, even to the point where pure Ruby code can sometimes rival or outperform C libraries due to optimized VMs and reduced data transfer overhead.

  • norman784 notes, "AFAIK with the latest JIT in some contexts pure Ruby can be faster than using C libraries, just because the VM can be better optimized and there is no overhead in moving data between the two realms."

The author of the original post, ciconia, strongly defends the idea that Ruby's performance does matter, even in an "I/O-bound" context. They argue that improving execution time and reducing GC pressure leads to better percentile performance and lower compute costs.

  • ciconia states, "From my experience, getting the average execution time per render from say 0.1ms to 0.01ms, and especially reducing allocations and GC pressure has a big effect on 99% percentiles, and consequently on the cost of compute." They also propose, "Saying because we use Ruby we don't care if it's slow or not is in a way dismissing it as a viable platform for writing reliable software (because performance is part of reliability)."

The Role of "Glue Code" and C Extensions

Another significant theme is the capability of Ruby to achieve high performance when used as "glue code" for C/C++ extensions. This approach leverages the expressiveness of Ruby for the overall application structure while offloading computationally intensive tasks to highly optimized native code.

  • Asmod4n highlights this, saying, "As long as you only use ruby as glue code for c(++) extensions it’s pretty fast." They provide a striking example: "Depends how you use it, just last week I’ve hit 40 nanoseconds unpacking a 8 megabyte msgpack array and accessing one of its values in a hash."

This perspective suggests that Ruby's performance limitations are more apparent in pure Ruby computations rather than in scenarios where it orchestrates external, high-performance libraries.

Developer Happiness vs. Infrastructure Costs

The discussion touches upon the classic trade-off between the speed of developer iteration and happiness, and the costs associated with scaling infrastructure to compensate for performance deficits. While some argue that "developer happiness" is paramount and performance issues can be managed with infrastructure, others contend that neglecting performance can lead to higher operational costs and reliability issues.

  • swombat initially frames this as, "For most webapps, the bottleneck is not execution-time performance, it's the speed and joy of writing the code."
  • makeitdouble concurs to a degree, stating, "Not OP, but to a point I think this is pretty much true...We currently have decent performance so it works out well for most use cases, but if Ruby were to be slower, we could probably cover that issue with infra, caching or other means. As we already do in many cases. It would be a pain point, but in comparison increasing developer happiness or the whole product dev experience is IMHO a lot harder."

However, the author ciconia pushes back against this dichotomy, arguing that good performance and developer happiness are not mutually exclusive.

  • ciconia asserts, "To me, you can use Ruby to create systems that have very good performance characteristics, and still benefit from developer happiness. The two are not contradictory."

Language Comparisons and Nuances

The conversation also involves comparisons between Ruby and other languages, particularly Python, and acknowledges the specific strengths and weaknesses of each.

  • barrkel offers a comparison: "Ruby is slow, but it is however faster than Python last I checked, with like for like code." They also add, "Python gives you other interesting ways of going fast (a lot of high performance plugins for numerics, Cython, and so on), while Ruby is a higher level more expressive language IMO, so you have more ways to shoot yourself in the foot more powerfully."

These comparisons highlight that perceived performance is often dependent on the specific use case and the ecosystem of libraries and tools available.

The "Mythical" I/O-Bound Application and Optimization

A sub-theme, directly stemming from the author's post and the linked article, is the questioning of the pervasive notion that most web applications are "I/O-bound" and therefore insulated from the impact of CPU performance. The argument is made that even in I/O-bound scenarios, reducing CPU-bound operations can lead to tangible benefits.

  • The linked external article, referenced by Lio as "the mythical io-bound rails app," is implicitly central to this discussion.
  • Lio also notes, "Even if you're not using CPU bound it's still daft to leave performance on the table you don't need to."

This challenges the widely held belief that optimizing CPU-bound code in Ruby is unnecessary for many web applications.


Note: The discussion also contained a brief, tangential exchange about the longevity of internet memes ("Yo dawg"), which is not a core technical theme of the performance discussion.