JRuby

/o regexp modifier (Ruby compatibility)

Details

  • Testcase included:
    yes
  • Number of attachments :
    3

Description

Ruby regular expressions provide a /o option with the following meaning (from Pickaxe, forgive me):

o - Substitute Once.
Any #... substitutions in a particular regular expression literal will be performed just once, the first time it is evaluated. Otherwise, the substitutions will be performed every time the literal generates a Regexp object.

JRuby currently ignores the modifier, which results in lower performance when a library is counting on the feature. For example, CodeRay (http://coderay.rubyforge.org/) uses it heavily, and runs about 10,000 times slower with jruby 1.1b1.

Testing the feature is simple:

{{assert Array('a'..'z').map { |c| c.to_s/#{c}/o }.compact.size == 1}}

  1. JRUBY-1552a.patch
    11/Nov/07 12:30 PM
    3 kB
    Joshua Graham
  2. JRUBY-1552b.patch
    16/Nov/07 2:42 AM
    2 kB
    Joshua Graham
  3. JRUBY-1552test.patch
    16/Nov/07 3:00 PM
    0.4 kB
    Joshua Graham

Activity

Hide
Charles Oliver Nutter added a comment -

This shouldn't be hard to fix for 1.1 and 1.0.3 (easy backport for the 1.0 interpreter).

Show
Charles Oliver Nutter added a comment - This shouldn't be hard to fix for 1.1 and 1.0.3 (easy backport for the 1.0 interpreter).
Hide
Joshua Graham added a comment -

Initial patch for compiler off mode.

Tested with:

jruby -C -e "10.times {|i| puts i.to_s/#{i}/o }"

Should now return:

0
nil
nil
nil
nil
nil
nil
nil
nil
nil

Show
Joshua Graham added a comment - Initial patch for compiler off mode. Tested with: jruby -C -e "10.times {|i| puts i.to_s/#{i}/o }" Should now return: 0 nil nil nil nil nil nil nil nil nil
Hide
Joshua Graham added a comment -

btw, patch for 1.1

Pretty much do the same thing as done to ASTInterpreter to EvaluationState in 1.0 (I don't have the source handy)

Show
Joshua Graham added a comment - btw, patch for 1.1 Pretty much do the same thing as done to ASTInterpreter to EvaluationState in 1.0 (I don't have the source handy)
Hide
Joshua Graham added a comment -

This alters the compiler to achieve the same end.

It lends from the optimisation in the ByteList processing RegExp which checks to see if it had already created a RegExp from the same ByteList.

In this alteration, we check to see if the source specified the /o flag. If it did, we store the first evaluation and then jump on subsequent evaluations, so the closure is, in effect, only evaluated once at runtime.

Show
Joshua Graham added a comment - This alters the compiler to achieve the same end. It lends from the optimisation in the ByteList processing RegExp which checks to see if it had already created a RegExp from the same ByteList. In this alteration, we check to see if the source specified the /o flag. If it did, we store the first evaluation and then jump on subsequent evaluations, so the closure is, in effect, only evaluated once at runtime.
Hide
Joshua Graham added a comment -

And an extra testRegexp.rb minirunit test to check behaviour (with and without /o flag)

Show
Joshua Graham added a comment - And an extra testRegexp.rb minirunit test to check behaviour (with and without /o flag)
Hide
Charles Oliver Nutter added a comment -

Patches look good, thanks Josh! Fixed on trunk in 4952 and on 1.0 branch in 4953.

Show
Charles Oliver Nutter added a comment - Patches look good, thanks Josh! Fixed on trunk in 4952 and on 1.0 branch in 4953.

People

Vote (0)
Watch (2)

Dates

  • Created:
    Updated:
    Resolved: