Skip to content

Avoid repeated casts when stored models have unknown attributes - #240

Draft
imacomber wants to merge 2 commits into
DmitryTsepelev:masterfrom
imacomber:fix/unknown-attribute-cast-performance
Draft

imacomber wants to merge 2 commits into
DmitryTsepelev:masterfrom
imacomber:fix/unknown-attribute-cast-performance

Conversation

@imacomber

@imacomber imacomber commented Oct 7, 2026 •

Copy link
Copy Markdown

Fixes #239
Related: #202

What

When a stored model has undeclared keys, StoreModel currently removes one key and casts the entire payload again for each UnknownAttributeError. That rebuilds nested models on every retry.

After the first missing-writer error, this change removes that key as before and retries construction once with a plain Hash that records later unknown keys during attribute assignment. It keeps direct Model.new strict and preserves unknown values through serialization.

Why

When dealing with large models containing nested objects, the raise and re-cast can start to take a long time.

A payload with many undeclared keys currently rebuilds the model and its nested attributes once per key. Recover later missing writers during one retry so casting stays bounded while preserving unknown-key round trips and strict direct construction.
@imacomber
imacomber marked this pull request as draft October 8, 2026 12:24
The recovery path has to preserve several ActiveModel and custom initializer behaviors. Make its Hash propagation and missing-writer checks easier to follow without changing the cast result. Spell out the optional nil value in the duplicate-key fallback.
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.

Avoid repeated casts when a payload has unknown attributes

1 participant