[ruby-core:77927] [Ruby trunk Feature#12901] Anonymous functions without scope lookup overhead
From:
rdmosolgo@...
Date:
2016-11-04 18:01:38 UTC
List:
ruby-core #77927
Issue #12901 has been updated by Robert Mosolgo.
Much +1 for this feature, I used anonymous functions extensively in graphql-ruby until I learned about this overhead. We still have lots of APIs where the user input is a callable, and I would love to be able to advise procs in those cases.
In my case, implementing this feature as an optimization would be great.
I'd be happy to test a patch if/when one is available.
My experience with Ruby source is limited to the C extension API, but if someone can advise _where_ this optimization should be implemented, I'd love to take a look and see if I can learn enough to make it work!
----------------------------------------
Feature #12901: Anonymous functions without scope lookup overhead
https://bugs.ruby-lang.org/issues/12901#change-61241
* Author: Richard Schneeman
* Status: Open
* Priority: Normal
* Assignee:
----------------------------------------
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: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>