Talks
Summarized using AI

Why JRuby?

by Charles Nutter

In the video titled "Why JRuby?", Charles Nutter discusses the advantages of using JRuby, a Ruby implementation that runs on the Java Virtual Machine (JVM). He begins by introducing himself and his extensive history with both Java and Ruby, highlighting his full-time work on JRuby since 2006. Nutter explains that JRuby allows developers to leverage the capabilities of the JVM, which offers robust memory management, superior multithreading, and an optimizing compiler.

Key Points Discussed:

  • What is JRuby?

    • JRuby is Ruby running on the JVM, primarily written in Java, but transitioning to Ruby for easier maintenance.
    • It supports features of Ruby versions 1.8 and 1.9, positioned as a drop-in replacement for MRI Ruby.
    • Limitations include incomplete support for certain low-level UNIX features and deprecated C extensions for performance reasons.
  • Performance Improvements:

    • JRuby has seen enhancements in concurrency, garbage collection, and execution speed through its integration with the JVM.
    • The JVM's garbage collectors minimize resource wastage, leading to better performance compared to MRI Ruby.
  • Cross-Platform Support:

    • JRuby works well across various platforms, including Windows and Dalvik for Android, making it versatile for deployment.
    • It provides significant library access, with compatibility for Java libraries that do not require native extensions.
  • Application Deployment:

    • Any Java-based server can run Ruby applications, making deployment seamless in Java environments.
    • Tools like Trinidad and TorqueBox enhance the experience for Ruby developers by packaging server functionalities.
  • Example Applications:

    • Demonstrations are provided, including graphics handling in games and applications using libraries like JavaFX for GUI components.
    • The discussion includes the example of a Minecraft plugin developed with JRuby, showcasing its capabilities in plugin development.

Conclusion and Takeaways:

Nutter discusses the vibrant JRuby community and encourages developers to transition from MRI Ruby, with the acknowledgment of potential challenges, primarily concerning C extension compatibility. He highlights JRuby's advantages, including optimized performance and extensive Java library access. Overall, the talk underscores JRuby as a potent, efficient alternative for Rubyists aimed at harnessing the JVM's strengths while maintaining Ruby's features.

