diff --git a/python/lsst/daf/butler/core/dimensions/_records.py b/python/lsst/daf/butler/core/dimensions/_records.py index 6de6a99954..22e9cebaf6 100644 --- a/python/lsst/daf/butler/core/dimensions/_records.py +++ b/python/lsst/daf/butler/core/dimensions/_records.py @@ -342,6 +342,10 @@ def to_simple(self, minimal: bool = False) -> SerializedDimensionRecord: # query. This may not be overly useful since to reconstruct # a collection of records will require repeated registry queries. # For now do not implement minimal form. + key = (id(self.definition), self.dataId) + cache = PersistenceContextVars.serializedDimensionRecordMapping.get() + if cache is not None and (result := cache.get(key)) is not None: + return result mapping = {name: getattr(self, name) for name in self.__slots__} # If the item in mapping supports simplification update it @@ -360,7 +364,10 @@ def to_simple(self, minimal: bool = False) -> SerializedDimensionRecord: # hash objects, encode it here to a hex string mapping[k] = v.hex() definition = self.definition.to_simple(minimal=minimal) - return SerializedDimensionRecord(definition=definition, record=mapping) + dimRec = SerializedDimensionRecord(definition=definition, record=mapping) + if cache is not None: + cache[key] = dimRec + return dimRec @classmethod def from_simple( diff --git a/python/lsst/daf/butler/core/persistenceContext.py b/python/lsst/daf/butler/core/persistenceContext.py index 7542754704..bc39b899b4 100644 --- a/python/lsst/daf/butler/core/persistenceContext.py +++ b/python/lsst/daf/butler/core/persistenceContext.py @@ -94,7 +94,7 @@ class PersistenceContextVars: """ serializedDimensionRecordMapping: ContextVar[ - dict[tuple[str, frozenset], SerializedDimensionRecord] | None + dict[tuple[str, frozenset] | tuple[int, DataCoordinate], SerializedDimensionRecord] | None ] = ContextVar("serializedDimensionRecordMapping", default=None) r"""A cache of `SerializedDimensionRecord`\ s. """