Skip to content

Commit

Permalink
/* PR_START p--short-term-perf 10 */ Cache joined elements.
Browse files Browse the repository at this point in the history
  • Loading branch information
plypaul committed Sep 30, 2024
1 parent 20cfcf7 commit 83f999d
Showing 1 changed file with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ def __init__(

# Map measures / entities to semantic models that contain them.
self._entity_to_semantic_model: Dict[str, List[SemanticModel]] = defaultdict(list)
self._measure_to_semantic_model: Dict[str, List[SemanticModel]] = defaultdict(list)

self._semantic_model_reference_to_semantic_model: Dict[SemanticModelReference, SemanticModel] = {}
for semantic_model in self._semantic_models:
for entity in semantic_model.entities:
self._entity_to_semantic_model[entity.reference.element_name].append(semantic_model)
self._semantic_model_reference_to_semantic_model[semantic_model.reference] = semantic_model

self._metric_to_linkable_element_sets: Dict[str, List[LinkableElementSet]] = {}
self._metric_references_to_metrics: Dict[MetricReference, Metric] = {}
Expand Down Expand Up @@ -188,6 +188,9 @@ def __init__(

logger.debug(LazyFormat(lambda: f"Building valid group-by-item indexes took: {time.time() - start_time:.2f}s"))

# Cache for `_get_joined_elements()`.
self._semantic_model_reference_to_joined_elements: Dict[SemanticModelReference, LinkableElementSet] = {}

def _generate_linkable_time_dimensions(
self,
semantic_model_origin: SemanticModelReference,
Expand Down Expand Up @@ -526,8 +529,20 @@ def _get_metric_time_elements(self, measure_reference: Optional[MeasureReference
}
)

def _get_joined_elements(self, measure_semantic_model: SemanticModel) -> LinkableElementSet:
def _get_joined_elements(self, measure_semantic_model_reference: SemanticModelReference) -> LinkableElementSet:
"""Get the elements that can be generated by joining other models to the given model."""
result = self._semantic_model_reference_to_joined_elements.get(measure_semantic_model_reference)
if result is not None:
return result

result = self._get_joined_elements_without_cache(measure_semantic_model_reference)
self._semantic_model_reference_to_joined_elements[measure_semantic_model_reference] = result
return result

def _get_joined_elements_without_cache(
self, measure_semantic_model_reference: SemanticModelReference
) -> LinkableElementSet:
measure_semantic_model = self._semantic_model_reference_to_semantic_model[measure_semantic_model_reference]
# Create single-hop elements
join_paths = []
for entity in measure_semantic_model.entities:
Expand Down Expand Up @@ -596,7 +611,7 @@ def _get_linkable_element_set_for_measure(
metrics_linked_to_semantic_model = LinkableElementSet()

metric_time_elements = self._get_metric_time_elements(measure_reference)
joined_elements = self._get_joined_elements(measure_semantic_model)
joined_elements = self._get_joined_elements(measure_semantic_model.reference)

return LinkableElementSet.merge_by_path_key(
(
Expand Down

0 comments on commit 83f999d

Please sign in to comment.