refactor: reduce nesting

This commit is contained in:
Mike A.
2026-01-06 00:36:35 +01:00
parent 2afba5ed51
commit c9897eaaba

View File

@@ -467,7 +467,9 @@ class _AccessoryKeyGenerator(KeyGenerator[KeyPair]):
# Use bisect to find the largest index < ind in O(log n)
pos = bisect.bisect_left(indices, ind)
if pos > 0:
if pos == 0: # No cached index less than ind
continue
cached_ind = indices[pos - 1]
if cached_ind > best_ind:
best_ind = cached_ind
@@ -478,23 +480,23 @@ class _AccessoryKeyGenerator(KeyGenerator[KeyPair]):
def _update_caches(self, ind: int, sk: bytes) -> None:
"""Update all applicable cache tiers with the computed key."""
for tier_idx, tier in enumerate(self._CACHE_TIERS):
if ind % tier.interval == 0:
if ind % tier.interval != 0:
continue
cache = self._sk_caches[tier_idx]
indices = self._cache_indices[tier_idx]
# Add to cache if not already present
if ind not in cache:
if ind in cache:
continue
cache[ind] = sk
bisect.insort(indices, ind)
# Evict if cache exceeds size limit
if tier.max_size is not None and len(cache) > tier.max_size:
# If adding a historical key, evict smallest (oldest)
# If adding a future key, evict largest (newest old key)
if indices and ind > indices[0]:
evict_ind = indices.pop(0)
else:
evict_ind = indices.pop(-1)
# If adding a historical key, evict smallest index
# If adding a future key, evict largest
evict_ind = indices.pop(0 if indices and ind > indices[0] else -1)
del cache[evict_ind]