 So, my name is Danielle Adams, I'm a software engineer at a company called Blue Apron where I aid our warehouse team and converting millions of raw produce into meals across the country. And so, today I'm going to talk about Ruby Racing. Okay, so let's get started. Show of hands. How many of you are programmers here? Excellent. Me too. How many of you write in Ruby? Cool. Me too. How many of you write primarily in Ruby? Every day? Awesome. Good ratio. And then, how many of you would you say that you are with absolute confidence that your code is running as quick as it could? One person. Awesome. Okay. And that's okay. As developers, we are, you know, we have a lot of stuff to do. We're writing code, we're involved in some sort of Git versioning, so we're pushing it up to GitHub, so to speak, we're deploying code, we're making sure that our apps don't break, we're making sure tests don't break, we're writing tests, we've got a lot going on. So, it's natural to trust what we're using, the open source software we're using, such as Ruby. And that's okay. Ruby was designed to make us happy, too. It wasn't necessarily, you know, it's not something that we've always been trained to question. Just by the look of this, can anybody tell me what method this is? Somebody preferably that is not on the Ruby core team, because they probably know, but other than that, somebody tell me what method this is. To us. Good. Much smaller, much simpler. This is why we love Ruby, because we don't want to write an entire block of code just to convert something into a string. Okay. So, just a little background, just to give you something. How do Ruby methods work? We have code. We save it to, you know, mycode.rb. This is converted into tokens, which is then parsed into an abstract syntax tree. To us, it might look like nested arrays, but it's actually a tree structure. And that's converted into byte code. As of 1.9, we're using byte code. And then that's interpreted by the Ruby VM. So, the YARV, and that's actually an acronym for yet another Ruby VM, just kind of a tongue twister. So what does that mean? It seems kind of simple, but we don't really think about it, or at least I didn't before I started investigating this. So basically, just to give you a background on what I actually, how this talk came about was, I was talking with some of my coworkers, and we were having a conversation about how to make our code faster and, well, what if we do it this way versus this way? And so we were on the whiteboard, and as most of us do, and so we're writing, oh, this looks better than this one. Well, this one's shorter than this one. This one's more readable. Okay, that's fair. As Ruby programmers, that's what we look for. We like smaller, more readable code. So then we actually put it to the test. First we put some RSpec tests to make sure it was actually doing what we wanted to do, because we don't want to base this off of buggy code. And then after that, we actually benchmarked it. So we measured it to see how fast it was, and we were actually discovered that some of the Ruby code that we were writing was actually faster in Ruby than it was in the C implementation. So then I went home and thought, oh, my gosh, what does this mean, mind blown? And then I, so I started looking for patterns that I could follow in Ruby that I could also, that I could implement my own Ruby methods. So yeah. So I rewrote a bunch of methods, all array methods, that's very easy, input, output. And yeah, I found that I was actually able to make some of the Ruby code faster. So and then I made an open source project. I made a little game, basically what you do is you pass in your own implementation of the Ruby methods. As of right now, it's, I think it can work with any type of method that, for any strings or array methods and whatnot, you find that that's not the case, come find me. And yeah, and so now I'm going to give you a little demo on how that works. So first I started with, with sort. Ruby sort is a, oh, you can, can you see that? Is that too small? Is it good? So I started with Ruby sort method, which is a quick sort algorithm. And so I tried to implement that in Ruby, much smaller, much shorter, longer as you can see. Clearly did not, did not be Ruby, which is fine. And then I tried samples. Sample is an interesting method because, yeah, find something at random, oops, that's the wrong file, erase, is that it? Yeah. Okay. So I tried, failed, whatever. Okay. And then I tried unique. It was faster. And then I tried max. Also was able to make it faster. So naturally, since I tried max, I also tried rewriting min, follows a similar algorithm. Also one. Okay. So just to give you a little preview of how, what I, how I actually implemented these. So as you can see, max was, was a basic, I was able to just use a very low level iterator, a while loop, and that just iterated through the, the array, chose the highest number, and returned it. Very simple. Unique was a little more complex. I had to, it was also using a while loop, which I found were the, was the way to make the Ruby methods faster. Using each loop actually implemented each, which was written in C. And so that created a layer of complexity. Using a while loop made was instrumental in making the Ruby methods faster. So, and I wrote my own version of contain, which is also used a while loop, which isn't most optimal way to do it, but worked, it was faster. Okay. So things to note about this. So first, doesn't necessarily mean that because of the input I'm, I'm providing that it's actually the most optimal way to, to implement, to implement that version of the method. So inputs could be a number of things and Ruby covers all of these different edge cases, best case scenario, worst case scenario. And my tiny little method just assumes that the programmer is smart and just does what exactly what we're supposed to do, because that's what we do. We always do what we say we're going to do. Benchmarking, also my method of benchmarking was just my version. It might, might be good, it might be bad. I took the difference of the time, start time, end time, made the difference, and ran that 50,000 times. So that's just, just to get a good sense of what the average number is. The average runtime. And then efficiency, just because I think it's efficient because it runs faster, it might not work for, for everybody that, anyone in this room. This efficiency I'm measuring by time, but efficiency could be measured by memory. Efficiency could be measured by scalability, by readability. There's a lot of ways to, to rate code as efficient. And so this is just my definition of efficient. So resources, so any of you that have a Python background, you might know, recognize some of these concepts. There's actually a language called PyPy. And what it did, they did is they rewrote a bunch of the Python language in Python. And it's actually proven to be faster than the original Python. Interesting. There's also a book called Ruby Under the Microscope by Pat Shaughnessy, which is a really good, really good explanation of what happens under the hood for Ruby code, highly recommended. Also, this was, this was interesting because this was something that came about just from interacting and collaborating with my coworkers. This is not something that I would have come up with alone or even bothered to think about. So I just thought it was really cool that they, they influenced me to do that. And then also, Ruby source code. I don't know how many, maybe I'm the only person that actually looks at the Ruby source code because I'm curious about how it works. I don't know C, but I try, try to understand it. Okay, so, there we go. Okay, so why do you care? You might not care, that's okay. But basically, what I want you guys to get out of this is that I think that we are taking, not taking advantage, not taking advantage, taking for granted how Ruby works. But we definitely want to be thinking about how it works under the hood and not just trusting that it's the most efficient way or it's the fastest way. And that's okay. That's what we, it's okay if it's not, but Ruby is, yeah, just be more curious about it. Okay, and also you can try this out for yourself. Gem install, Ruby racer, other readme has a very small example of how to use it. So if you have any questions, you tweet me or whatever, email, maybe. And that's it. If anything, I hope you guys can take away. It's a cute picture of a puppy, I love puppies. But if not that, then I hope that you will walk away from this a little more curious about your Ruby code, what's going on under the hood. And yeah, just break them and put them back together and be curious. Thank you.