From: eregontp@... Date: 2017-03-01T11:11:08+00:00 Subject: [ruby-core:79841] [Ruby trunk Feature#12901] Anonymous functions without scope lookup overhead Issue #12901 has been updated by Benoit Daloze. Shyouhei Urabe wrote: > Let me ask a (possibly stupid) question. Consider this scenario: > > 1. create a proc. > 2. it gets optimized. > 3. lots of calculations goes on without variable captures. > 4. obtain a binding from it (via deoptimization). > > now, should what be visible from the obtained binding? Should we re-calculate the optimized-out variables? I assume you mean a binding via Kernel#binding. I think that's the general contract of deoptimization: it must not be observable from the user. So at the deoptimization point, the current computations must be stored in a real frame, or be recomputed if they are side-effect free and the deoptimization implements that. If it is a Proc#binding, then it only captures variables defined outside the Proc, so variables only defined inside the Proc are not visible to that. With the technique I mentioned, writes to captured variables can only be optimized out if the surrounding scope is compiled as well. ---------------------------------------- Feature #12901: Anonymous functions without scope lookup overhead https://bugs.ruby-lang.org/issues/12901#change-63276 * Author: Richard Schneeman * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- Right now if you are writing performance sensitive code you can get a performance bump by not using anonymous functions (procs, lambdas, blocks) due to the scope lookup overhead. https://twitter.com/apotonick/status/791167782532493312?lang=en I would like to be able to create an anonymous function and specify I don't need it to perform the lookup when called. I am thinking that this code: ~~~ Proc.new(scope: false) {|var| puts var } ~~~ Would be the equivalent of ~~~ def anon(var) puts var end ~~~ If you call it while trying to access something from outside of the scope it would error ~~~ var = "hello" Proc.new(scope: false) { puts var }.call # => NameError: undefined local variable or method `var' for main:Object ~~~ An example of a case where this could be used is found in https://github.com/rails/sprockets/pull/417. In this case we are getting rid of anonymous functions in favor of a method created with refinements. This solution works but it was not an obvious optimization. It would be convenient to have a syntax for defining anonymous functions that do not need access to caller scope. -- https://bugs.ruby-lang.org/ Unsubscribe: