Skip to content

Commit

Permalink
Cache some properties immutable properties (#1423)
Browse files Browse the repository at this point in the history
This PR caches some properties of immutable classes to reduce
computation overhead.
  • Loading branch information
plypaul authored Sep 24, 2024
1 parent 5c41956 commit 2c74085
Showing 1 changed file with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import time
from collections import defaultdict
from dataclasses import dataclass, field
from functools import cached_property
from typing import Dict, FrozenSet, List, Sequence, Set, Tuple

from dbt_semantic_interfaces.enum_extension import assert_values_exhausted
Expand Down Expand Up @@ -282,7 +283,7 @@ def filter(
path_key_to_linkable_metrics=key_to_linkable_metrics,
)

@property
@cached_property
def only_unique_path_keys(self) -> LinkableElementSet:
"""Returns a set that only includes path keys that map to a single distinct element."""
return LinkableElementSet(
Expand All @@ -303,7 +304,7 @@ def only_unique_path_keys(self) -> LinkableElementSet:
},
)

@property
@cached_property
@override
def derived_from_semantic_models(self) -> Sequence[SemanticModelReference]:
semantic_model_references: Set[SemanticModelReference] = set()
Expand All @@ -319,7 +320,7 @@ def derived_from_semantic_models(self) -> Sequence[SemanticModelReference]:

return sorted(semantic_model_references, key=lambda reference: reference.semantic_model_name)

@property
@cached_property
def spec_count(self) -> int:
"""If this is mapped to spec objects, the number of specs that would be produced."""
return (
Expand All @@ -328,7 +329,7 @@ def spec_count(self) -> int:
+ len(self.path_key_to_linkable_metrics.keys())
)

@property
@cached_property
def specs(self) -> Sequence[LinkableInstanceSpec]:
"""Converts the items in a `LinkableElementSet` to their corresponding spec objects."""
specs: List[LinkableInstanceSpec] = []
Expand Down

0 comments on commit 2c74085

Please sign in to comment.