Methods defined with `define_method` are slower to call
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 42/100
- Issue type
- Documentation
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- ruby
- Domain
- documentation, performance
Research direction
Start by locating the existing comparison of define_method and module_eval in the repository, then review the benchmark shown in the issue. Done means documenting that calling methods created with define_method is slower, alongside the existing method-definition comparison and its performance tradeoff.
Written by the indexing model from the issue text.
Description
While define_method indeed defines methods slighly faster than module_eval, I think it should be noted that calling methods defined with define_method is slower than calling module_eval ones (because define_method creates a closure):
require "benchmark/ips"
object = Class.new {
module_eval "def evaled_method; end"
define_method(:defined_method) {}
}.new
Benchmark.ips do |x|
x.report("module_eval") { object.evaled_method }
x.report("define_method") { object.defined_method }
x.compare!
end
Calculating -------------------------------------
module_eval 98.664k i/100ms
define_method 93.809k i/100ms
-------------------------------------------------
module_eval 6.794M (± 8.1%) i/s - 33.743M
define_method 4.588M (± 6.7%) i/s - 22.889M
Comparison:
module_eval: 6793763.3 i/s
define_method: 4587570.1 i/s - 1.48x slower
I think you benefit more from a method being faster to call than faster to define.
- Dominant language
- Ruby
- Stars
- 5.7k
- Forks
- 370
- PR merge metrics
- No merged PRs in 30d
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from fastruby/fast-ruby
-
Difficulty 2/5 1-3 hours Newbie friendliness 52/100
-
Difficulty 4/5 3-5 days Newbie friendliness 32/100
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 45/100
-
Difficulty 4/5 3-5 days Newbie friendliness 42/100
All issues in fastruby/fast-ruby
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
palladius/rails8-app-on-gcp#145 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
rubocop/rubocop-rspec#2236 ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
riscv/riscv-unified-db#2624 · 1 reaction ·
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100