<Astra1993>
Multiple struct-like structures are defined using different available Ruby constructs (Array, Hash, Struct, Data, Class, OpenStruct)
<Astra1993>
and then the performance of their instantiation is measured
<Astra1993>
my question is why instantiating a class is five-folds slower than instantiating the same structure with a Hash?
<Astra1993>
*three-folds
<Astra1993>
and if the reason is because the class is defined inside of the Ruby code, and the Hash is a native object defined in C, then why Struct (which afaik is also a native construct) is also slower than Hash?
jenrzzz has joined #ruby
<kinduff>
because when you initialize a class you also initialize the object's instance variables, setup the inheritance chain and method table, etc
<kinduff>
Hashes are implemented in C, yes, but its a simple datastructure
<Astra1993>
But everything is an Object in Ruby
<kinduff>
and Structs are also implemented in C btw
<Astra1993>
Ruby has to instantiate an object whether it is instantiating a Hash or a Struct
<kinduff>
yes, but there are differences due to the specifics of how different objects are instantiated and managed internally
<Astra1993>
I personally benchmarked reading attributes from a Class object and dereferencing keys from a Hash object, and the performance was the same (#bmbm with GC disabled beforehand)
Inline has quit [Remote host closed the connection]
<Astra1993>
I was naturally expecting the Class object to be faster (I don't know why, gut feeling I guess) than the Hash
<kinduff>
I doubt that the performance was the same
Inline has joined #ruby
<Astra1993>
Where can I post code? Can I send it here?
<Astra1993>
You're testing instantiation. That's faster with a Hash, by a magnitude of two, on my machine as well. I just tested reading attributes. Let me send the code in a pastebin
<ruby[bot]>
Astra1993: we in #ruby do not like pastebin.com, it loads slowly for most, has ads which are distracting and has terrible formatting. Please use https://gist.github.com or https://dpaste.org/
<kinduff>
Still slower Astra
<kinduff>
I posted the results in my gist. I'm running 3.2.2 tho, what version are you running?
<Astra1993>
3.0.2p107
<Astra1993>
It is slower. but being slower by 1.01x is not as noticable
<Astra1993>
as the instantiation being 2 to 3 times slower
<Astra1993>
I thought attr_accessor was better from a performance perspective than defining a getter in Ruby code
<Astra1993>
(Thanks for the link btw! I'll read it now)
<kinduff>
It is!
<Astra1993>
:|
<kinduff>
Your benchmark shows almost 2x slower if attr_accessor is removed
<kinduff>
and defining a getter ofc
<Astra1993>
Absolutely. I have no contention there
<Astra1993>
What's bothering me is why a Struct isn't as fast as a Hash?!
<Astra1993>
They are both native objects
<kinduff>
doesnt matter if its native or not, what you are doing with a Struct is very different from a Hash
<kinduff>
You add n keys on initialization to a hash, but you need to define them before in a Struct
<kinduff>
this gives a hint of why performance is better, since the hash keys are defined, are set in memory, so its optimal
<kinduff>
rather than storing in memory a whole hash which can be dynamic, variable, etc
<kinduff>
this is what I think, I may be wrong (certainly I am haha)
eddof13 has quit [Quit: eddof13]
jenrzzz has quit [Ping timeout: 268 seconds]
jenrzzz has joined #ruby
<Astra1993>
You're correct, but the attributes are also defined beforehand as well (when the Struct is first defined). It is definitely an implementation detail of Ruby's engine.
<Astra1993>
Anyways, thanks a lot for your answer kinduff. <3
<Astra1993>
Also why was Data created?? Its feature-set could've been easily added to Struct, by adding an `immutable' keyword argument to Struct.new
<Astra1993>
I used to do immutable Structs by overriding #initialize and adding a freeze at the end: https://dpaste.org/eYeHq
cxl_ has quit [Quit: bye]
<Astra1993>
Type multiplicity is how languages become Java! :D
jenrzzz has quit [Ping timeout: 240 seconds]
Linux_Kerio has quit [Ping timeout: 260 seconds]
jenrzzz has joined #ruby
jenrzzz has quit [Ping timeout: 256 seconds]
mmohammadi9812 has joined #ruby
TomyLobo has joined #ruby
Astra1993 has quit [Quit: Client closed]
Astra7 has joined #ruby
entropie has quit [Quit: ""]
Astra7 is now known as Astra1993
entropie has joined #ruby
Astra1993 has quit [Ping timeout: 250 seconds]
eddof13 has joined #ruby
jenrzzz has joined #ruby
jenrzzz has quit [Ping timeout: 246 seconds]
jenrzzz has joined #ruby
Astra71 has joined #ruby
Astra71 is now known as Astra1993
entropie has quit [Ping timeout: 260 seconds]
entropie has joined #ruby
jenrzzz has quit [Ping timeout: 256 seconds]
<kinduff>
np Astra1993 :) was a fun chat
Vonter has quit [Ping timeout: 256 seconds]
Vonter has joined #ruby
eddof13 has quit [Quit: eddof13]
cappy has joined #ruby
eddof13 has joined #ruby
eddof13 has quit [Client Quit]
donofrio has quit [Read error: Connection reset by peer]
donofrio has joined #ruby
donofrio has quit [Remote host closed the connection]
donofrio has joined #ruby
donofrio has quit [Remote host closed the connection]
donofrio has joined #ruby
jenrzzz has joined #ruby
Astra1993 has quit [Ping timeout: 250 seconds]
user71 has quit [Quit: Leaving]
eddof13 has joined #ruby
mmohammadi9812 has quit [Ping timeout: 255 seconds]