Ruby Performance: Mike Hall Interviews Alexander Dymo | RailsConf 2014

UGtastic Archive
Full Transcript Available
🚀 Discover how Ruby 2.1 can dramatically improve your application's memory footprint and performance! 🌟 Alexander Dymo shares his insights on copy-on-write and generational garbage collection. Don't miss this essential guide for Ruby developers! 💻 #RubyPerformance #MemoryOptimization #GenerationalGC #CopyOnWrite #Ruby21 #RailsConf2014 #DevOps #SoftwareEngineering #CTA https://just3ws.github.io/interviews/alexander-dymo-ruby-performance-talk-railsconf-2014
The Interviewer

Mike Hall

Interviewer, UGtastic

The Guest

Alexander Dymo

Ruby performance

The Conversation


Mike Hall Interviewer, UGtastic
Hi it's Mike with UGtastic. I'm at RailsConf 2014 and I'm sitting here with Alexander Dima who's going to be giving a talk this afternoon on how to cheaply and easily improve your memory footprint for your Ruby applications by moving up to Ruby 2. 1. Thank you for taking the time to speak with me. Thank you for having me. Yeah. Good to meet you Mike. So the talk title I couldn't quite recall it. It's a long title. What is the gist of the talk?
Alexander Dymo Ruby performance
So the talk is about two things mainly. Ruby 2. 1 and memory optimization. And the gist of my talk is that in fact what Ruby 2. 1 does is that it optimizes memory for you.
Mike Hall Interviewer, UGtastic
Okay.
Alexander Dymo Ruby performance
So you can do it either yourself or you can upgrade to Ruby 2. 1 or both. Like the copy on write implementation and things like that. And most importantly the generational garbage collection of course. Okay can you kind of maybe explain what those are because I even I have a fumbling understanding of what the copy on write really does. So what copy on write does is that if you fork your process you will not you will not have to copy your memory. So you don't immediately allocate the memory but you don't. You allocate the memory, you fork your process, you have now two processes that use the same memory and they use it until you modify it.
Mike Hall Interviewer, UGtastic
Okay.
Alexander Dymo Ruby performance
And only then you actually allocate more memory. Okay so you're saying okay I'm gonna fork this process and the VM knows okay there's this handle I have to have and then you start to write to it. That's okay now I'm gonna allocate it. This handle is operated by, this is handled by operating system. Oh it's by the OS level. So it's on the OS level so you don't have to do anything with this.
Mike Hall Interviewer, UGtastic
Okay.
Alexander Dymo Ruby performance
You just need to be copy and write safe.
Mike Hall Interviewer, UGtastic
Right.
Alexander Dymo Ruby performance
That's what, that actually was introduced in 2. 0 already.
Mike Hall Interviewer, UGtastic
Okay.
Alexander Dymo Ruby performance
Is that something, if you said they had to be copy and write safe, is that something that developers, Rubyists need to change the way they go about writing code? Not at all. Unless they use of course C extensions. Oh okay. Not at all. Ruby does it for you. Okay so if you're using native Ruby or Ruby core.
Mike Hall Interviewer, UGtastic
Yeah.
Alexander Dymo Ruby performance
You're gonna be fine. You're gonna be fine.
Mike Hall Interviewer, UGtastic
Okay.
Alexander Dymo Ruby performance
Okay but if it's, if you're using a gem with with C extensions you might want to look at that. You might want to investigate. You might want to investigate. But usually it's okay.
Mike Hall Interviewer, UGtastic
Okay.
Alexander Dymo Ruby performance
And you mentioned the the garbage collector.
Mike Hall Interviewer, UGtastic
There's been arguments recently about, you know, is it production ready?
Alexander Dymo Ruby performance
There's been kind of a snarky article about that. You know maybe you can tell us a little bit more about what you're gonna be talking about with the garbage collector. All right. Spoiler. This is gonna come out after your talk. It's not a spoiler.
Mike Hall Interviewer, UGtastic
Yeah.
Alexander Dymo Ruby performance
What you get from to one is a huge improvement.
Mike Hall Interviewer, UGtastic
Okay.
Alexander Dymo Ruby performance
So it is definitely production ready and the improvement I usually see is about 40% speed up just for free.
Mike Hall Interviewer, UGtastic
Yeah. Have you dug into what is different about it versus previous?
Alexander Dymo Ruby performance
So the only difference is how long GC takes usually. So in old Ruby's you had this huge memory allocated for your batches. And the garbage collection had to go through all this memory and see if there are some objects to delete or to collect. And in 2. 1 it usually goes through only new objects that were created after the last GC. So usually you have like a limited set of objects that the garbage collection goes through. That's why it takes less time. And in Ruby it's called minor GC.
Mike Hall Interviewer, UGtastic
Okay. Minor GC is that's that's the new garbage collection? Yeah.
Alexander Dymo Ruby performance
So basically the minor GC is what is collecting new objects and the major GC is the good old-fashioned GC that collects everything. Just cleans up the hole.
Mike Hall Interviewer, UGtastic
Yeah.
Alexander Dymo Ruby performance
So that's why the garbage collection in 2. 1 is actually called generational. Because you have these two generations. One is new objects and another one is old objects.
Mike Hall Interviewer, UGtastic
Yeah.
Alexander Dymo Ruby performance
And 2. 2 will have three generations. So it will be even faster.
Mike Hall Interviewer, UGtastic
So what got you digging into into the internals of Ruby like this?
Alexander Dymo Ruby performance
Well we had a bad experience with memory usage. For example like if you deploy on Heroku you have 500 megs limit. And it's quite low for Ruby.
Mike Hall Interviewer, UGtastic
Yeah.
Alexander Dymo Ruby performance
It's so easy to take 500 megs. And if you do that there are two ways. You either stop using Heroku or by there you'll PX. PX.
Mike Hall Interviewer, UGtastic
Yeah.
Alexander Dymo Ruby performance
Which is like two gigs I think for Dyno. Or it was a large number. It's a large number.
Mike Hall Interviewer, UGtastic
Yeah.
Alexander Dymo Ruby performance
If you go listen to the interview with Schneems that's that's he he says the number. And basically what you don't want to do is you don't want to spend that much money on hardware. So instead you can just optimize memory. And it's quite easy to do actually.
Mike Hall Interviewer, UGtastic
Yeah.
Alexander Dymo Ruby performance
You know looking at it have you done any digging to compare your metrics using Ruby 2.
Mike Hall Interviewer, UGtastic
1 versus using JRuby? Is it something like you've looked at or are you fully invested in MRI Ruby? Yeah.
Alexander Dymo Ruby performance
At this point fully invested in MRI and not really wanted to switch.
Mike Hall Interviewer, UGtastic
Okay.
Alexander Dymo Ruby performance
The reason is that I wrote several Rails applications Rails 2 application Rails 3 applications Rails 4 application and none of them worked with JRuby so.
Mike Hall Interviewer, UGtastic
Okay.
Alexander Dymo Ruby performance
That's that's the way it goes for me.
Mike Hall Interviewer, UGtastic
Okay.
Alexander Dymo Ruby performance
I can say I I have gotten a few out there and and it's worked for me but everybody's you know there's that old saying everybody's mileage and it all depends on what we're doing. Yeah and what we're using because I'm I'm using C extensions a lot. My proprietary extensions too. That's why I cannot use JRuby and that's why I'm not quite interested in it. So you you have a more of an engineering background.
Mike Hall Interviewer, UGtastic
Yeah.
Alexander Dymo Ruby performance
More of more of the the DHH rather you're you actually do have an engineering background. Well if software engineering. If we use DHH classification yeah it's software engineer. We'll just leave it at that. But but you've you've been you've obviously been digging into C code for a while.
Mike Hall Interviewer, UGtastic
Yeah.
Alexander Dymo Ruby performance
Has that really helped you with your Ruby understanding C so you can look at it be like oh okay I see what the abstractions are. Right and I actually done a lot of C++ programming in the past and I enjoyed that unlike most people. You were a different breed then. Yeah so that's why I'm okay when I see the problem with the Ruby I just go ahead and read the source code. Yeah that's what I did. So so again going to kind of what the theme of the original keynote was is is the reading of the source code and and having you know building up some additional skills outside of of simply being a Rubyist. You know not just saying oh I'm a Rubyist and I I know Ruby but knowing how some of that lower level languages works. It's been yeah it's it's worth the investment. It is a good investment definitely because you you come to understand how it works.
Mike Hall Interviewer, UGtastic
Yeah.
Alexander Dymo Ruby performance
And why it behaves the way it behaves.
Mike Hall Interviewer, UGtastic
Yeah.
Alexander Dymo Ruby performance
That's important when you when we optimize things. Like to really optimize you need to understand why it is slow.
Mike Hall Interviewer, UGtastic
Right.
Alexander Dymo Ruby performance
Without this you you just cannot optimize. Yeah and avoid the cargo culting.
Mike Hall Interviewer, UGtastic
Yeah.
Alexander Dymo Ruby performance
Because that was I do understand that also the memory like the old Twitter environment variables that people would use to override the garbage collector or memory or the number of objects that can be allocated between garbage collection right. But that's also been fixed in 2. 0 or 2. 1. 2. 1 actually increased memory limits.
Mike Hall Interviewer, UGtastic
Yeah.
Alexander Dymo Ruby performance
So like most people do not want to tweak these variables anymore and what I've seen recently that some people do and they do it in the wrong way.
Mike Hall Interviewer, UGtastic
Right.
Alexander Dymo Ruby performance
So if you have that those old GC environment variables and you're upgrading to 2. 1 you might want to look at taking those outs or determining what they are the new defaults are. Yeah and then like that's the part of my talk.
Mike Hall Interviewer, UGtastic
Yeah.
Alexander Dymo Ruby performance
About new defaults and like do I really want to change them.
Mike Hall Interviewer, UGtastic
Yeah. Okay.
Alexander Dymo Ruby performance
And by the way speaking about cargo cult and another way that Ruby community and especially Rails community don't want to learn is SQL.
Mike Hall Interviewer, UGtastic
Yeah. Yeah.
Alexander Dymo Ruby performance
And SQL has been there for 40 years and web frameworks come and go come and go and SQL is still here and it's such a good investment if you want to improve your performance as well.
Mike Hall Interviewer, UGtastic
Yeah. Yeah. Yeah.
Alexander Dymo Ruby performance
It's still sitting on the porch giving the old those little those little web frameworks that are rolling by under skating boards and they're roller shoes.
Mike Hall Interviewer, UGtastic
Yeah. Yeah.
Alexander Dymo Ruby performance
I understand that using barrel sounds cool but at the end it's the same plain old SQL.
Mike Hall Interviewer, UGtastic
Right.
Alexander Dymo Ruby performance
But written in Ruby.
Mike Hall Interviewer, UGtastic
Right.
Alexander Dymo Ruby performance
It's generating the SQL.
Mike Hall Interviewer, UGtastic
Yeah.
Alexander Dymo Ruby performance
It's just like you know you're people people complain about using CoffeeScript because they say oh it's just generating JavaScript that a long time ago people were saying oh why am I using an ORM it's just generating SQL. Mm-hmm. And you know it there's there's a lot of things that are going on in the world there's pros and cons to that is that right okay you can write code a little bit faster now with the ORM but we forgot about SQL I wonder if that's going to be the future with all these abstractions on JavaScript we're going to have people that don't even know JavaScript because they only know these abstractions. I don't know it's a question maybe we'll find out well the next 20 years. It's more like another question of whether you need to choose between two but you'd rather use both.
Mike Hall Interviewer, UGtastic
Right.
Alexander Dymo Ruby performance
When appropriate and I think some things are just not good to do in Ruby and Ruby will take enormous time to to do calculations for example if you have large data sets so that's why you should not forget completely about SQL because this is the thing you want to use at some point I'm not going to say that okay just forget about Active Record yeah yeah do not use any models but let's have a toolbox. And they have more than one tool right yeah you just need to have several tools.
Mike Hall Interviewer, UGtastic
Sometimes you just need a hammer yeah yeah all right well thank you very much for taking the time to speak with me. Thank you very much for having me thanks thanks user groups with lots to say interviews and more no way sharing great ideas in the tech community fascinating conversations a plethora of information find out for yourself today at ucdastic. com you