Skip to content

Avoid creating Struct at runtime - #3492

Open
byroot wants to merge 1 commit into
aws:version-3from
byroot:runtime-class-creation
Open

byroot wants to merge 1 commit into
aws:version-3from
byroot:runtime-class-creation

Conversation

@byroot

@byroot byroot commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Struct.new create a new class and define new methods, which isn't something you want to happen at runtime as it flushes some VM caches, and can generate an unbounded amount of methods, which may be compiled by the JIT.

Since these Struct hold configuration data, we can define a simpler class that rely on method_missing. Attribute access are very slightly slower, but negligible for a network API client, and much more friendly to the Ruby VM.

In our app, aws-sdk is by far the top source of runtime class creation:
Screenshot 2026-11-06 at 15 13 43

@byroot
byroot requested review from a team as code owners November 6, 2026 14:15
@byroot
byroot force-pushed the runtime-class-creation branch from a22ab15 to b35d5d8 Compare November 6, 2026 14:16
`Struct.new` create a new class and define new methods, which isn't
something you want to happen at runtime as it flushes some VM caches,
and can generate an unbounded amount of methods, which may be compiled
by the JIT.

Since these Struct hold configuration data, we can define a simpler
class that rely on `method_missing`. Attribute access are very sligthly
slower, but negligible for a network API client, and much more friendly
to the Ruby VM.
@byroot
byroot force-pushed the runtime-class-creation branch from b35d5d8 to 5d7345c Compare November 9, 2026 09:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant