Skip to content

Avoid creating Struct at runtime - #3492

Open
byroot wants to merge 2 commits into
aws:version-3from
byroot:runtime-class-creation
Open

byroot wants to merge 2 commits 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 2 times, most recently from b35d5d8 to 5d7345c Compare November 9, 2026 09:23
`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.
@jterapin
jterapin force-pushed the runtime-class-creation branch from 5d7345c to 571325a Compare November 16, 2026 20:44
@jterapin

Copy link
Copy Markdown
Contributor

Hi @byroot,

Thanks for working on this! I’m also rethinking how we use Structs in the SDK for the next major version.

While testing the change across the SDK, I found two existing paths that need a little more compatibility from DynamicStruct:

  • S3 multipart copy with copy_source_region calls each_pair on the client configuration.
  • SWF duplicates the configuration before applying its request-specific polling timeout, so the duplicate needs independent attribute storage.

I tested adding each_pair and initialize_copy, along with regression coverage for both paths. The core, S3, and SWF test suites pass with those additions.

Would you be able to run your original benchmark or workload with these changes and confirm that they still address the runtime class-generation issue?

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.

2 participants