@@ -240,14 +240,7 @@ def transform_properties_dict_value_to_str(cls, properties: Properties) -> dict[
240240 def _lazy_id_to_snapshot_position (self ) -> dict [int , int ]:
241241 """Return an index of snapshot ID to its position in the snapshots list.
242242
243- Positions are cached rather than Snapshot instances, so that replacing an entry of the list
244- in place is still picked up by the next lookup.
245-
246- A plain `cached_property` cannot be used here: `model_copy` carries `__dict__` over to the
247- new instance, so a copy that replaces the snapshots would inherit a stale index. The index is
248- tied to the list it was built from, and rebuilt whenever `snapshots` is a different list.
249- Keeping a reference to that list also keeps it alive, so its identity cannot be reused by
250- another object.
243+ This is calculated once per snapshots list and cached.
251244 """
252245 cached = self .__dict__ .get ("_id_to_snapshot_position" )
253246 if cached is None or cached [0 ] is not self .snapshots :
@@ -265,8 +258,7 @@ def snapshot_by_id(self, snapshot_id: int) -> Snapshot | None:
265258 if (position := self ._lazy_id_to_snapshot_position .get (snapshot_id )) is not None and position < len (snapshots ):
266259 if (snapshot := snapshots [position ]).snapshot_id == snapshot_id :
267260 return snapshot
268- # Either the id is absent, or the list was mutated in place and the cached positions
269- # no longer line up. Fall back to a scan, which is always correct.
261+ # Absent id, or the list was mutated in place and the cached positions no longer line up.
270262 return next ((snapshot for snapshot in snapshots if snapshot .snapshot_id == snapshot_id ), None )
271263
272264 def schema_by_id (self , schema_id : int ) -> Schema | None :
0 commit comments