From 4b37c74a4048e9278286a916ce9e5ec7ba113011 Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Mon, 30 Sep 2024 14:01:06 -0700 Subject: [PATCH] Cache `SemanticModelLookup.get_agg_time_dimension_specs_for_measure()`. This is also a common method that is called during query resolution. The memory used by the cache is small since the number of measures is on the order of ~100 in large manifests. --- .../model/semantics/semantic_model_lookup.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/metricflow-semantics/metricflow_semantics/model/semantics/semantic_model_lookup.py b/metricflow-semantics/metricflow_semantics/model/semantics/semantic_model_lookup.py index 91ec266172..a3c3ef8388 100644 --- a/metricflow-semantics/metricflow_semantics/model/semantics/semantic_model_lookup.py +++ b/metricflow-semantics/metricflow_semantics/model/semantics/semantic_model_lookup.py @@ -67,6 +67,9 @@ def __init__(self, model: SemanticManifest, custom_granularities: Dict[str, Expa # Cache for defined time granularity. self._time_dimension_to_defined_time_granularity: Dict[TimeDimensionReference, TimeGranularity] = {} + # Cache for agg. time dimension for measure. + self._measure_reference_to_agg_time_dimension_specs: Dict[MeasureReference, Sequence[TimeDimensionSpec]] = {} + def get_dimension_references(self) -> Sequence[DimensionReference]: """Retrieve all dimension references from the collection of semantic models.""" return tuple(self._dimension_index.keys()) @@ -364,6 +367,17 @@ def get_agg_time_dimension_specs_for_measure( self, measure_reference: MeasureReference ) -> Sequence[TimeDimensionSpec]: """Get the agg time dimension specs that can be used in place of metric time for this measure.""" + result = self._measure_reference_to_agg_time_dimension_specs.get(measure_reference) + if result is not None: + return result + + result = self._get_agg_time_dimension_specs_for_measure(measure_reference) + self._measure_reference_to_agg_time_dimension_specs[measure_reference] = result + return result + + def _get_agg_time_dimension_specs_for_measure( + self, measure_reference: MeasureReference + ) -> Sequence[TimeDimensionSpec]: agg_time_dimension = self.get_agg_time_dimension_for_measure(measure_reference) # A measure's agg_time_dimension is required to be in the same semantic model as the measure, # so we can assume the same semantic model for both measure and dimension.