I've started working on a new edition of Ruby Under a Microscope that covers Ruby 3.x. I'm working on this in my spare time, so it will take a while. Leave a comment or drop me a line and I'll email you when it's finished. Here’s an excerpt from the completely new content for Chapter 4, about YJIT and ZJIT. I’m still finishing this up… so this content is fresh off the page! It’s been a lot of fun for me to learn about how JIT compilers work and to brush up on my Rust skills as well. And it’s very exciting to see all the impressive work the Ruby team at Shopify and other contributors have done to improve Ruby’s runtime performance. Chapter 4: Compiling Ruby To Machine Language Interpreting vs. Compiling Ruby Code4 Yet Another JIT (YJIT)6 Virtual Machines and Actual Machines6 Counting Method and Block Calls8 YJIT Blocks8 YJIT Branch Stubs10 Executing YJIT Blocks and Branches11 Deferred Compilation12 Regenerating a YJIT Branch12 YJIT Guards14 Adding Two Integers Using Machine Language15 Experiment 4-1: Which Code Does YJIT Optimize?18 How YJIT Recompiles Code22 Finding a Block Version22 Saving Multiple Block Versions24 ZJIT, Ruby’s Next Generation JIT26 Counting Method and Block Calls27 ZJIT Blocks29 Method Based JIT31 Rust Inside of Ruby33 Experiment 4-2: Reading ZJIT HIR and LIR 35 Summary37 Counting Method and Block Calls To find hot spots, YJIT counts how many times your program calls each function or block. When this count reaches a certain threshold, YJIT stops your program and converts that section of code into machine language. Later Ruby will execute the machine language version instead of the original YARV instructions. To keep track of these counts, YJIT saves an internal counter nearby the YARV instruction sequence for each function or block. Figure 4-5: YJIT saves information adjacent to each set of YARV instructions Figure 4-5 shows the YARV instruction sequence the main Ruby compiler created for the sum += i block at (3) in Listing 4-1. At the top, above...
First seen: 2025-11-17 20:47
Last seen: 2025-11-18 15:50