8 Aug 23:09
Re: Does &method(:foo) get inlined? Can it? Should it?
Evan Phoenix <evan <at> fallingsnow.net>
2011-08-08 21:09:24 GMT
2011-08-08 21:09:24 GMT
I think that using this style is certainly possible, but fairly obtuse to most rubyists. If you never wrote blocks and instead only wrote these &method(:blah) isms, the other programmers on your team would get frustrated with you. That being said, you asked a technical question rather than a stylistic one, so I'll answer it technically. You're assumption that it could be the same as a block is, I'm sad to say, dead wrong. There reason you don't see Method#to_proc and such in profiling is 2 fold: 1) Most (all?) MRI profilers do not show a methods that MRI defines in C, so they'd never show up. 2) The mechanism for activating a method that has been turned into a Proc is all in C, so the overhead is invisible on the invocation side too. You're point about the arty differences are right on. Additionally, you're thinking that a VM could easily optimize it into a block is quite wrong. Object#method is a not specially, not something that would be detected and optimized away. Additionally, even with runtime optimizations, something like escape analysis is still required since #method returns a Method object that you'd have to see inside and extract the information from. On the invocation side, the invoked method can only do something special with the block in the case of block inlining, an optimization that only Rubinius has. So to get to your questions: 1. Does Rubinius optimize this code? No. Could it? Yes, but it's hardly easy. 2. In time it could, yes. 3. In time it should, yes. - Evan -- --(Continue reading)
RSS Feed