Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Class.new is all you need

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.
Avatar for Shia Shia
April 22, 2026

Class.new is all you need

RubyKaigi 2026 LT

Avatar for Shia

Shia

April 22, 2026

More Decks by Shia

Other Decks in Technology

Transcript

  1. SIM SANGYONG (@shia) - STORES Inc. - GitHub: @riseshia -

    X: @riseshia - And…Day 3!!! Self Introduction 3
  2. "type-guessr" needed to collect info from project code at startup

    and it was slow. So, I profiled and… Background 4
  3. Target: Initialization speed - Data.define, Struct, Class - kwargs, positional

    args - 1 ivar, 8 ivars Ruby 4.0.1 / linux / no YJIT So, benchmark it 6
  4. - Class.new > Struct.new >> Data.define.new - kwargs are slow

    - more ivars, more slower Observation 8
  5. Observation 9 8 fields - Class.new > Struct.new >> Data.define.new

    - kwargs are slow - more ivars, more slower Note: Class.new outperforms with YJIT
  6. See perf results 10 "perf record -g --call-graph dwarf" Ruby

    4.0.1 / linux / no YJIT / ivar 1 field
  7. - Is kwargs initialization optimized? - Create Hash for kwargs

    on initialization or not - Is ivar access optimized? - Byte code caching with object shape applied or not - Does the object need to be frozen? Why?? 16
  8. shia: "I found it really is.." ko1: "We optimize what

    people use most, so yeah" shia: "That's absolutely true😂" After talk 17
  9. I originally used Data because I vaguely assumed Data/Struct was

    lighter than Class. Reality was different at least object initialization. Did you know that? Summary 18
  10. If "Class" is the fastest, why not re-implement these with

    it?? 🤔🤔🤔 How to speed up Struct or Data.define 21
  11. Re-impl Struct with "Class", and benchmark again: (8 fields, YJIT

    off/on) How to speed up Struct or Data.define 22
  12. 28 You can check the full benchmark & RubyStruct implementation!

    https://github.com/riseshia/class-new-is-all-you-need Class.new might be all you need (if initialization perf matters)