Skip to content

Commit bf037aa

Browse files
wmaynerclaude
andcommitted
Stop memoizing num_subsets_larger_than_one_element
The function evaluates `2**n - n - 1` in about 109 ns, against roughly 250 ns for the cache lookup wrapping it, so the memoization cost more than twice what it saved. It also has no callers inside the library. Removing the decorator drops `cache_info()` / `cache_clear()` from the function. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Qi3MYsc3afQMxoWZZo2tEL
1 parent 018805e commit bf037aa

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
`pyphi.combinatorics.num_subsets_larger_than_one_element` is no longer memoized,
2+
and so no longer carries `cache_info()` / `cache_clear()`. It evaluates
3+
`2**n - n - 1` in about 109 ns, against roughly 250 ns for the cache lookup that
4+
was wrapping it, so caching it cost more than it saved.

pyphi/combinatorics.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
import numpy as np
1515

16-
from .cache import cache
17-
1816
# TODO: move relevant functions from utils here
1917

2018

@@ -91,7 +89,6 @@ def _extend(
9189
yield from _extend(i + 1, [i], sets[i])
9290

9391

94-
@cache(cache={}, maxmem=None)
9592
def num_subsets_larger_than_one_element(n: int) -> int:
9693
"""Return the number of subsets on N elements with size >1.
9794
@@ -100,6 +97,12 @@ def num_subsets_larger_than_one_element(n: int) -> int:
10097
|X| = |P(n)| - |{S ∈ P(n) | |S| = 1}| - |{S ∈ P(n) | |S| = 0}|
10198
= 2^n - (n choose 1) - |{ø}|
10299
= 2^n - n - 1
100+
101+
Notes
102+
-----
103+
Deliberately not memoized: the expression evaluates in about 66 ns, well
104+
under the roughly 250 ns a cache lookup costs, so caching it was a net
105+
loss of about a factor of four.
103106
"""
104107
return 2**n - n - 1 # type: ignore[no-any-return]
105108

0 commit comments

Comments
 (0)