00:00:14.900 Okay, it's three o'clock now, so I guess we'll just get going. I will talk a little bit about why JRuby is interesting, give some fun demos, and hopefully have time for some questions at the end.
00:00:21.240 I'm Charles Nutter, and I've been doing Java since the beginning. I've actually been working with Ruby since 2004 when I attended a Ruby conference. I have been working on JRuby full-time since 2006.
00:00:28.710 It's kind of crazy that it's been six years now of just working on JRuby, and I'm still not bored of it, which is even more amazing. Recently, I moved to Red Hat in the JBoss Polyglot Server group where we're doing some fun stuff.
00:00:39.420 So, first of all, what is JRuby? Hopefully, some of you have played with JRuby before. Okay, about half the room. JRuby, well, you can look in the book we have on deploying JRuby, but basically it is Ruby running on the JVM, or a better virtual machine for running Ruby code.
00:00:51.060 Most of JRuby is written in Java, which is kind of legacy since that's what the code base was. However, we are moving more and more parts of it into Ruby over time, which makes it easier for us to maintain and allows more people to contribute.
00:01:06.509 Everything is pretty much standard; we have 1.8 and 1.9 features in the box all the time, so you can switch between them depending on what you are opting for. We try to make it a drop-in replacement for MRI, which is standard Ruby.
00:01:17.939 However, there are a few caveats. Low-level UNIX stuff, like low-level file descriptor control, process control, and things like 'fork,' are not fully implemented yet. Some of these are being added and improved over time, but it's an ongoing process. We don't really support C extensions in JRuby.
00:01:35.909 In JRuby 1.6, we added support for C extensions as part of the Ruby Summer of Code, but it proved to be a significant maintenance hassle, especially with Ruby 1.9 additions to the APIs, and very few people used it.
00:01:48.900 Moreover, the way that the Ruby C extension API is designed actually cripples concurrency and performance in many ways. For the most part, C extensions are deprecated, but we can clarify that later.
00:02:03.090 Like I said, some features are different or unavailable. For example, each object is not always available unless you explicitly turn it on for debugging or profiling purposes. There are also trace functions that allow you to enable debugging support.
00:02:15.300 We can't support some features because, at the VM level, they simply don't work, like continuations and forking. JRE 1.7 is the release we are working on right now, which I hope everybody will try out.
00:02:26.970 JRuby 1.9.3 is the default, and we have made a ton of changes to ensure that 1.9.3 behavior is nearly perfect. We've been working particularly hard on encoding, which is a significant change in Ruby 1.9.
00:02:41.970 There are numerous performance improvements, particularly around concurrency and straight-line execution. We have also added support for Java 7's invoke dynamic, which I'll discuss shortly.
00:02:54.060 This essentially provides us a much better chance at optimizing Ruby in the future. We also have the beginnings of a new optimizing compiler within JRuby, before even hitting the code to the JVM.
00:03:10.259 To get started with JRuby, you need a JVM installed. Most platforms have a JVM that's relatively easy to get. We have a blog post on the JRuby blog explaining how to set up Java 7 on Linux, Windows, OS X, etc.
00:03:25.340 If you're using RVM, 'rvm install JRuby' will get the current major release, which is 1.6.8 right now. I encourage you to try out the 1.7 release candidate because we're very close to a final release.
00:03:42.460 You can also easily download it manually; there's no complex build process involved. We pre-built all the native bits for almost a dozen platforms, so you can simply download a tarball, unpack it, put it in your path, and you're ready to go.
00:03:56.900 Let's see what running JRuby looks like. I've prepared a basic command line benchmark that generates a Julia set fractal.
00:04:03.120 Okay, I finished the flip-flop fractal. I like this one because it actually builds the fractal in the terminal.
00:04:18.900 So JRuby is obviously working at this point. Now I have to show the code for this, just because it's hilarious. This code must have been part of an obfuscation contest because I have absolutely no idea how the bottom piece of code works.
00:04:32.460 Has anyone ever used the flip-flop operator in Ruby? Yes, of course, Aaron has – he uses it all the time, which is why Rails is awesome. But most people don't even know what it does in Ruby.
00:04:50.810 This author has managed to stack nine of them together to generate a fractal. If you want some homework for afterwards, you can figure out exactly what's going on in that code.
00:05:02.450 My job today is to convince anyone who hasn't played with JRuby or tried it out that it's really the best Ruby runtime you can use. I put an asterisk here because it's the best for some definitions of 'best.' There are various contexts where JRuby might not be the right tool.
00:05:25.820 But I'm going to show you where I think JRuby really fits and where its sweet spots are. First of all, let's talk about the JRuby team, which is the baseline of why JRuby works as a project.
00:05:43.670 Most people think of JRuby and the JRuby team as pretty much myself, since I do a lot of work on it and give many talks. However, it’s much more complicated than that.
00:05:55.310 The real JRuby team consists of Tom and me at the top, orchestrating things and managing a whole bunch of contributors who come and go over time. We're all working on core JRuby and the libraries that support it.
00:06:08.990 Additionally, all the different JVMs out there are also part of our team. They handle the VM-level operations, integrating with the processor and the operating system. We build on top of that.
00:06:21.890 The JRuby team includes all the different JVM groups that are doing great work as well. When you look at how JRuby is structured as an implementation, you'll see parts dedicated to JRuby, core team contributions, the parser, the bytecode JIT, core classes, Java integration, and native integration.
00:06:39.650 Additionally, the JVM at the base implements threading, concurrency, safe concurrency at the VM level, garbage collection, and has its own JNI (Java Native Interface) capabilities.
00:06:51.080 This means we don't have to worry about implementing our own garbage collector or debugging it. The JVM developers handle that, and we can focus on improving JRuby.
00:07:02.909 This structure allows us to catch up with and almost match the Ruby core implementation. As it happens, JRuby 1.7 has a hidden '--2.0' flag that enables several Ruby 2.0 features that are named and released.
00:07:15.770 We've caught up now, and we can keep pace with regular Ruby development. The bottom line is that we could basically stop working on JRuby today, and it would still continue to improve, as the JVM teams keep implementing optimizations.
00:07:28.510 As long as the JVM exists, JRuby will continue to get better, even if we don’t do anything further. Next, I want to highlight JRuby's cross-platform support.
00:07:43.030 There have been various people who have claimed JRuby is the best way to run Ruby on Windows. For a time, Matz even suggested that if you're struggling with Ruby on Windows, you should give JRuby a shot. We've spent considerable effort ensuring that JRuby runs well on Windows and other exotic platforms because the JVM has been tested and optimized across multiple platforms.
00:08:25.300 Interestingly, we can run on Dalvik, which is the Android VM. There is support for a library called Rubato that allows you to write Ruby applications in JRuby, deploy them to the market, and run them as normal Dalvik applications.
00:08:40.460 We've had support for this for a couple of years now. Additionally, Oracle recently made their embedded HotSpot and OpenJDK available for general-purpose use, allowing you to use JRuby on small devices for embedded computing.
00:08:54.270 Here is our issue tracker: are there any OpenVMS users or AS/400 users here? Not surprisingly, OpenVMS is still alive and kicking. There are a couple of folks who have maintained those systems for 25 years.
00:09:18.520 We have approximately 15 mostly closed issues and a few open ones in our tracker for OpenVMS. Our list of supported platforms includes unique cases like s390x for Linux mainframes and PowerPC Linux. Unfortunately, we do not yet have OpenVMS or AS/400 builds, but we are working hard to ensure that everything you receive with JRuby will run on any JVM without requiring a build.
00:09:51.200 This list from RubyGems outlines all the libraries for Java platform versions. Some of these libraries are only compatible with JRuby because they were designed for it. The important concept here is that these libraries are built with JRuby in mind, needing no native C extensions or additional library requirements.
00:10:16.300 You can simply install them, and they'll run. Even better, you can pull everything into a single installable package and ship it to another machine where it will run just the same.
00:10:42.670 Regarding servers, any Java-based server can host Ruby applications. If you're working in an environment focused on Java, you can just deploy your Ruby applications on the same servers. We also have servers designed more with Ruby developers in mind.
00:11:01.780 Trinidad, for instance, is essentially the Tomcat application server stripped down and wrapped with a user-friendly Ruby command line, allowing you to run it like Unicorn or Puma.
00:11:26.840 It retains all the functionalities of Tomcat, including multiple threads and high concurrency but operates under a regular Ruby server framework.
00:11:41.980 Lastly, TorqueBox is interesting because it integrates the services from larger enterprise applications within one package, retaining a Ruby-centric API and configurations.
00:12:03.250 If we look at a typical Ruby application, it might start simply with a server, a web front-end using Rack, and perhaps a backend using Sinatra or Rails. However, as applications grow, they become more complex.
00:12:24.210 You may need background jobs, scheduled tasks, and monitoring systems. The idea with TorqueBox is that it provides all these services—messaging, caching, job scheduling, etc.—all in one place, managed through a single process.
00:12:36.759 Here’s a short list of what comes packaged with TorqueBox. If you're building a JRuby application, you can handle everything within a single server without needing additional external dependencies.
00:12:58.140 TorqueBox is simply installable as a gem, allowing you to pull it down, start it up, and have everything running on the system without installing anything else. The Ruby APIs for all these tools are quite accessible.
00:13:18.530 The basic configuration allows you to specify which Ruby version to use, whether you want to compile things at runtime, enable debugging, or run interactively. TorqueBox can even spin up a little IRB console on the server so you can check connectivity and run queries.
00:13:41.260 The configuration can be done in both Ruby DSL form and YAML form depending on your preference. Let's specifically look at messaging for a moment.
00:14:02.630 Here are the Ruby and YAML versions for setting up a queue and a topic. All of this goes into the same TorqueBox configuration file, giving you message queues that follow a simple Ruby API.
00:14:24.000 You won't have to deal with the typical hassle of managing separate processes for your messaging. This is quite straightforward.
00:14:37.920 Now, moving on to libraries, if you examine Maven, the standard repository of libraries for Java applications, there are currently 340,000 versions of different JARs available.
00:14:54.720 In contrast, RubyGems.org has around 35,000 gems—indicating a ten-to-one difference in library availability. If you’re running on JRuby, nearly all of these can be accessed just as you would with normal Ruby libraries.
00:15:19.460 Very few of these libraries require native code—installing them allows you to call them from your Ruby code effortlessly. One example of an application leveraging libraries is a commercial app that utilizes the Batik SVG library for supply chain management.
00:15:42.130 If anyone has played with Redcar, there's an editor that is entirely written in Ruby and makes use of some JRuby libraries for its visual and GUI functions. It includes support for Closure and other languages, all integrated within the same editor.
00:16:00.400 I'd like to demonstrate another example involving JavaFX. JavaFX has been criticized, and rightly so, but it provides a useful library we can call and use directly to create a scene graph containing vectors and 2D/3D elements.
00:16:29.400 In the example of an analog clock, we're creating graphics using basic JavaFX primitives to move targets and draw shapes; these are optimized and rendered through the backend. This, too, is implemented in Ruby.
00:16:55.680 Also, for those who play Minecraft, there's a library named Proven that allows you to write plugins for Minecraft through a clean DSL. An implementation runs in the Minecraft runtime, demonstrating how diverse tools can work in the JRuby environment.
00:17:16.270 In this mini-project, we draw a pyramid in Minecraft using a simple Ruby script executed through the plugin system, showcasing JRuby's synergy with such creative applications.
00:17:28.620 Additionally, there's a library called ThreePence that wraps the Ardor 3D library, allowing for 3D graphics and games to be implemented easily using JRuby. It's available for any platform with JRuby.
00:17:47.190 One of the fun aspects is to see demo applications, like one that demonstrates basic 3D operations such as spinning cubes. These examples display how effectively JRuby can seamlessly interact with Java libraries.
00:18:05.470 Lastly, there’s another entertaining demonstration called the Bubble Mark benchmark, showcasing a bunch of bubbles bouncing around and how efficiently JRuby handles graphics and performance.
00:18:29.460 As you increase the load (e.g., 128 bubbles), JRuby maintains surprisingly high performance, showcasing its capabilities in handling intensive graphical tasks.
00:18:57.520 Now, let’s shift gears a bit. Recently, I was supposed to present on performance topics but realized I pulled up the wrong presentation. So, let's refocus.
00:19:16.180 Returning to libraries, there are tons of effective libraries available for you to work with using JRuby. Manipulating JVM libraries entails very little hassle as most are accessible directly from Ruby.
00:19:30.020 This means you get to leverage a lot of sophisticated tools and libraries while continuing to develop in Ruby, which is incredibly exciting.
00:19:48.220 Looking into garbage collection issues, many have encountered GC-related challenges while using MRI Ruby. Continual pauses can lead to undesirable CPU usage.
00:20:04.070 With JRuby, the JVM provides various options that cater to different loads and scales effectively to multiprocessor systems.
00:20:19.310 The best garbage collectors in the world are implemented on the JVM, which has seen substantial research and optimization improvements throughout the years.
00:20:33.820 When running a demo that exercises both JRuby and MRI, I found that JRuby ran significantly fewer garbage collections overall, indicating better memory management.
00:20:49.040 JRuby runs efficiently and avoids the cumbersome bottlenecks that are common when employing RC Ruby, particularly in larger applications.
00:21:02.720 This makes JRuby an attractive choice as it lends itself well to heavy-duty applications without encountering frequent GC issues.
00:21:16.470 In terms of performance, the threads and concurrency in JRuby are naturally mapped to JVM threads, allowing it to effectively saturate the cores in your system while streamlining requests.
00:21:30.780 In a benchmark comparing single-threaded Ruby and JRuby, the JRuby environment capitalizes on multi-core advantages.
00:21:43.040 Such performance improvements allow JRuby to outshine traditional MRI in multi-threaded workloads gracefully.
00:21:55.270 As we transition to performance tools, it's significant to note that numerous tools exist for JVM profiling for heap analysis, application profiling, and much more.
00:22:09.290 JRuby also integrates with the advanced JMX (Java Management Extensions) framework, enabling you to monitor production applications.
00:22:21.960 Notably, visual VM is an integral tool that facilitates CPU and memory monitoring of JRuby applications.
00:22:36.910 The JVM offers built-in monitoring and basic profiling features that can help you analyze the performance and memory consumption of your applications.
00:22:50.960 Using these tools can provide insight into your application's performance and help pinpoint areas for improvement.
00:23:04.650 JRuby's capability of handling near real-time processing with great efficiency is complemented by these extensive JVM optimization technologies.
00:23:17.310 Overall, JRuby indeed provides significant benefits, particularly surrounding threading, garbage collection, and performance management.
00:23:36.020 Specifically, Java 7's 'invoke dynamic' support allows Ruby programs to execute with performance enhancements similar to static language implementations on the JVM.
00:23:51.650 Furthermore, JRuby’s developments have led to notable performance gains over MRI by optimizing dynamic calls and improving execution speed.
00:24:07.960 As the JRuby team continues to leverage JVM advancements, we’re witnessing a rapid evolution within JRuby, ensuring its place as a leading Ruby runtime.
00:24:24.690 By integrating with native Java libraries like TorqueBox and utilizing JRuby's capabilities, significant performance improvements are realized.
00:24:37.790 For instance, notable facilities such as scalability, performance tweaks, and reduced overhead all contribute toward making JRuby the preferred solution.
00:24:55.560 With JRuby removing the friction often associated with traditional Ruby, there's a wealth of performance-enhancing strategies available for users.
00:25:10.290 As we look at performance benchmarks, JRuby demonstrates its ability to outperform MRI significantly in typical scenarios.
00:25:25.440 Utilizing JRuby within a production-level application can yield a smoother experience, reduced latency, and improved throughput versus MRI.
00:25:41.990 Team discussions and observations have confirmed this behavior and led to the foundation’s ongoing optimization efforts.
00:25:59.030 Overall, deploying Ruby applications via JRuby gives you remarkable performance benefits and less resource overhead.
00:26:14.960 We work on JRuby solely for the benefit of the community and the positive feedback we're receiving validates our direction.
00:26:30.100 The growth we've witnessed, alongside the community's suggestions and input, will only push us further to optimize JRuby.
00:26:45.030 If you have libraries you've been working on, consider using Travis CI, which has been an invaluable resource for cross-Jruby library testing.
00:27:00.010 Finally, if you’re considering migrating existing MRI applications to JRuby, it’s worth knowing that the biggest caveat lies in C extension compatibility.
00:27:12.410 Most applications have evolved in the MRI environment and rely on various C extension libraries, which can create a friction point when trying to switch.
00:27:26.050 You can often find Java replacements for these libraries; many have compatible APIs in JRuby despite the need for some adaptation.
00:27:44.880 Moreover, while start-up time can remain an ongoing issue for JRuby due to the entire implementation compiling at JVM initial cold starts, we’re addressing this.
00:27:58.820 By relying on tools like Nailgun, we can keep the JVM warmed up in the background, thereby significantly reducing runtimes.
00:28:09.750 With JRuby, transitioning should be manageable, keeping in mind to mitigate potential challenges.
00:28:22.580 Now that I’ve spoken about a lot of aspects revolving around JRuby, I’ll open it up to questions.
00:28:35.150 I’m here to help clarify or delve into further information about JRuby and its ecosystem, so please feel free to ask.
00:28:50.040 If you switch from MRI to JRuby, the biggest hurdle you will encounter is the C extension issue. Many applications have a significant reliance on various C libraries. Though JRuby can adapt and substitute many libraries, having an equivalent may require adjustments.
00:29:05.080 Some libraries have Java versions specifically made for JRuby. While adapting to different library APIs may be necessary, most C extension libraries can find an adequate JRuby substitute. Another issue is start-up time, which has improved considerably with JRuby 1.7.
00:29:20.210 We've noted reductions in start-up times of 20-40%, ensuring a gradual improvement trajectory as JRuby matures.
00:29:37.890 In the end, it is crucial to help us shape JRuby into a more performant and functional tool to serve your Ruby development better.
00:29:50.710 I appreciate your time today and for exploring the realms of JRuby with me!
00:30:05.950 Thank you!
Explore all talks recorded at Aloha RubyConf 2012
+13