Ruby Internals

Inside Ruby's VM: The TMI Edition.

Inside Ruby's VM: The TMI Edition.

by Aaron Patterson

The video titled "Inside Ruby's VM: The TMI Edition" features Aaron Patterson discussing Ruby's Virtual Machine (VM), specifically focusing on MRI's VM. Throughout the talk, Patterson provides insights into how Ruby code is compiled into bytecode and executed by the VM, bridging theoretical concepts with real implementation. The session includes:

  • Introduction to Ruby VM Internals: Patterson introduces himself and the talk's theme, emphasizing its focus on the inner workings of the Ruby VM and his personal journey in understanding it, including attempts at creating an ahead-of-time compiler.
  • Process of Running a Ruby Program: He breaks down the execution of a Ruby program into two main steps: preparation (lexer and compiler) and execution (the VM).
  • Role of the Lexer and Parser: Patterson briefly discusses the lexer that transforms the program into tokens, followed by the parser which constructs an abstract syntax tree (AST) from these tokens. While not delving deeply into these two areas, he acknowledges their importance in the context of how the VM processes Ruby code.
  • Personal Anecdotes: Throughout the talk, Patterson shares lighthearted anecdotes about his experience at RubyConf, his cats, and humorous insights to establish rapport with the audience.
  • Challenges and Learnings: He reflects on his attempts at writing an ahead-of-time compiler for Ruby. Although he considers this a 'failure', he rebrands it as a potential for future success, highlighting the importance of learning from failed attempts.

In conclusion, Patterson's presentation emphasizes understanding the Ruby VM's internals and how this knowledge can empower developers to make their Ruby code run more effectively. The key takeaway is that while exploring complex systems like Ruby's VM can be challenging, it is through this exploration that developers can innovate and enhance their experience with the language.

00:00:15 Hey, we should start now because it’s time to do that. Uh, one sec, let me take a photo. I’ve got to use this selfie stick.
00:00:30 All right, all right, all right. Thank you, thank you. I just want to say thanks to Justin for giving that really incredible talk earlier. It was really good, so everybody give him a round of applause.
00:00:41 It’s definitely the talk that we all want our co-workers to watch—those who are not here and not watching the live stream. Anyway, before I get started, I want you to know that Justin, Gary, and I met before RubyConf and we swapped all of our slides.
00:01:03 So I’m going to be giving either his talk or Gary’s talk; I’m not sure which one yet. I haven’t actually seen the slides, so please, please bear with me.
00:01:14 All right, so let’s try to do this. Okay, so JavaScript. I’m not sure if this is—I'm not sure whose talk this is yet. Could be Gary’s, could be Justin’s. Let’s see, let’s go to the next slide and see what that is.
00:01:31 I can’t tell, I really can’t. All right, let’s get to some serious business. This talk is called Ruby VM Internals: The TMI Edition. My name is Aaron Patterson; you know me on the internet as Tender Love.
00:01:50 If you don’t recognize me, I look different than I do online. Here’s what I look like online in case you don’t recognize me. This is my cat, Gorbachev Puff Puff Thunder Horse. I’ve got stickers of him, so if you’d like a sticker, you can come say hello to me and I will give you a sticker of him. This is my other cat, CAC, also known as Facebook, YouTube, Instagram. We call her Cho Cho. We also have a sticker of her now, so you can get that one too.
00:02:31 I’m on the Ruby core team and also on the Rails core team. This doesn’t mean that I know what I’m talking about; it just means that I’m very bad at saying no. This is true. I work at Red Hat as an engineer on the ManageIQ team, and we build an open-source application that manages cloud environments.
00:03:06 So if you have a cloud at your work, we can manage it with our software. We manage everything from regular clouds to rainy clouds to snowy clouds. The application is open source, and you can check it out on GitHub.
00:03:40 I signed up for the RubyConf 5K by accident, thinking it was a retirement plan. It turns out it’s just a bunch of people who are going to run, and there’s literally no point; you just run. Anyway, I’m really excited to be here in Texas. It’s a great place, and I’m particularly glad to be in San Antonio.
00:04:06 I hear San Antonio is really famous for ice cream. My wife and I arrived last night, had dinner, and when I ordered dessert, I wanted pie. Unfortunately, there was no pie emoji, so I put a pizza pie in.
00:04:44 All right, let’s get this started for real. I was going to name this talk "Stupid Ruby VM Tricks" but then I realized that the tricks aren’t really stupid, so that didn’t make sense. They’re also not really tricks, so it’s just Ruby VM.
00:05:22 I’m going to talk about Ruby’s virtual machine and its internals. In case you can’t tell, I’m very nervous. I’ve never given this talk before, and I’m scared, but we will get through this together.
00:05:34 The good news is that I’m right before lunch, so if I end too quickly, everyone will be happy to go to lunch. Also, this is the very first day, so by the end of the conference, everyone will have forgotten about my talk, so it doesn’t really matter how poorly I do.
00:06:06 Plus, I probably won’t die on stage. Hopefully, if I did die, I wouldn’t care about how bad I did. So, warning: this is a tech talk. There is actually a lot of code here; I apologize in advance. There will be code, and not all of it will be Ruby—much of it will actually be C code.
00:06:41 We’re going to talk about Ruby’s virtual machine and its internals. This is true except that this talk is primarily about failure—how I failed. I decided I would try to write an ahead-of-time compiler, thinking it would be fun, but I failed at doing that.
00:07:13 So this talk is about that and the things I learned along the way. I don’t actually think it’s a failure; I just think I’m not finished yet. I want to rebrand failure as potential for success.
00:07:51 I know how to do it; it’s just not done yet. I think data analysis is a cool thing, but after this morning’s keynote, I’m worried about the data analysis that I did. I collected a time breakdown of when I feel successful and created a pie chart.
00:08:10 You can tell that this chart is legit because one of the pie pieces is actually extracted and moved outside, so you know it’s for real. I said I failed but at least I learned something, right?
00:08:32 Does that matter? The answer is no. All right, so let’s dive in. I was thinking, as we prepare for ahead-of-time compiling, what does it mean to run a Ruby program? When we say, "Ruby, run my program," what does that mean?
00:09:00 We can actually break that down into two distinct steps: stuff that happens before the program runs and running the actual program. The stuff that happens before the program runs leads into the running of the actual program.
00:09:43 When you write eval in your code—please don’t write eval—you’re essentially going back to the beginning of this process. If we think about these two steps, we have the lexer and compiler on one side and the virtual machine on the other.
00:10:06 I don’t want to talk too much about the lexer and parser; I’m going to dedicate about two slides each to them. Most of the time, I want to spend discussing the stuff that happens between the parser and the compiler and the virtual machine.
00:10:29 The lexer exists to take your program—which is a string—and turns it into tokens. That’s it. Then there’s the parser, which takes those tokens and tries to make sense of them, converting them into an abstract syntax tree (AST).