Skip to content

Lazy load model data classes - #851

Merged
kuhe merged 4 commits into
developfrom
kuhe/chore/models
Oct 19, 2026
Merged

kuhe merged 4 commits into
developfrom
kuhe/chore/models

Conversation

@kuhe

@kuhe kuhe commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

@dataclass annotations in the models.py file take up too much CPU time on initialization. As things stand, a 128mb lambda would OOM just by loading a handful of AWS SDK clients, if they are any of the somewhat large ones (not necessarily even to EC2's level).

To avoid this, the PR:

  • makes models.pyi declare the classes as type interfaces only
  • has models.py generate the classes on-demand

Cold-start benchmark

AWS Lambda w/ 512 MB, Python 3.12

Imports (does not instantiate) 5 generated clients at module scope: bedrock-agentcore-control, bedrock, guardduty, bedrock-agent, lambda.

Init Duration, median of 3 cold starts.

models source-only hash-pinned .pyc
eager (develop) ~3.9s ~2.3s
lazy (this PR) ~1.6s ~0.8s

@kuhe
kuhe force-pushed the kuhe/chore/models branch 6 times, most recently from e403e39 to 1578269 Compare October 15, 2026 17:15
@kuhe

kuhe commented Oct 15, 2026

Copy link
Copy Markdown
Contributor Author

4% serde slowdown after cold start, but we'll reclaim that performance in another PR.

The SDK has to start up without going OOM first.

@kuhe
kuhe marked this pull request as ready for review October 15, 2026 18:50
@kuhe
kuhe requested a review from a team as a code owner October 15, 2026 18:50
@kuhe
kuhe force-pushed the kuhe/chore/models branch 3 times, most recently from c68bef4 to 987e77b Compare October 15, 2026 21:14
if scalar is not None:
return scalar
if st == ShapeType.STRUCTURE:
target = member.expect_member_target().id.name

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we lose the namespace here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

smithy model validation currently guarantees service closure uniqueness in the short names being used here.

I want to make that fix in a different PR if/when needed.

Comment thread packages/smithy-core/src/smithy_core/_dynamic_models.py Outdated
Comment thread packages/smithy-core/src/smithy_core/_dynamic_models.py
Comment thread packages/smithy-core/src/smithy_core/_dynamic_models.py Outdated
Comment thread packages/smithy-core/src/smithy_core/_dynamic_models.py
@kuhe
kuhe force-pushed the kuhe/chore/models branch 4 times, most recently from aaf2366 to 6c16a24 Compare October 17, 2026 18:30
Comment thread packages/smithy-core/src/smithy_core/_dynamic_models.py Outdated
Comment thread packages/smithy-core/src/smithy_core/_dynamic_models.py
Comment thread packages/smithy-core/src/smithy_core/_dynamic_models.py Outdated
kuhe added a commit that referenced this pull request Oct 19, 2026
…rename detection

Addresses review comments on PR #851:
- Deserialize enum members through the built enum class instead of returning the raw str/int.
- Set __module__ on union parent, variants, and Unknown so they report the client models module.
- Guard build() with an RLock so concurrent builders of one shape cannot publish two class objects.
- Read the enum class name from ENUM_SYMBOL in ModelModuleGenerator so renamed enums index correctly and no bogus str/int rename entries are emitted.
kuhe added 4 commits October 19, 2026 16:52
…rename detection

Addresses review comments on PR #851:
- Deserialize enum members through the built enum class instead of returning the raw str/int.
- Set __module__ on union parent, variants, and Unknown so they report the client models module.
- Guard build() with an RLock so concurrent builders of one shape cannot publish two class objects.
- Read the enum class name from ENUM_SYMBOL in ModelModuleGenerator so renamed enums index correctly and no bogus str/int rename entries are emitted.
Constructing the APIOperation table builds no model classes and hydrates no schemas; using one operation builds only that operation's input, not sibling operations' shapes or every schema in the module.
@kuhe
kuhe force-pushed the kuhe/chore/models branch from e28e988 to ab3271d Compare October 19, 2026 16:52
@kuhe
kuhe merged commit 25d2900 into develop Oct 19, 2026
11 checks passed
@kuhe
kuhe deleted the kuhe/chore/models branch October 19, 2026 18:19
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