From: nelhage@... Date: 2019-07-25T21:01:00+00:00 Subject: [ruby-core:93916] [Ruby master Feature#15955] UnboundMethod#apply Issue #15955 has been updated by nelhage (Nelson Elhage). Whoops, sorry for the belated response -- Redmine email seems to not be working for me. We have a `replace_method` helper that is a shorthand for doing something like: ``` orig_require = Kernel.instance_method(:require) Kernel.define_method(:require) do |*args| # ��� do some pre-processing orig_require.bind(self).call(*args) end ``` We use it in a number of places, including to replace `require` as part of our custom autoloader (c.f. this talk: https://www.youtube.com/watch?v=lKMOETQAdzs) I'm not quite sure how to get a representative microbenchmark; I am sure I could construct ones where the overhead is anywhere from ~0% to arbitrarily high. Profiling shows that as much as ~6% of the allocation on app startup comes from `UnboundMethod#bind` calls. There are also other places I'd like to use this idiom. We had an incident the other day related to Sorbet's [use of `is_a?`](https://github.com/sorbet/sorbet/blob/d12b7144d2222398e0f28a772b7759fe26b2b9ba/gems/sorbet-runtime/lib/types/types/simple.rb#L20) (a `BasicObject` subtype that was being passed around had a surprising `is_a?` implementation) in runtime checking. I'd love to be able to grab `Object.instance_method(:is_a?)` and then use that in our typechecking to make sure that we can test true subtyping no matter what monkey-patches are in place, but we know from past work that adding even a single allocation to the common case of runtime typechecking is too expensive. ---------------------------------------- Feature #15955: UnboundMethod#apply https://bugs.ruby-lang.org/issues/15955#change-80042 * Author: nelhage (Nelson Elhage) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- I'd love a way to apply an UnboundMethod to a receiver and list of args without having to first `bind` it. I've ended up using `UnboundMethod`s in some hot paths in my application due to our metaprogramming idioms, and the allocation from `.bind` is comparatively expensive. I'd love `unbound_method.apply(obj, args���)` to be equivalent to `unbound_method.bind(obj).call(args���)` but without allocating the intermediate `Method` -- https://bugs.ruby-lang.org/ Unsubscribe: