Skip to content

Make Message a regular class instead of a Struct#386

Open
moberegger wants to merge 1 commit intodiscourse:mainfrom
moberegger:moberegger/poro-message
Open

Make Message a regular class instead of a Struct#386
moberegger wants to merge 1 commit intodiscourse:mainfrom
moberegger:moberegger/poro-message

Conversation

@moberegger
Copy link
Copy Markdown
Contributor

@moberegger moberegger commented Apr 2, 2026

This is a weird one that I hope isn't too contentious. The initialization of Message was coming up as a CPU hotspot in our production profiles. I didn't think anything of it and figured it was just because it was called so often. Then I noticed it was a Struct and got curious.

I compared the initialization between a regular class and a Struct, and there is a noticeable improvement when using a plain old Ruby class - at least on Ruby 4 with yjit on.

ruby 4.0.2 (2026-03-17 revision d3da9fec82) +YJIT +PRISM [arm64-darwin25]
Warming up --------------------------------------
Message.new (Struct)   879.474k i/100ms
 Message.new (Class)     1.480M i/100ms
Calculating -------------------------------------
Message.new (Struct)     11.474M (± 6.6%) i/s   (87.15 ns/i) -     57.166M in   5.011466s
 Message.new (Class)     18.826M (± 6.2%) i/s   (53.12 ns/i) -     94.715M in   5.056730s

Comparison:
Message.new (Struct): 11473914.4 i/s
 Message.new (Class): 18826156.1 i/s - 1.64x  faster

This PR refactors Message to a regular Ruby class. Included is an implementation of == for use by tests.

@moberegger moberegger marked this pull request as ready for review April 2, 2026 02:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant