support builtin for Kernel#clone - #2954
Conversation
96ce2c6 to
8a1d74c
Compare
|
could you check performance? |
|
Yes, did it. check benchmark, this code. require 'benchmark_driver'
Benchmark.driver do |x|
x.prelude <<~RUBY
obj = Object.new
RUBY
x.report 'origin non freeze', %{ clone = obj.clone }
x.report 'builtin non freeze', %{ clone = obj.patch_clone }
end
Benchmark.driver do |x|
x.prelude <<~RUBY
obj = Object.new
RUBY
x.report 'origin freeze', %{ clone = obj.clone(freeze: true) }
x.report 'builtin freeze', %{ clone = obj.patch_clone(freeze: true) }
endand, result. sh@MyComputer:/mnt/c/users/shunh/desktop/rubydev/benchmark$ ../install/bin/ruby benchmark.rb
Warming up --------------------------------------
origin non freeze 6.152M i/s - 6.218M times in 1.010765s (162.55ns/i, 456clocks/i)
builtin non freeze 5.111M i/s - 5.125M times in 1.002616s (195.65ns/i, 549clocks/i)
Calculating -------------------------------------
origin non freeze 6.775M i/s - 18.456M times in 2.723969s (147.59ns/i, 414clocks/i)
builtin non freeze 5.362M i/s - 15.334M times in 2.859671s (186.50ns/i, 523clocks/i)
Comparison:
origin non freeze: 6775338.0 i/s
builtin non freeze: 5362017.6 i/s - 1.26x slower
Warming up --------------------------------------
origin freeze 3.331M i/s - 3.392M times in 1.018313s (300.19ns/i, 842clocks/i)
builtin freeze 5.016M i/s - 5.033M times in 1.003497s (199.37ns/i, 559clocks/i)
Calculating -------------------------------------
origin freeze 2.999M i/s - 9.994M times in 3.332212s (333.44ns/i, 936clocks/i)
builtin freeze 5.737M i/s - 15.047M times in 2.622901s (174.31ns/i, 489clocks/i)
Comparison:
builtin freeze: 5736928.3 i/s
origin freeze: 2999068.5 i/s - 1.91x slower |
|
But, this patch is not enough. Fail in test code like that. irb(main):001:0> o = Object.new irb(main):002:0> o.clone(freeze: false) Traceback (most recent call last):
6: from ../install/bin/irb:23:in `<main>'
5: from ../install/bin/irb:23:in `load'
4: from /mnt/c/Users/shunh/Desktop/rubydev/install/lib/ruby/gems/2.8.0/gems/irb-1.2.3/exe/irb:11:in `<top (required)>'
3: from (irb):2
2: from <internal:kernel>:27:in `clone'
1: from <internal:kernel>:27:in `initialize_clone'
ArgumentError (wrong number of arguments (given 2, expected 1))
irb(main):003:0> gived |
| { | ||
| if (freeze == Qfalse) return FALSE; | ||
|
|
||
| if (freeze != Qundef && freeze != Qtrue) |
|
|
||
| if (!special_object_p(obj)) | ||
| return mutable_obj_clone(obj, kwfreeze); | ||
| return mutable_obj_clone(obj, kwfreeze); |
|
Please add a benchmark file under |
c757b55 to
226fcc6
Compare
|
Fix |
|
|
|
You have to apply the difference at https://travis-ci.org/github/ruby/ruby/jobs/661078945#L2071-L2101 |
|
kernel.rb has 4-column indents, but the other bundled ruby scripts have 2-column indents. |
6673d8d to
523360d
Compare
523360d to
f6a54e6
Compare
| clone: "object.clone" | ||
| clone_true: "object.clone(freeze: true)" | ||
| clone_false: "object.clone(freeze: false)" | ||
| loop_count: 10000 No newline at end of file |
ab7861d to
54f1a5d
Compare
| clone_true: "object.clone(freeze: true)" | ||
| clone_false: "object.clone(freeze: false)" | ||
| loop_count: 10000 | ||
|
No newline at end of file |
There was a problem hiding this comment.
Sorry, that's typo
| __builtin_rb_obj_clone2(freeze) | ||
| end | ||
| end | ||
|
No newline at end of file |
| int kwfreeze = obj_freeze_opt(freeze); | ||
| if (!special_object_p(obj)) | ||
| return mutable_obj_clone(obj, kwfreeze); | ||
| return mutable_obj_clone(obj, kwfreeze); |
| rb_raise(rb_eArgError, "unexpected value for freeze: %"PRIsVALUE, | ||
| rb_obj_class(kwfreeze)); | ||
| } | ||
| ret = obj_freeze_opt(kwfreeze); |
There was a problem hiding this comment.
Currently only freeze option is used and kwfreeze can't be Qundef here, but it would be safer to check for it.
And trailing spaces.
54f1a5d to
22a3ba0
Compare
fc97a55 to
bb9fc3c
Compare
bb9fc3c to
dc189bd
Compare
using
builtinforKernel#clonemethod.