[ruby-core:113750] [Ruby master Misc#19693] Data initialization is significantly slower than Struct
From:
janosch-x via ruby-core <ruby-core@...>
Date:
2023-06-03 11:13:12 UTC
List:
ruby-core #113750
Issue #19693 has been updated by janosch-x (Janosch M=FCller).
@byroot
nice idea! i've [tried it](https://gist.github.com/jaynetics/0d04eb85177140=
e2a84fc88aeea68764). it still doubles (instead of triples) kw init speed, a=
t the cost of halving speed when initializing with list args:
```
orig list: 7136048.0 i/s
patch list: 3115468.7 i/s - 2.29x slower
patch kw: 2956280.5 i/s - 2.41x slower
orig kw: 1621000.3 i/s - 4.40x slower
```
> I'm not sure is spec compliant, as it would allow to call Foo.new("a", a:=
"also_a")
i think this one test, for structs with `keyword_init: true`, will fail: ht=
tps://github.com/ruby/ruby/blame/master/test/ruby/test_struct.rb#L116
however, this gives me the idea that a simple Ruby keyword initializer (`de=
f self.new(a: nil, b: nil); orig_new(a, b) end`) could be auto-defined, and=
speed tripled, if and only if `keyword_init: true` is given. this should b=
e spec compliant. structs with `keyword_init: true` already raise an arity =
error when given list args.=20
then again, i assume the idea is to rather fade out the `keyword_init` opti=
on, which doesn't even exist for `Data`?
----------------------------------------
Misc #19693: Data initialization is significantly slower than Struct
https://bugs.ruby-lang.org/issues/19693#change-103397
* Author: janosch-x (Janosch M=FCller)
* Status: Closed
* Priority: Normal
----------------------------------------
Maybe there is potential to make it as fast as Struct?
```ruby
require 'benchmark/ips'
S =3D Struct.new(:a, :b, :c, :d, :e, :f, :g, :h, :i, :j)
D =3D Data.define(:a, :b, :c, :d, :e, :f, :g, :h, :i, :j)
Benchmark.ips do |x|
x.report('Struct') { S.new(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) }
x.report('Data') { D.new(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) }
x.compare!
end; 1
# =3D> [...]
# =3D> Struct: 6916530.4 i/s
# =3D> Data: 1507259.5 i/s - 4.59x slower
```
--=20
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-c=
ore.ml.ruby-lang.org/