Ruby Internals

Summarized using AI

What's happening when initializing mruby?

Yurie Yamane and Masayoshi Takahashi • November 18, 2019 • Nashville, TN

In the video titled "What's happening when initializing mruby?" presented at RubyConf 2019, Yurie Yamane and Masayoshi Takahashi delve into the initialization process of mruby, a lightweight implementation of the Ruby programming language optimized for embedded systems.

The session begins with a brief introduction to mruby and its significance in embedded environments, highlighting its smaller size compared to other Ruby versions like MRI and JRuby. The speakers outline the challenges faced during mruby's initialization, particularly concerning memory usage and execution speed.

Key points discussed include:

- Overview of mruby: mruby aims to comply with the ISO Ruby standard while focusing on embedded applications such as servers and games.

- Demonstration: The team showcases mruby running on the M5StickC board, displaying functionalities like a countdown timer and movement response, demonstrating its practical application capabilities.

- Initialization Process: Initialization in mruby involves setting up core libraries, defining classes, modules, and standard objects, which significantly impacts memory allocation.

- Memory Management: mruby's memory management involves dynamic memory allocation during initialization, creating potential issues for resource-limited environments.

- Proposed Solutions: The speakers present a new approach to mruby's initialization by implementing pre-execution. This technique runs mruby to generate C source code, which is then compiled, thereby optimizing memory usage.

- Results of the Enhancements: They discuss test results showing reduced memory usage and improved performance, crucial for embedded programming.

- Future Directions: Insights are shared on ongoing efforts to enhance memory management without sacrificing functionality, emphasizing structured data organization and efficient method handling.

The conclusion reiterates the importance of optimizing mruby for better performance in embedded systems, inviting further discussions and collaboration from the audience on these innovations and improvements.

What's happening when initializing mruby?
Yurie Yamane and Masayoshi Takahashi • November 18, 2019 • Nashville, TN

RubyConf 2019 - What's happening when initializing mruby? by Yurie Yamane and Masayoshi Takahashi

Like MRI, mruby initializes all classes before execution. This has a negative impact on Ruby's initialization speed and memory. This increase in memory usage is particularly fatal for mruby. We have created a new mrbgem to solve this issue. The key idea is pre-execution; we actually run mruby and do initialization, generate the structures in the form of C source code, and recompile it. In this session, we will look at the initial behavior of the mruby through an introduction to our tools.

#rubyconf2019 #confreaks

RubyConf 2019

00:00:12.679 Hi everyone, welcome! Today we will talk about the initialization of mruby. As we begin, we will provide a brief overview of our session, and then dive into the specifics of mruby and its initialization process. We'll discuss the problems associated with mruby's initialization and how we can improve it.
00:00:30.300 Our solution involves using a new implementation method for mruby. Finally, we will share our results and discuss future directions for our work. Let me introduce ourselves: I am Yurie Yamane, and I'm interested in microcomputers and real-time operating systems. I'm also a member of the Ruby 2 project, which focuses on legacy systems.
00:00:51.210 I present demonstrations and lectures at various exhibitions. Along with my colleague Masayoshi Takahashi, who has utilized Ruby for over 20 years and is involved in organizing meetups in Japan, we come together to present this session. We also published a book called "Introduction to mruby" a few months ago that I'd like to share with anyone interested.
00:01:40.799 The topic of our session today focuses on the initialization of mruby. We will discuss how objects are created during this process and our proposed improvements. First, let’s dive into what mruby is: it is an implementation of the Ruby programming language designed to be lightweight and efficient for embedded systems.
00:02:14.210 Mruby is much smaller compared to implementations like MRI and JRuby, and it aims to comply with the ISO standard of Ruby. Primarily, mruby targets embedded systems as well as applications like servers and games. We recently utilized a board called M5Stack to demonstrate mruby.
00:02:58.780 This board, known as the M5StickC, is compact yet powerful, featuring an ESP32 microcontroller with Wi-Fi and Bluetooth capabilities, as well as a small LCD and various sensors. We have already installed mruby and a sample program on it, which we will demonstrate shortly.
00:03:48.900 (Demo of the M5StickC) This device can display a countdown timer, synchronize time using NTP via its Wi-Fi connection, and reacts to movement by displaying a random number when shaken. The original program, written in C++, has been ported to mruby for this demonstration.
00:05:09.300 Now, returning to our main topic—initialization of mruby. This process involves creating various objects. An important consideration is that all classes in Ruby are also objects, leading to a larger number of objects than classes. Specifically, the number can range from 3 to 212, depending on the configuration.
00:06:00.060 The initialization process in mruby encompasses several stages, which include setting up the core library, defining all classes and modules, and establishing standard objects such as strings and arrays. Importantly, many objects are generated when initializing mruby, which we will define further in this discussion.
00:07:02.410 In this session, we are defining initialization as everything that occurs before the execution of your mruby program. This encompasses critical stages such as system setup and the allocation of memory for objects. In mruby, memory allocation occurs during initialization, creating a significant memory footprint.
00:08:42.100 One key aspect of mruby’s memory management is the implementation of a mutable state, which holds contextual information necessary during execution.
00:08:54.680 We will discuss how classes and modules are defined during initialization. Classes in mruby are represented by structs that contain various tables for instance variables, methods, and more. This information is stored in memory and initializes every necessary object.
00:09:45.000 Our goal is to optimize memory usage during this initialization process. Many classes and methods remain static over time, allowing us to reduce memory consumption in environments with limited resources, such as microcontrollers.
00:10:58.259 We have proposed a method that utilizes advanced techniques to improve memory consumption, particularly for low-memory environments. This involves pre-execution where we run mruby to generate C source code that will then be compiled into an executable.
00:11:55.000 This generated code allows for more efficient memory use than traditional methods, particularly by establishing a clear distinction between memory allocated during execution and memory that can remain static.
00:13:00.000 Using this new approach for memory allocation and project execution leads to a significant reduction in memory consumption while maintaining functionality. The generated C source code includes symbols and objects necessary to dynamically execute mruby applications effectively.
00:14:45.000 Now let's take a closer look at how memory usage differs when using our enhanced mruby implementation. For example, unlike traditional methods that frequently allocate memory on-the-fly, our method allows key information and objects to be represented more efficiently.
00:16:14.500 We are also introducing a new system for method handling where class methods are stored in ROM rather than in heap memory, allowing us to streamline the allocation process. Our methodology also allows for easier memory management by organizing data into predefined categories.
00:17:45.200 Moreover, we have implemented a more structured approach to enhancing functionality without sacrificing performance. By capturing method information and object data at the right time during initialization, we can maintain optimal performance across all scenarios.
00:19:07.200 We conducted further tests that demonstrate our method's efficiency in memory usage, particularly in embedded environments where memory constraints are present. Our findings indicate that reduced memory usage is pivotal for the success of applications utilizing mruby.
00:20:50.900 In conclusion, we have shared insights into our ongoing projects, challenges, and future ambitions for optimizing mruby. We express our gratitude for your attention and invite everyone to provide feedback or ask questions now.
00:22:19.300 Thank you all very much for joining us today and for your interest in our work! We look forward to more discussions and collaborations in the future.
Explore all talks recorded at RubyConf 2019
+88