Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement key conversions with symbol by Java within Date._strptime #4676

Closed

Conversation

muga
Copy link
Contributor

@muga muga commented Jun 18, 2017

To improve performance of Java strptime implemented on #4635, this PR fixes Date._strptime to implement key conversions with symbol in Java. Because It reduces the count of objects passing between JRuby and Java.

a RubyDateParser object in ThreadContext to reduce the cost of RubyDateParser object creations. The cache is basically similar approach to RubyDateFormatter in ThreadContext. And this PR assumes #4675.

The following is my micro benchmark based on https://github.com/macournoyer/tinyrb. The performance by this PR is 2.5 times faster maximumly.

1,000 times Date._strptime('2001-02-03', '%Y-%m-%d')
                     user     system      total        real
mri 2.4.1        0.000000   0.000000   0.070000 (  0.086790)
jruby 9.1.9.0    0.000000   0.000000   8.020000 (  3.789310)
jruby (master)   0.000000   0.000000   7.290000 (  4.043696)

10,000 times Date._strptime('2001-02-03', '%Y-%m-%d')
                     user     system      total        real
mri 2.4.1        0.000000   0.000000   0.090000 (  0.142577)
jruby 9.1.9.0    0.000000   0.000000  12.450000 (  7.671689)
jruby (master)   0.000000   0.000000   8.180000 (  4.401070)

100,000 times Date._strptime('2001-02-03', '%Y-%m-%d')
                     user     system      total        real
mri 2.4.1        0.000000   0.000000   0.200000 (  0.209632)
jruby 9.1.9.0    0.000000   0.000000  17.420000 ( 10.452343)
jruby (master)   0.000000   0.000000   9.010000 (  4.304226)

1,000,000 times Date._strptime('2001-02-03', '%Y-%m-%d')
                     user     system      total        real
mri 2.4.1        0.000000   0.000000   1.260000 (  1.362743)
jruby 9.1.9.0    0.000000   0.000000  60.650000 ( 59.166195)
jruby (master)   0.000000   0.000000  12.170000 (  7.638632)

10,000,000 times Date._strptime('2001-02-03', '%Y-%m-%d')
                     user     system      total        real
mri 2.4.1        0.000000   0.000000  11.970000 ( 12.952664)
jruby 9.1.9.0    0.000000   0.000000 482.880000 (516.068042)
jruby (master)   0.000000   0.000000  36.650000 ( 35.192336)

The raw test data: https://gist.github.com/muga/e566299fc9afdaa0511a8bb7804d5a7f

parser = org.jruby.util.RubyDateParser.new
map = parser.parse(JRuby.runtime.current_context, fmt, str)
return map.nil? ? nil : map.to_hash.inject({}){|hash,(k,v)| hash[k.to_sym] = v; hash}
return JRuby.runtime.current_context.getRubyDateParser.parseAndCreateHash(JRuby.runtime.current_context, fmt, str)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quite an ugly design to get current_context to pass it current_context. maybe this whole method should be/delegate to a native ext impl.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@muga I think you could make parser.parse just return the symboled hash right? Then @kares comment about currentContext (and having to know there is a cache) can be encapsulated by the parse.

As a broader comment. A Bag is made then a Map is made and then a Hash with Symbols is made. I have not viewed how much a Bag modifies its data but I wondered if you could just drop the bag and the map and just use a Hash with symbols directly as you parse? I can see some post processing happening at end of bag and when map is made but I just wondered about whether this could just be one thing or not.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@muga @kares oh I misread this a bit...I agree with @kares that if we had this as an Ext the def self._strptime would just be an @JRubyMethod() in Java.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@muga @kares I am implementing _strptime as native ext since I think I can do this a faster (in fact I think I am already done). This will invalidate this PR but I will point out which commit it is in.

@enebo enebo added this to the Won't Fix milestone Jun 19, 2017
@enebo
Copy link
Member

enebo commented Jun 19, 2017

We can consider caching part of this in the other PR or in a new one but I implemented the other bits and pushed the result in 4323f93. You can read that commit for the improvement. @muga this duplicated your strategy but just differently using a native ext as @kares suggested.

@enebo enebo closed this Jun 19, 2017
@muga
Copy link
Contributor Author

muga commented Jun 19, 2017

@kares @enebo Thank you for suggesting and fixing. I totally agree with your thoughts and 4323f93.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants