You can call yield in Ruby without an explicit block declared

(written by lawrence krubner, however indented passages are often quotes). You can contact lawrence at: lawrence@krubner.com

Very interesting:

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!

And:

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

Source