fix: skip redundant save on Entity.load()#3
Merged
deucalioncodes merged 1 commit intomainfrom Mar 18, 2026
Merged
Conversation
…on.dumps + storage write Every Entity.load() was triggering a full re-serialization and write-back via __init__ -> _save(), even though the data was just read from storage. This caused 3 JSON round-trips + 1 storage write for every read-only load: - 1x json.loads (read) - 1x serialize() + json.dumps (redundant write-back) - 1x storage.insert (redundant StableBTreeMap write on canister) - 1x audit json.dumps (if audit enabled) Fix: skip _save() in __init__ when _loaded=True. Explicitly save in Entity.load() only when migration was applied, so migrated data is still persisted. Closes #1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Every
Entity.load()was triggering a full re-serialization and write-back via__init__→_save(), even though the data was just read from storage.Before (per load)
json.loads(read)serialize()(Python dict construction, MRO walk)json.dumps(redundant write-back)storage.insert(redundant StableBTreeMap write on canister)json.dumps(if audit enabled)After (per load)
json.loads(read)Changes
__init__: Skip_save()when_loaded=True— the data is already in storage.Entity.load(): Explicitly call_save()after migration, so migrated data is still persisted.Test results
106 passed, 3 pre-existing failures (unrelated
TestDatabase.setUpissue when run via pytest).Closes #1