Skip to content

Commit

Permalink
Cache dimension records when serializing
Browse files Browse the repository at this point in the history
Make use of the serialization caches when calling `to_simple` on
dimension records.
  • Loading branch information
natelust authored and TallJimbo committed Aug 21, 2023
1 parent 3e27943 commit 4d5a0bb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 8 additions & 1 deletion python/lsst/daf/butler/core/dimensions/_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 348 in python/lsst/daf/butler/core/dimensions/_records.py

View check run for this annotation

Codecov / codecov/patch

python/lsst/daf/butler/core/dimensions/_records.py#L348

Added line #L348 was not covered by tests

mapping = {name: getattr(self, name) for name in self.__slots__}
# If the item in mapping supports simplification update it
Expand All @@ -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

Check warning on line 369 in python/lsst/daf/butler/core/dimensions/_records.py

View check run for this annotation

Codecov / codecov/patch

python/lsst/daf/butler/core/dimensions/_records.py#L369

Added line #L369 was not covered by tests
return dimRec

@classmethod
def from_simple(
Expand Down
2 changes: 1 addition & 1 deletion python/lsst/daf/butler/core/persistenceContext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand Down

0 comments on commit 4d5a0bb

Please sign in to comment.