Why blocks make Ruby methods slower
omniref.com
omniref.com
I think they buried the lede though. (Edit: not complaining! I like tech taught via a discovery narrative like they use here.) Given this:
def a(&block)
yield
end
def b
yield
end
Running `a {1+1}` is 4x slower than running `b {1+1}`. I didn't even know you could yield in a method without an explicit &block parameter. I'll guess I'll change my ways then, although I like having the block declared in the method signature so I know at a glance I can pass one. Too bad!So I understand from the article that the slowness is because of extra memory allocation etc., but I still don't understand why. I mean, if both methods behave the same, what is the difference? Is it because declaring &block means my code in the method body can now "do stuff" with `block`, whereas in the implicit case I don't have a reference so I can't do any funny business but just yield? That would make sense.
That's exactly it. With `&block`, you get a reified Proc on which you can do all kinds of stuff[0] and which you can move around. With a `yield` and an implicit block, you can't do any of that, you can just call the block. So the interpreter can bypass all the Proc reification as it knows the developer won't be able to use it, it can keep just a C-level structure instead of having to setup a Ruby-level object.
There's a not-completely-dissimilar situation with `arguments` in javascript: if you're using it, the runtime has to leave a number of optimisations off because you can do much, much more with it than with just formal arguments.
And keep in mind MRI/YARV is a relatively straightforward bytecode interpreter. It'd be interesting to see how Rubinius or JRuby fare: the should be able to compile away the reification if it's not necessary (though there's a trap: they may also have plain cheaper procs which they don't need to optimise away).
[0] http://ruby-doc.org//core-2.2.0/Proc.html has plenty of fun methods, which you can only access via a reified proc
It's real easy to test too: just bench
def a flag
Proc.new if flag
nil
end
with the flag set to `true` or `false`. Note that the proc is never actually called or returned to a possible caller, the only difference is that one reifies the proc and the other one not.You'll get roughly the same 5x difference (with `flag=false` being the fastest) as TFA notes.
Using &block in the signature does not indicate that the method yields, it indicates that the method reifies a Proc. To me, its a sign that the method does something which makes it likely that the passed block will be called outside of the usual lifetime of the called method.
Now, it may be an issue that Ruby has no self-documenting signature element which indicates that a method expects a block for the purpose of yielding without reification, but overloading the signature element which is for reification doesn't really address that (and, destroys one element of self-documentation in order to create another one.)
Furthermore, if you pass it a block with & on the call-side, it's not really reifying a block anyways (even if there is still a performance penalty, which I've never checked), so it's not even true that's what it means in general.
The truth is all functions take a block in ruby. Only some of them do anything with it. Given that, indicating that you take a block with the &block argument seems entirely reasonable to me, if you don't mind the performance hit.
Code is read from top to bottom, and finding out that a method uses a block after reading through most of it, is confusing, whereas `&block` in the method signature clarifies that this method will do something with a block.
The performance hit, as bad as it may be, is not something that should be a concern in many programs. After all, you are using Ruby...
- def a(&block)
+ def a #&block
This works when a method uses `yield` and doesn't do more with the block parameter.Ideally Ruby would have a way to write a method signature to indicate a block is expected and/or or a yield happens, all without triggering the transformation of the block into a first class object.
Also I'm a guy who took a Smalltalk class taught by a passionate teacher back when I was getting my degree.
Smalltalk is one of the main parents of ruby. It's less practical then ruby in many ways but its more pure/simpler.
To me ruby's blocks/yield is one of the ugliest of parts of ruby. In Smalltalk just about everything is objects and messages. In Smalltalk Blocks are just special syntax for BlockClosure objects that you evaluate by sending the 'value' method on them and they don't have to be passed at the end, you can pass multiple blocks(if you want ideas of why you might want to do this see some of the c# linq overloads: https://msdn.microsoft.com/en-us/library/system.collections.... (or some thread/future like thing with a run action and an exception callback) although I admit many of them can be done with an extra map function.
Ruby Blocks aren't objects, yield is not a method, it's harder to assign/store closure to variables and pass them around.
I may be wrong but I was under the impression that blocks are the way they are because they are a speed hack, the original ruby being a simple interpreter and blocks allowed the common pattern of defining and passing a closure to the method calling the closure without overhead of allocating/initializing/gc a block closure object.
Other objections ruby doesn't seem to be a language that the number of arguments in the function declaration doesn't bear resemblance to the number of arguments you can pass ala javascript I don't see why blocks should be special, I don't want to glance at the body of the function to see if it expects a block or not.
Also if you want to overload on block_given? you can use a named/default argument at the end defaulting to nil.
Uh what?
1. File#open is a special implementation, the canonical opening of a file is File#new
2. with-open-file is just a wrapper macro around unwind-protect/open/close
Not much. They just optimized the yield case so its basically skips the red tape.
"As it turns out, MRI has optimized this common case, providing an entirely separate pathway, from parsing through execution, for blockless yields (it's actually a primitive in YARV, the meta-language to which Ruby is compiled at execution time). Because of this special pathway, calls to yield end up being handled by a dedicated C function in the interpreter (vm_invoke_block in vm_inshelper.c, if you're interested), and are much faster than their explicitly blocked bretheren"
But in this specific case, the code could go through data flow analysis and the value passed to block determined never to escape the function, so that callers could save creating a proc. But late-bound callers may still pass an explicit proc, so the body of the function would need to work with both forms. It's not a trivial optimization.
As another poster points out, JRuby seems to have a much lower relative overhead (~1.13x, IIRC), so there may or may not be optimization concepts there that can eventually be applied to MRI.
Python has a similar performance impact for closures. Defining a lambda forces local variables onto the heap, even if the lambda is never instantiated, e.g. when defined inside an infrequently true condition.
Well, up to 4x. If you're doing anything interesting or complex, or, god forbid, making a database call, then this might turn into an instance of that famous anecdote where Bill Gates wouldn't even bend over to pick up a thousand dollars laying on the ground.
Is there anyway to search through all of these types of source code annotations across the site?
I wanted to learn about method dispatch in Ruby, there was a post earlier on HN about this using OmniRef, I don't know how to retrieve it from the site directly.
I think this is the method dispatch one you're wanting:
https://www.omniref.com/ruby/2.2.0/files/method.h?#annotatio...
http://patshaughnessy.net/ruby-under-a-microscope
http://www.amazon.com/Ruby-Under-Microscope-Illustrated-Inte...
Profiling is essential when figuring out how to improve your code. What we're doing here is explaining one weird benchmark result.
"Why blocks make Ruby methods 439% slower" implies something that isn't happening here, at least to me.
Apparently, this is due to overhead from creating a Proc object. The performance is better with blocks in JRuby, and the same will apply to MRI 2.3 when that gets released.
What happens if you're halfway through executing this method, you've not allocated the block, and somebody wants to start a debugger? Then you have you go back and somehow recreate the Proc as if it was always there. Same thing if someone uses ObjectSpace to look for all live Procs.
Yes. Otherwise, you'd have to have separate "being debugged" and "not being debugged" runtimes that keep track of different information and do different things, and it's not unlikely that you'd end up with programs doing different things as a result. You can actually access debugger information from inside the program, and it's not that uncommon to do that.
Taking that a step further, you've started in Ruby, it was fast enough, now you have a performance issue. Your options are: ignore it; make Ruby faster; use a faster idiom; somehow push this off to a C or Java extension; or rewrite in a new language. Rewrites have a spectacular failure rate. Ignoring it is bad for business. Ruby is already implemented in C and Java, so if you're going to go down the extension path to make a core language feature faster, you may as well just make Ruby faster -- it's virtually the same effort. And using faster idioms is just the lowest cost option.
Languages are not the same as implementations, and languages are not fast or slow.
That said, the question just becomes "why would you use MRI if you care about performance?"