Skip to content

Allocate shape metadata on first write (3,800 fewer empty Hashes when loading S3 and KMS) - #3491

Merged
jterapin merged 1 commit into
aws:version-3from
czj:perf/lazy-shape-metadata
Nov 16, 2026
Merged

jterapin merged 1 commit into
aws:version-3from
czj:perf/lazy-shape-metadata

Conversation

@czj

@czj czj commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Every Seahorse::Model::Shapes::Shape and ShapeRef creates an empty metadata Hash in initialize, though most of them never get any metadata. This creates it on the first []= instead, and [] reads a missing Hash as no metadata. It's a small one, next to #3465: about 0.6 MB less per process for S3 and KMS, and the same for every other service API an app loads.

Loading the S3 and KMS APIs (ClientApi::API), on version-3 at 1419898 and with this change, Ruby 4.0.9:

version-3 this PR
unfrozen empty Hashes left after load 4,186 353
objects allocated while loading 200,783 196,950
ObjectSpace.memsize_of_all 6.06 MB 5.48 MB

Load time doesn't change (within noise, interpreter and YJIT). Query-protocol responses (STS, SQS, SNS, IAM) also allocate 3 Hashes less each, since Query::Handler#rules builds a shape and two refs per response.

Every metadata read on the S3 and KMS models (4,273 shape, ref and operation reads over 27 keys, including one that is never set) gives the same values before and after. aws-sdk-core (2,496 examples) and aws-sdk-s3 (1,721) specs pass, and the new spec covers reading metadata that was never set. Metadata is written while the API loads; the only later writer is the rest-xml response stubbing, which always writes the same key and value, so a concurrent first write can't lose anything.

Operation and Api keep their eager Hash: there are only a few of them, so changing them would grow the diff for no measurable gain.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

AI-assisted; prompted, reviewed and verified by me.

@czj
czj requested review from a team as code owners November 6, 2026 08:56
@czj
czj requested review from jterapin and siddsriv November 6, 2026 08:56
@czj
czj force-pushed the perf/lazy-shape-metadata branch from 1fb151f to 6ff8394 Compare November 11, 2026 08:57
@czj

czj commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

I rebased this onto version-3 to fix a CHANGELOG conflict, the code is unchanged. Measured again on the current base: 4,252 → 419 empty Hashes and 8.41 → 7.83 MB after loading S3 and KMS, the same gain as in the description.

@czj

czj commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

Re-reading this I found that shape metadata isn't only written while the API loads: the rest-xml response stubbing also writes it at request time. It always writes the same key and value, so a concurrent first write on the lazy Hash can't lose anything; I added a sentence about it to the description.

@jnunn-aws jnunn-aws left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reproduced on Ruby 4.0.9. Loading S3 and KMS leaves 3,833 fewer empty Hashes and 0.61 MB less memsize_of_all (about 0.15 MB on Ruby 3.2, where an empty Hash is 40 bytes, not 160). S3 and KMS metadata reads are unchanged, and 32 threads making the first stubbed get_bucket_policy_status call get the same result.

@czj
czj force-pushed the perf/lazy-shape-metadata branch from 6ff8394 to 09b0ed5 Compare November 12, 2026 10:20
@czj

czj commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @jnunn-aws for checking it down to Ruby 3.2 and the threaded first call! The CHANGELOG conflicted again once 3.268.0 shipped, so I rebased onto version-3, the code is unchanged.

@jterapin
jterapin removed the request for review from siddsriv November 12, 2026 16:34
Every Shape and ShapeRef created an empty metadata Hash, though most never get any metadata. Loading the S3 and KMS APIs left 3,833 of them, about 0.6 MB of Ruby objects. Create the Hash on the first []= instead; [] reads nil as no metadata.

Assisted-by: Claude Opus 5.5 [Claude Code]
@jterapin
jterapin force-pushed the perf/lazy-shape-metadata branch from 09b0ed5 to ad7d7dd Compare November 16, 2026 18:46
@jterapin
jterapin merged commit bce0ba9 into aws:version-3 Nov 16, 2026
29 checks passed
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.

4 participants