2009年12月23日星期三

JRuby

JRuby

From Wikipedia, the free encyclopedia

  (Redirected from Jruby)
Jump to: navigation, search
JRuby
Developer(s) Charles Nutter, Thomas Enebo, Ola Bini and Nick Sieger
Stable release 1.4.0 / 2009-11-02; 50 days ago
Written in Ruby and Java
Operating system Cross-platform
Platform Java Virtual Machine
Type Ruby programming language interpreter
License CPL/GPL/LGPL
Website http://www.jruby.org/

JRuby is a Java implementation of the Ruby programming language, being developed by the JRuby team. It is free software released under a three-way CPL/GPL/LGPL license. JRuby is tightly integrated with Java to allow the embedding of the interpreter into any Java application with full two-way access between the Java and the Ruby code (similar to Jython for the Python language).

JRuby's lead developers are Charles Nutter [1], Thomas Enebo [2] Ola Bini [3] and Nick Sieger [4]. In September 2006, Sun Microsystems hired Enebo and Nutter to work on JRuby full time. [1] In June 2007, ThoughtWorks hired Ola Bini to work on Ruby and JRuby.[2] In July 2009, the JRuby developers left Sun to continue JRuby development at Engine Yard. [3]

Contents

[hide]

[edit] History

JRuby was originally created by Jan Arne Petersen, in 2001. At that time and for several years following, the code was a direct port of the Ruby 1.6 C code. With the release of Ruby 1.8.6, an effort began to update JRuby to 1.8.6 features and semantics. Since 2001, several contributors have assisted the project, leading to the current (2008) core team of four members.

The Netbeans Ruby Pack, available since NetBeans 6.0, allows IDE development with Ruby and JRuby, as well as Ruby on Rails for the two implementations of Ruby.[4][5]

JRuby 1.1 added Just-in-time compilation and Ahead-of-time compilation modes to JRuby and was already faster in most cases than the current Ruby 1.8.7 reference implementation[6].

JRuby 1.1.1 is stated to be packaged in Fedora 9[7][8].

Since version 1.1.1, the JRuby team began to issue point releases often to quickly address issues that are brought up by users[9].

On July 2009, the core JRuby developers, Charles Nutter, Thomas Enebo and Nick Sieger, joined Engine Yard to continue JRuby development.[3][10]

JRuby initially supported Ruby MRI 1.8.6, and gradually improved its Ruby 1.9 support[11]. Since 1.4.0 it also support Ruby 1.8.7.

[edit] Rails

JRuby supports Ruby on Rails since version 0.9 (May 2006) [12][13], with the ability to execute RubyGems and WEBrick. Since the hiring of the two lead developers by Sun, Rails compatibility and speed have improved greatly. JRuby version 1.0 successfully passed nearly all of Rails' own test cases[14]. Since then, developers have begun to use JRuby for Rails applications in production environments [15].

[edit] Multiple virtual machine collaboration

On February 27, 2008, Sun Microsystems and the University of Tokyo announced a joint-research project to implement a virtual machine capable of executing more than one Ruby or JRuby application on one interpreter[16].

[edit] Dynamic invocation on Java Virtual Machines

JSR 292 (Supporting Dynamically Typed Languages on the JavaTM Platform) [17] propose to:

  • add a new invokedynamic instruction at the JVM level, to allow method invocation relying on dynamic type checking,
  • to be able to change the classes and methods at runtime dynamically in a production environment.

The Sun Open source project Multi Language Virtual Machine aim to prototype this JSR[18]. The first working prototype, developed as a patch on OpenJDK, was announced and made available on end of August 2008[19][20].

The JRuby team has successfully wired dynamic invocation in their codebase, albeit in a very primitive way. Dynamic invocation shipped with the 1.1.5 release, although being disabled on JVMs without Dynamic invocation capabilities[21].

[edit] Release history

This table present only releases that present significant steps in JRuby history, aside from versions that mainly fixed bugs and improved performance.

Release Release Date Highlights
0.9 2006-08-01 Rails support[12]
1.1 2008-03-28 Performs better than Ruby MRI 1.8.7[6]
AOT mode and JIT mode[22]
1.1.4 2008-08-28 Refactored Java integration layer
Beginning of Ruby 1.9 support
FFI subsystem for calling C libraries[23]
1.2.0[24] 2009-03-16 Ruby 1.9 support almost complete (including JIT compiler)
Preliminary Android support
1.3.0[25] 2009-06-03 JRuby runs in restricted environments better like GAE/J
Performance improvement
1.4.0[26][27] 2009-11-02 Windows Native Launcher and Windows installer
Ruby 1.8.7 support
Improved Ruby 1.9 support

[edit] Design

Since early 2006, the current JRuby core team has endeavored to move JRuby beyond being a simple C port, to support better performance and to aid eventual compilation to Java bytecode. To support this end, the team set an ambitious goal: to be able to run Ruby on Rails unmodified using JRuby. In the process of achieving this goal, the JRuby test suite expanded to such extent that the team gained confidence in the "correctness" of JRuby. As a result, toward the end of 2006 and in the beginning of 2007, they began to commit much more complicated redesigns and refactorings of JRuby's core subsystems.

JRuby is designed to work as a mixed-mode virtual machine for Ruby, where code can be either interpreted directly, just-in-time compiled at runtime to Java bytecode, or ahead-of-time compiled to Java bytecode before execution. Until October 2007, only the interpreted mode supported all Ruby's constructs, but a full AOT/JIT compiler is available since version 1.1[22]. The compiler design allows for interpreted and compiled code to run side-by-side, as well as decompilation to reoptimize and outputting generated bytecode as Java class files.

[edit] Frameworks support

JRuby has built-in support for Rails, RSpec, Rake, and RubyGems. It embeds an FFI subsystem to allow to use C libraries bundled as gems.

It also allows to launch the Interactive Ruby Shell (irb) as Ruby MRI does.

[edit] Programming

[edit] Ruby meets Java

JRuby is essentially the Ruby interpreter, except this version is written entirely in Java. JRuby features some of the same concepts, including object-oriented programming, and duck-typing as Ruby. The key difference is that JRuby is tightly integrated with Java, and can be called directly from Java programs[28].

[edit] Calling Java from JRuby

One powerful feature of JRuby is its ability to invoke the classes of the Java Platform. To do this, one must first load JRuby's Java support, by calling "include Java" ("require 'java'" in earlier versions). The following example creates a Java JFrame with a JLabel:

include Java

frame = javax.swing.JFrame.new()
frame.getContentPane().add(javax.swing.JLabel.new('Hello, World!'))
frame.setDefaultCloseOperation(javax.swing.JFrame::EXIT_ON_CLOSE)
frame.pack()
frame.set_visible(true)

JRuby also allows the user to call Java code using the more Ruby-like underscore method naming and to refer to JavaBean properties as attributes:

frame.content_pane.add label
frame.visible = true

[edit] Calling JRuby from Java

JRuby can just as easily be called from Java, using either the JSR 223[29] Scripting for Java 6 or the Apache Bean Scripting framework. More information on this is available in the JRuby Wiki article.

[edit] Performance

JRuby supports interpreted mode, AOT mode, and JIT mode (the last two modes are available since version 1.1[22]). JRuby evolved from being several times slower than Ruby Reference implementation[30], to being several times faster.[31] [32] Benchmarks as of 16th December 2009, show JRuby using between 2 and 56 times the memory of Ruby MRI.[32]

[edit] Interpreted mode

In this mode, JRuby 1.0 was slower than the C Ruby reference[33]. For example, serving up Rails requests in the standard interpreted mode, JRuby was 50% to 70% slower than C Ruby 1.8. Since then, JRuby performance in interpreted mode has improved a lot. The JRuby team claims that JRuby 1.1.4 is 15%-20% faster in interpreted mode than Ruby MRI [34].

When using Ruby 1.9 (YARV) benchmarks on Java 6, interpreted JRuby 1.0 was 4 times slower than Ruby (including startup time).

[edit] Just-in-time compilation mode

JIT mode is available since JRuby 1.1. In performance benchmarks, JRuby is consistently 200% to 300% faster than C Ruby 1.8.6 [31] but still 15%-25% slower than C Ruby 1.9. However, the JRuby 1.1.6 version outperforms C Ruby 1.9 in some cases [35][36] [37].

Also in a real Mongrel web server application, JRuby performance is better than Ruby (after the Virtual Machine has instantiated)[38].

[edit] See also