Skip to content

Commit abd8a26

Browse files
committed
perf: cache compliance calculations to avoid redundant computation
Store compliance results in a cache dictionary during the first pass through token standards, then reuse cached values when checking for partial implementations. This reduces compliance calculations from 10 to 5 per contract (50% reduction).
1 parent 2a51ebf commit abd8a26

File tree

1 file changed

+7
-2
lines changed
  • slither/printers/summary

1 file changed

+7
-2
lines changed

slither/printers/summary/erc.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,13 @@ def _analyze_contract(self, contract: Contract) -> dict | None:
106106
"details": {},
107107
}
108108

109+
# Cache compliance results to avoid redundant calculations
110+
compliance_cache: dict[str, tuple[float, list[str], list[str]]] = {}
111+
109112
# Check each token standard - compute compliance once per standard
110113
for display_name, erc_key in TOKEN_STANDARDS.items():
111114
compliance, implemented, missing = self._get_compliance(contract, erc_key)
115+
compliance_cache[display_name] = (compliance, implemented, missing)
112116

113117
# Only consider fully compliant (100%) as a detected token
114118
if compliance == 100:
@@ -123,10 +127,11 @@ def _analyze_contract(self, contract: Contract) -> dict | None:
123127
}
124128

125129
# Check for partial implementations (>= threshold but not fully compliant)
130+
# using cached compliance values
126131
if not results["types"]:
127132
partial_types = []
128-
for display_name, erc_key in TOKEN_STANDARDS.items():
129-
compliance, _, _ = self._get_compliance(contract, erc_key)
133+
for display_name in TOKEN_STANDARDS:
134+
compliance, _, _ = compliance_cache[display_name]
130135
if compliance >= PARTIAL_THRESHOLD:
131136
partial_types.append(f"{display_name} ({compliance:.0f}%)")
132137

0 commit comments

Comments
 (0)