Takashi Kokubun: how to make Ruby applications run faster

The central Russian Ruby conference on September 28 marks 10 years. This year, RubyRussia has a new venue, as many as 4 streams of selected reports, communication and, of course, the legendary after-party! Among the speakers are Yukihiro Matsumoto, Takashi Kokubun, Xavier Noria, Nick Sutterer, Genadi Samokovarov and more!



In preparation, we talk with speakers about Ruby, work and life. Today we publish material in which Dmitry Matveyev (project manager in Evrone ) asks questions to Takashi Kokubun .



image

Speaker RubyRussia 2019, an optimizer for the Ruby JIT compiler, supports Haml and ERB, a backend engineer in ArmTreasure Data.



Tell me what will be discussed in your report at the conference?



About work on the JIT compiler. I would like to focus on three points. The first is function inlining. The function must be “clean”, without side effects, so that its body can be inline. If there are too many functions, the compiler starts to work slowly. I will describe how to solve this problem.

The second point concerns the optimization of the placement of objects in memory. In large and complex applications like Rails, you need to create many objects in one request. If you optimize this process, you can improve performance. Usually objects are created on the heap. Therefore, you have to manage complex data structures in it. You also have to call the garbage collector to remove unused objects. And if we were placing objects on the stack, we would simply increase the stack pointer when adding an object and decrease it when the object needs to be deleted. Using the stack to store objects is faster in itself, plus the garbage collector starts working faster. But in order to use the stack, we need to make sure that objects are not used in other places. For this purpose, we apply a technique called “escape analysis”. We need to analyze each method and make sure that each of its internal objects is not used externally and is not returned as the result of the current method. If we can do this, we can place objects on the stack, and this will increase productivity. My idea is to add metadata for instructions. Now this is an experiment, but I'm going to do a pullrequest to the master with this code in the near future.



The third point is about optimizing the dispatch of function calls in JIT. Currently, this process generates a strong branching of logic, which affects the branch prediction mechanism of the processor. This slows down the processor as a whole. We need to simplify this logic, that is, reduce the number of conditions. I am currently exploring how to do this.



This is a hardcore report with lots of code and concepts! It seems to be difficult?



I think that most people do not implement compilers on their own, and I understand that understanding this can be difficult. But I will try to explain everything as simple as possible with examples and illustrations. That is, my report should be understood by people without special experience in compiler development.



Is it possible to use the new compiler in production now, and in what cases?



The idea is to make RoR applications run faster. For example, my current company will benefit from this. But I would not say that everything is ready for production. It turned out that just RoR applications are not particularly accelerated. This is because such applications are usually large, and generate a lot of C code, which leads to inefficient use of caching in the processor. I have some ideas on how to fix this. That's what I'm working on right now. But now my compiler can be useful for small applications. For example, I know that the karafka gem works faster with it than without it.



I read that the JIT compiler is already poured into the Ruby language brunch master and has been available since version 2.6. Is it so?



Yes, my merge request has already been accepted. But this is the first version, and much work remains to be done. Improvements will already be in Ruby 2.7, and even more in Ruby 3.



Tell me how you started writing in Ruby?



At first I worked with Objective-C, but I didn't really like it. Once there was a need to write a backend application, and friends recommended trying Ruby On Rails. I did just that - and was very pleased with the result! I liked the simple and understandable syntax of the language, and I began to study and use it more deeply.



Are you writing in Ruby at work now?



I have been with Treasure Data for the past couple of years. It is a platform that tracks user data in client applications. This project was originally written in Ruby On Rails, and my post was called "Ruby Developer." So I wrote a lot in Ruby. But now I am working on some distributed services written in Java and Kotlin, and also sometimes use Python.



The Treasure Data project is associated with high load and big data. But Ruby is not the fastest language. How did you manage to create a high-load application that works with big data on the RoR platform?



The application records user data and saves it in storage. It also provides an interface for querying data in languages ​​like Hive and Presto, and an admin panel. It was not a problem to write it all on rails. Plus, we made a competent infrastructure and have the ability to increase memory and processor resources at peak loads. The main problem that we have with Ruby is that it is not suitable for parallel computing. And we have to use Kotlin for this purpose.



Why do you love Ruby? If there was a project that could be implemented in both Ruby and Python, which one would you choose?



I like the clean Ruby syntax, it is very readable and intuitive. This allows you to easily and quickly write application logic, which is important from a business point of view. This is a purely object language, I really appreciate it. I have experience in Python, but it doesn't seem to me as intuitive. I would choose it only for machine learning projects.



Another thing you've been working on lately is the Haml template engine ...



I worked on Haml and ERB at a previous job. Now I don’t devote too much time to this, but sometimes I look through pull-quests in these projects.



What do you think about the prospects of template engines such as Haml, Slim or ERB, in general, given that in the modern world there is a tendency to completely separate the back and front into two different applications, and give the formation of HTML pages completely to JavaScript? That is, in this configuration, template engines are completely unnecessary?



I agree that for tasks with a complex user interface it makes sense to put this logic in a separate application. This has to be done in order to compete with other companies, which are increasingly increasing the complexity and functionality of the UI. But there is still a need for simpler and, more importantly, cheaper solutions, which makes sense to write on RoR with classic template engines. Thus, they are still in operation and will be used for a long time.



I see that you can program in many languages, at least in Ruby, Python, Java, Kotlin and C. A good set. Can you give beginners advice on how to learn so many languages? How to become a good programmer?



I think that the effectiveness of training will be high when the language is suitable for those tasks that a programmer faces. Ruby is good for writing web applications, because working with strings in it is very easy compared to Java. Python is more suitable for machine learning (it has a rich infrastructure in this area). If you want to learn Python, I would recommend solving machine learning tasks rather than writing web applications. If you are more interested in Java, Kotlin, or other JVM-based languages, I suggest writing distributed systems. Now, Ruby is not well suited for demanding or parallel computing. To solve such problems in Ruby, we need many processes that consume a lot of resources, so they will not be effective. Thus, the use of appropriate tools for relevant problems will help to understand why this tool is designed in this way. This will help to better understand its essence.



What books are at the top of your personal “rating”?



The first is Ruby under a Microscope. It is written very simply and describes the details of the interpreter. A deep understanding of how the interpreter works will help in solving complex problems, and will also add confidence in the ownership of the tool, i.e. tongue. I recommend this book even for beginners.



The second is “Readable Code”. It's about how to write clear code, oddly enough :). We write a lot of code and read a lot of code, and we must be able to write code that others will understand. And for this, we need to look at the code from the angle of someone else's perception. Books like this one help. Also recommend even to beginners. Well, the third book ... This is a book in Japanese, about how the processor is arranged, about its architecture, up to the transistors of which it consists. Understanding how a computer works at such a low level is sometimes very useful even when solving problems at levels much higher. I recommend reading any good book on microprocessor design, even if at first glance this seems unobvious.



What do you expect from Russia and the RubyRussia conference ?



I've never been to Russia before. I expect that I will see a lot of everything beautiful, for example, beautiful houses, beautiful landscapes ... In addition, it should be colder in Russia than in Japan. It will be interesting for me to see and feel all these differences between our countries.



Cool! I can assure you that even if it is cold (in fact, at the end of September it is not so cold with us), the conference and after-party will be hot anyway!



We are also waiting for you at the conference! You can ask your questions in person (and at the legendary afterparty :) on September 28th. The program is here , and the ticket now costs 9,000 rubles.



Thanks to the companies that support RubyRussia :



Organizer - Evrone

General Partner - Toptal

Gold Partner - Gett

Silver Partners - JetBrains and Bookmate

Bronze Partner - InSales



All Articles