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.... :)


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

  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.

  3. @Logan

    I installed this at a url I could trust:

    Then added stylesheet mojo to include it.

  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 :)

  5. 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.

  6. 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

  7. 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.

  8. This comment has been removed by the author.

  9. 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.

  10. 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

  11. 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.

  12. 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.

  13. 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

  14. 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.

  15. This blog now has really thought me so much stuff that I still have to know concerning writing. I sincerely thank you for the time created to put on this wonderful article. Thanks so much for sharing. unn jupeb admission

  16. Thanks for sharing an informative and useful blogs. It was written in an simple and simple and elegant way. The blog was really awesome and was easy to learn. I have enjoyed a lot by reading your beautiful blogs and articles. So continue sharing the beautiful blogs always with us.
    chapter 7 lawyer near me

  17. Thanks for sharing this helpful wonderful post.your blog is truly faultless and unique.
    Abogado Criminal Federal de Virginia

  18. When I was the fifth semester of the university,I have to faced many difficulties for my programming assignment because,I have weaked at programming my logics are not build perfectly so that I asked my friend he was good at programming, my friend help me to submit my assignment,after that I have to understand how to write the best assignment,If any one have interested to buy a dissertation paper service then you can easily contact with me,I have provided the best solution.

  19. Beyond just completing tasks, the goal of the "GED test help services" service is to promote genuine academic growth, ensuring students grasp concepts and develop critical thinking skills.

  20. "Block Assignment Refactoring Fun!" will take you on a coding adventure. Investigate novel approaches to improve your codebase using refactoring techniques. With our interesting and practical recommendations, you'll be able to dive into the realm of clean, efficient, and maintainable code. Improve your programming skills while having fun with refactoring. Let the coding adventure begin! reckless driving in virginiaThank you for sharing your knowledge! Keep up the good job! Continue to spread the word. Please take a peek at my website.

  21. These are especially useful when initializing or updating multiple variables with related values. This approach aligns with clean coding principles, contributing to a more concise and comprehensible codebase, ultimately making it easier for developers to manage and collaborate on the code. family lawyer near me

  22. Thank you for posting such a great Work! It contains wonderful and helpful posts. abogados divorcio chantilly va

  23. A fascinating tour into the nuances of code optimization can be found in "Block Assignment Refactoring Fun". The author skillfully guides readers through the process of refactoring, converting large, unwieldy chunks of code into elegant, functional solutions, all while blending technical know-how with comedy. Every chapter provides useful advice and real-world examples, enabling even the most experienced engineers able to understand difficult ideas. Using sophisticated refactoring techniques and identifying code smells, this book gives readers the tools they need to become more proficient programmers. "Block Assignment Refactoring Fun" is a must-read for anyone hoping to become an expert in code optimization because of its captivating story and useful tips.
    motorcycle accident i 95 virginia