Wednesday, July 8, 2009

Block Assignment Refactoring Fun...

I have been refactoring some of our block dispatch code recently. These changes are currently only for the interpreter, but they can easily be adapted for the compiler later. In fact, I think I can adapt this for our interpreters method parameter assignment and also unify it with our Ruby 1.9 assignment code (for both blocks and methods). But I am getting a head of myself. Why refactor block assignment code?

Block assignment code is one of the ickiest pieces of logic in JRuby and for that matter in Ruby. Lots and lots of conditional logic to pad things out and even weirder logic when your block has one parameter and it is a RubyArray (perhaps not as weird as when it isn't a RubyArray). So there were three goals in the refactoring:
  1. Make each type of block parameter signature it's own code path with little or no conditional logic
  2. Try and make the logic for the strange cases more readable
  3. Try and speed up block invocation

The refactoring is to have InterpretedBlock (and by extension SharedBlock) contain a new attribute called assigner of base type Assigner. A new class Assigner has split-arity call paths and a boxed catch-all assign method. Also, a special RubyArray version assignArray for the most ugly block assignment case:

public abstract class Assigner {
public abstract void assign(Ruby runtime, ThreadContext context, IRubyObject self, Block block);
public abstract void assign(Ruby runtime, ThreadContext context, IRubyObject self, IRubyObject value1, Block block);
public abstract void assign(Ruby runtime, ThreadContext context, IRubyObject self, IRubyObject value1, IRubyObject value2, Block block);
public abstract void assign(Ruby runtime, ThreadContext context, IRubyObject self, IRubyObject value1, IRubyObject value2, IRubyObject value3, Block block);
public abstract void assign(Ruby runtime, ThreadContext context, IRubyObject self, IRubyObject[] values, Block block);
public abstract void assignArray(Ruby runtime, ThreadContext context, IRubyObject self, IRubyObject values, Block block);
// ...
}
For each type of block parameter signature we have a subclass of Assigner which implements these assign methods with the proper minimalist code to do assignment in that case. So for a block which has a single required argument and a rest argument (Pre1Rest1Assigner) we have an unboxed 2 argument assign method which looks like:

public void assign(Ruby runtime, ThreadContext context, IRubyObject self, IRubyObject value1,
IRubyObject value2, Block block) {
parameter1.assign(runtime, context, self, value1, block, false);

rest.assign(runtime, context, self, runtime.newArrayNoCopyLight(value2), block, true);
}
All in all I think the code reads much nicer than it did. There are still some uses of the old code which need to be eliminated in a future refactoring, but I think the code is in better shape than it was.

This along with creating split-arity versions of YieldNode ended up yielding some good results (both running jruby -X-C --server bench/language/bench_yield.rb):

Before After Improvement
1m x10 yield 1 to { } 1.304000 1.139000 ~12%
1m x10 yield to { } 1.253000 1.174000 ~6%
1m x10 yield 1 to {|j| j} 1.572000 1.474000 ~0.6%
1m x10 yield 1,2 to {|j,k| k} 2.228000 1.570000 ~30%
1m x10 yield 1,2,3 to {|j,k,l| k} 2.607000 1.731000 ~33%
1m x10 yield to {|j,k,l| k} 2.921000 1.710000 ~41%
1m x10 yield 1,2,3 to {|*j| j} 3.009000 2.492000 ~17%
1m x10 yield to {1} 1.313000 1.273000 ~0.3%

It is always good when things get faster after a refactoring.... :)

23 comments:

  1. Gemini could use lots of block love. Thanks!
    How do you do the formatted code stuff with blogger?

    ReplyDelete
  2. Very nice post, and excellent perf results. And congrats on a new blog with a reasonable address :) You may want to darken the text or lighten the background a bit though...dark grey on light grey is a little hard to read.

    ReplyDelete
  3. @Logan

    I installed this at a url I could trust:

    http://code.google.com/p/syntaxhighlighter/

    Then added stylesheet mojo to include it.

    ReplyDelete
  4. @Charles

    Ok ok...yeah I will be tweaking this here and there to improve how it looks. Not too much time spent yet...I mostly wanted to make you happy with the short blog url :)

    ReplyDelete
  5. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head.
    I am highly impressed with your blog. It is very nicely explained.
    Your article adds best knowledge to our Java Online Training from India. or learn thru Java Online Training from India Students.

    ReplyDelete
  6. Ooh, this looks dangerous as it includes coding, and courses that include coding scares me a lot. That is why I have switched from computer science to business management. But still, I need custom academic writing services for my help because now I understand the submitting any sort of assignment on time is a hectic task.

    ReplyDelete
  7. this for our interpreters method parameter assignment and also unify it with our Ruby 1.9 assignment code (for both blocks and methods). But I am getting a head of mysel are currently only for the Dissertation Writing Services

    ReplyDelete
  8. Many students want to get high grades and for this purpose, they get help from different writing companies and face issues because they choose wrong companies. They should visit assignment writing services and get high grades because this writing company provides you quality services.

    ReplyDelete
  9. This is really nice which is really cool blog and you have really helped a lot of people who visit the blog and give them useful information.
    Data Science Training in Noida

    ReplyDelete
  10. Informative Post. The information you have posted is very useful and sites you have referred was good. Thanks for sharing.
    Data Science Course with Placement

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. Thanks for sharing an amazing post.
    I really appreciate your writing skills and I find your post useful and informative for students. I am also an educator at Unifolks which is an online portal that provides assignment help and essay writing services to students and other individuals.

    ReplyDelete
  13. Very interesting blog and lot of the blogs we see these days don't provide anything that interests me but i am really interested in this one just thought I would post and let you know.
    Data Science Training Institute in Noida

    ReplyDelete
  14. Interesting post. which i wondered about this issue so thanks for posting and very good article which is a really very nice and useful article. Thank you
    Data Science Course in Noida

    ReplyDelete
  15. Excellent post to make this blog more wonderful, attractive and cool stuff you have. Thank You.
    Data Science Course in India with Placements

    ReplyDelete
  16. Nice Post thank you very much for sharing such a useful information and will definitely saved and revisit your site and i have bookmarked to check out new things frm your post.
    Data Science Course

    ReplyDelete
  17. I like to view your web site which is very useful and excellent resource and truly adored reading your posting. Thank you!
    Data Science Course in Gurgaon

    ReplyDelete
  18. Really this article is truly one of the best in article history and am a collector of old "items" and sometimes read new items if i find them interesting which is one that I found quite fascinating and should be part of my collection. Very good work!
    Data Scientist Course in Gurgaon

    ReplyDelete
  19. I really enjoyed reading this post and keep up the good work and let me know when you can post more articles or where I can find out more on the topic.
    Data Science Online Course

    ReplyDelete
  20. We have been providing professional athe assignmnet help for professional help and also secure their grades under the shade of expert writers. Therefore, to avail of our assignment help offering no plagiarism, Free Turnitin, Partial Payment, Unlimited Revisions, etc. you'll contact our team of highly professional, experienced, Ph.D. experts to avail the discounted services.An avail top btec assignment services online based on real- life around work contexts and scenarios and can include. With the growing academic competition, it has become truly challenging for all the students to leave a good impression on the professor. We didn’t become the students most trusted academic writing help. It took us years of hard work and effort which helped us reach where we are today. Feel free to contact.

    ReplyDelete
  21. Well if you are stucked with your assignment and want online dissertation help or assignment help or any other help related to your academics then do visit us , we are providing the best services with cheap cost as well.

    ReplyDelete
  22. Autumn is probably the best time of the year to visit Arizona. The weather is mild and the crowds are not as bad. This makes it an amazing opportunity for a romantic trip, especially for those who plan to propose.
    Engagement ring Cost

    ReplyDelete
  23. Thank you for this fantastic post.
    I admire your writing abilities and find your post to be useful and informative for students. By the way, many students want to get good grades, so they seek assistance from various writing companies. However, they run into problems when they choose the wrong companies. They should use cheap assignment help uk service to get good grades because this writing company offers high-quality services.

    ReplyDelete