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:
- Make each type of block parameter signature it's own code path with little or no conditional logic
- Try and make the logic for the strange cases more readable
- 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:
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 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);
// ...
}
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.
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);
}
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.... :)
Gemini could use lots of block love. Thanks!
ReplyDeleteHow do you do the formatted code stuff with blogger?
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@Logan
ReplyDeleteI installed this at a url I could trust:
http://code.google.com/p/syntaxhighlighter/
Then added stylesheet mojo to include it.
@Charles
ReplyDeleteOk 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 :)
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.
ReplyDeletethis 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
ReplyDeleteMany 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.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThanks for sharing an amazing post.
ReplyDeleteI 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.
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.
ReplyDeleteData Science Training Institute in Noida
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.
ReplyDeleteWell 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.
ReplyDeleteAutumn 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.
ReplyDeleteEngagement ring Cost
Thank you for this fantastic post.
ReplyDeleteI 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.
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
ReplyDeleteThanks 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.
ReplyDeletechapter 7 lawyer near me
Thanks for sharing this helpful wonderful post.your blog is truly faultless and unique.
ReplyDeleteAbogado Criminal Federal de Virginia
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.
ReplyDelete