From: eregontp@...
Date: 2020-10-29T20:12:03+00:00
Subject: [ruby-core:100660] [Ruby master Feature#17288] Optimize __send__ call with a literal method name

Issue #17288 has been updated by Eregon (Benoit Daloze).


How about a private module instead?
```ruby
class Foo
  module Helpers
    def self.special # General users should not call or rely on `special`
      :special
    end
  end
  private_constant :Helpers

  def foo # even from inside our own class...
    Helpers.special # this won't work without `send`
  end

  class Bar
    # Bar is a helper class, written by us
    def foo 
      Helpers.special # we want to call special, we need to use `send`
    end
  end
end

p Foo.new.foo # => :special
p Foo::Bar.new.foo # => :special

p Foo::Helpers.special # =>  private constant Foo::Helpers referenced (NameError)
```

Refinements seems rather heavy to me for this case, notably it creates extra modules, and makes initial lookups slower (once cached it shouldn't matter much).
For an uncached call (e.g. `refine Object` and many different receivers at some call site), I think the overhead would be noticeable.

Also if the refinements need to be used in multiple files, the module passed to `using` needs to be named, and stored in a private constant.
If done so, there seem little point to `using PrivateHelpers; ...; self.class.foo` vs `PrivateHelpers.foo`, except maybe for instance methods added on existing classes.
But then one could simply use a private method on `Foo` to begin with.

I'm probably biased against refinements because the semantics around refinements + super or eval are fairly messy.

----------------------------------------
Feature #17288: Optimize __send__ call with a literal method name
https://bugs.ruby-lang.org/issues/17288#change-88300

* Author: mrkn (Kenta Murata)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
I made a patch to optimize a `__send__` call with a literal method name.  This optimization replaces a `__send__` method call with a `send` instruction.  The patch is available in [this pull-request](https://github.com/ruby/ruby/pull/3707).

By this change, the redefined `__send__` method is no longer called when it is called by a literal method name.  I guess it is no problem because the following warning message is displayed for a long time.

    $ ruby -e 'def __send__; end'
    -e:1: warning: redefining `__send__' may cause serious problems

This change makes the optimized case x5~x6 faster.  The benchmark result is below:

```
$ make benchmark COMPARE_RUBY="../../ruby/build-o3/ruby" ITEM=vm_send.yml
(snip)
# Iteration per second (i/s)

|             |compare-ruby|built-ruby|
|:------------|-----------:|---------:|
|vm_send      |     18.536M|  113.778M|
|             |           -|     6.14x|
|vm_send_var  |     18.085M|   16.595M|
|             |       1.09x|         -|
```



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>