Talks
Towards Ruby 4 JIT
Summarized using AI

Towards Ruby 4 JIT

by Takashi Kokubun

In the presentation "Towards Ruby 4 JIT," Takashi Kokubun delves into the advancements of Ruby, focusing on Just-In-Time (JIT) compilation as the language transitions towards Ruby 4. Key aspects of the talk include:

  • Ruby Evolution: The speaker emphasizes the progression from Ruby 3 to Ruby 4, introducing JIT compilation to enhance performance. Kokubun notes that he is relatively new to the Ruby community, having joined Shopify in July.

  • Understanding JIT: Kokubun explains the role of JIT compilers in optimizing Ruby's execution speed by transforming Ruby code into a more efficient instruction sequence that operates closer to machine-level code. This change boosts performance significantly compared to traditional methods of executing Ruby scripts.

  • Template Engines: The presenter discusses the evolution of template engines, specifically mentioning the transition from Haml 5 to Homily 6, which is now the default template engine. He advocates for switching to Homily for better performance, as Slim is currently the slowest engine.

  • JIT Compilers: Kokubun elaborates on the current JIT implementations in Ruby, namely MJIT and WJIT. He acknowledges the performance shortcomings of MJIT while expressing optimism about WJIT's potential. Performance benchmarking is a vital component of ensuring these JIT compilers function effectively, especially for real-world applications like Rails.

  • Future Enhancements: The discussion touches on the architectural shifts anticipated in WJIT for Ruby 3.1 and beyond, including enhancements that will improve performance and flexibility. Devs will benefit from features allowing monkey patching and customized JIT implementations, enhancing the ecosystem's adaptability.

  • Goal of Ruby 4: Kokubun concludes by noting the ultimate goal of driving Ruby's performance to rival languages like Java and JavaScript, ensuring that Ruby maintains its elegance while achieving modern development benchmarks.

In summary, Kokubun’s talk highlights significant strides in Ruby's execution speed via JIT compilation and lays out the pathway towards Ruby 4, with a clear focus on maintaining the language’s core principles while advancing its capabilities.

00:00:06.140 Hi everyone! I'm going to talk about Ruby 4. I might be the only one discussing Ruby 4 at this conference.
00:00:12.599 Currently, we are still on Ruby 3. I go by the name 'Kevin' instead of '4.' I joined Shopify in late July of this year, so I am still new to the company.
00:00:24.779 Though I've been discussing JIT compilers for about five years, I just recently joined the YJ team, where I am focusing on Ruby's JIT implementation.
00:00:37.559 In my presentation today, I will also discuss template engines including Haml and ERB, the default template engine for Ruby. These projects have been supported by my sponsors, and I'm grateful to GitHub for their support.
00:00:58.379 I have replaced the Haml 5 template engine with Homily, which I first introduced at RubyKaigi in 2015. Finally, Homily has become the default implementation for templates.
00:01:12.299 Haml 5 and Homily 6 are not the same; the latter is a complete redesign. If you are using Slim, it is currently the slowest template engine, and I encourage you to switch to Homily for better performance.
00:01:30.840 I don't expect all of you to have experience with JIT, so I will explain how it works. The Ruby interpreter typically compiles Ruby source code into an abstract syntax tree, which can be slow to traverse.
00:01:46.560 Instead, we compile it into a more sequential code to enhance performance. This involves pushing arguments onto the stack and executing them to get a return value. This approach is how the Ruby virtual machine operates, which Kohg built.
00:02:22.380 These days, we are optimizing further by generating machine-specific code instead of simulating the behavior with Ruby's virtual machine instructions. This example demonstrates how the JIT translates into assembly code.
00:02:46.920 The instruction sequence generated can be compared to bytecode, but since we are working on a broad scale, we refer to it as an instruction sequence. You can view this instruction sequence by using the 'D' method for analysis.
00:03:06.599 As you analyze the instruction, you'll find it greatly simplifies optimization for Ruby scripts. I encourage you to focus on writing beautiful Ruby code while JIT handles the low-level details.
00:03:28.620 Currently, Ruby's implementation includes two JIT compilers. The first is MJIT, introduced in Ruby 2.6, and the second is WJIT, which you might have heard about in recent discussions.
00:04:20.160 For performance benchmarking, we have a suite called 'widget bench' that helps analyze and compare different implementations. The benchmarking is crucial as it allows us to measure real-world application performance.
00:04:50.520 Currently, MJIT is not optimally performing, but WJIT also shows potential. Our ongoing efforts aim to improve overall efficiency; for instance, we aim to ensure that WJIT optimizes Rails applications effectively.
00:05:29.220 In the realm of benchmarks, we categorize them into headlining benchmarks that reflect real-world applications. For example, Rails benchmarks are essential as they directly relate to applications generating revenue.
00:05:55.740 Another interesting benchmark is the NES emulator found in our other category benchmarks. While MJIT helps improve many areas, it is essential to ensure that all benchmarks align with the Ruby developers' expectations.
00:06:34.920 As we progress with Ruby 3.1, the JIT compilers are evolving. While MJIT has limitations, WJIT is being refined to enhance performance significantly, and we are committed to supporting both options for different use cases.
00:07:14.220 The architecture of WJIT is shifting to incorporate more robust features. From Ruby 3.1 onward, you can expect better performance as we integrate changes that allow Ruby to optimize more effectively.
00:07:58.580 The transition to a new architecture will provide us room to implement various capabilities and optimizations that push Ruby performance to new heights.
00:08:29.040 On a more experimental note, the JIT compilers will allow for greater flexibility. Features like monkey patching will enable developers to customize how JIT operates, providing an exciting avenue for advanced Ruby programming.
00:09:07.920 The flexibility we are instituting means that developers will increasingly write their own JIT implementations and integrate them with Ruby efforts seamlessly.
00:09:40.920 As a community, we should embrace this flexibility and utilize it to further the Ruby ecosystem. I encourage everyone here to explore these new avenues and test the developments.
00:10:00.400 In conclusion, the evolution of Ruby, specifically with the JIT improvements, aims to bring Ruby closer to speeds achieved by languages such as Java and JavaScript. The goal is clear: we want Ruby to be competitive and meet modern development standards.
00:10:51.780 As we push towards Ruby 4, I envision a version of Ruby that can keep pace with languages optimized for performance while maintaining the elegance and simplicity that Ruby developers cherish. Thank you for your attention!
Explore all talks recorded at RubyKaigi 2022
+29