From 791b97f0f451eddced9285bac24e019e8c83cb1b Mon Sep 17 00:00:00 2001 From: deucalioncodes Date: Wed, 18 Mar 2026 07:43:03 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20skip=20redundant=20save=20on=20Entity.lo?= =?UTF-8?q?ad()=20=E2=80=94=20eliminates=20unnecessary=20json.dumps=20+=20?= =?UTF-8?q?storage=20write?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- ic_python_db/entity.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ic_python_db/entity.py b/ic_python_db/entity.py index 9633970..893897d 100644 --- a/ic_python_db/entity.py +++ b/ic_python_db/entity.py @@ -180,7 +180,8 @@ class User(Entity): setattr(self, k, v) self._do_not_save = False - self._save() + if not self._loaded: + self._save() @classmethod def new(cls, **kwargs): @@ -386,6 +387,10 @@ def load( # Create instance first entity = cls(**data, _loaded=True) + # If migration was applied, persist the migrated data + if stored_version != current_version: + entity._save() + # Restore legacy "relations" block if present in serialized data. # Otherwise keep the _relations that __init__ already populated # via relationship descriptors (OneToMany, ManyToOne, etc.).