perf(roi): skip writes for unchanged cached identities

This commit is contained in:
moe-berri 2026-09-30 13:12:49 -07:00
parent deb9d2d5e5
commit 5d642e6211
2 changed files with 15 additions and 2 deletions

View file

@ -313,9 +313,16 @@ def _processed_records(processed: tuple[_ProcessedPull, ...]) -> Mapping[int, RO
return MappingProxyType({item.position: item.record for item in processed})
async def _cache_estimated_pull(repository: _ReportRepository, key: str | None, record: ROIPullRecord) -> None:
async def _cache_estimated_pull(
repository: _ReportRepository, key: str | None, record: ROIPullRecord, previous: ROIPullRecord | None = None
) -> None:
if key is None or record["estimate"]["status"] != "estimated":
return
if previous is not None and (record.get("profile_email"), record["emails"]) == (
previous.get("profile_email"),
previous["emails"],
):
return
await repository.set_param(
"roi_calculator_pull_" + key,
_JSON_OBJECT_ADAPTER.validate_python(TypeAdapter(ROIPullRecord).dump_python(record, mode="json")),
@ -506,7 +513,9 @@ class SyncManager:
}
)
)
await _cache_estimated_pull(repository, key, cached_record)
await _cache_estimated_pull(
repository, key, cached_record, cached_pull if saved is not None else None
)
self._update_estimate_progress(cached_record["estimate"])
return _ProcessedPull(index, cached_record)
try:

View file

@ -75,12 +75,15 @@ class _Parameter:
class _ReportRepository:
def __init__(self) -> None:
self.values: Mapping[str, object] = MappingProxyType({})
self.pull_writes: int = 0
async def get_param(self, param_name: str) -> _Parameter | None:
value: Final = self.values.get(param_name)
return _Parameter(value) if value is not None else None
async def set_param(self, param_name: str, param_value: object) -> object:
if param_name.startswith("roi_calculator_pull_"):
self.pull_writes += 1
_assert_json_round_trip(param_value)
self.values = MappingProxyType({**self.values, param_name: param_value})
return self.values[param_name]
@ -589,6 +592,7 @@ async def test_reused_profile_preserves_email_only_when_lookup_fails(profile_sta
subsequent: Final = TypeAdapter(ROIReport).validate_python(repository.values["roi_calculator_report"])
assert subsequent["pulls"][0]["profile_email"] == expected
assert subsequent["pulls"][0]["emails"] == ((expected,) if expected else ())
assert repository.pull_writes == (2 if profile_status == 200 else 1)
@pytest.mark.asyncio