From: marcandre-ruby-core@... Date: 2020-10-28T20:48:14+00:00 Subject: [ruby-core:100624] [Ruby master Feature#17288] Optimize __send__ call with a literal method name Issue #17288 has been updated by marcandre (Marc-Andre Lafortune). shyouhei (Shyouhei Urabe) wrote in #note-4: > Private methods shall not be called at the first place. Period. That is breaking encapsulation. I wish that was the case, but Ruby access is *not expressive enough* for this to be the case. ```ruby class Foo class << self private def special # General users should not call or rely on `special` end end def foo # even from inside our own class... self.class.special # this won't work without `send` end class Bar # Bar is a helper class, written by us def foo Foo.special # we want to call special, we need to use `send` end end end ``` Note: using `protected` changes nothing in this example In so many gems, you have to either mark method as `private` and use `send`, or else use `# @api private` but that's just a comment. ---------------------------------------- Feature #17288: Optimize __send__ call with a literal method name https://bugs.ruby-lang.org/issues/17288#change-88257 * 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: