Commit b64796b
Use plain sets for watcher dep bookkeeping
Watcher._deps and Watcher._new_deps were WeakSets. WeakSet is
implemented in pure Python, so the two membership checks and the add
in Watcher.add_dep -- which run for every dep touched on every watcher
evaluation -- each cost a Python function call, as does iterating in
cleanup_deps. Profiling showed this bookkeeping at 32-40% of runtime
in watcher-heavy workloads. Plain sets do the same operations in C.
Strong references to deps are safe here:
- Dep._subs remains a WeakSet, so there are no reference cycles and
watcher lifecycle is unchanged: deleting a watcher still
unsubscribes it from its deps.
- A dep whose container has been garbage collected is inert (nothing
can notify it anymore) and is released on the watcher's next
re-evaluation (cleanup_deps), on deactivation, or when the watcher
itself is collected. Until then it keeps only a small Dep object
alive, not the container.
Benchmarks (local, macOS, vs master):
- deep_watch_mutate_leaf: 1.33-1.49x faster
- notify_watchers: 1.49-1.61x faster
- read_dict_getitem_tracked: 1.58x faster
- untracked reads and plain baselines: unchanged
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>1 parent 88841a4 commit b64796b
2 files changed
Lines changed: 40 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| |||
219 | 219 | | |
220 | 220 | | |
221 | 221 | | |
222 | | - | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
223 | 229 | | |
224 | 230 | | |
225 | 231 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
2 | 4 | | |
3 | 5 | | |
4 | 6 | | |
| |||
53 | 55 | | |
54 | 56 | | |
55 | 57 | | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
0 commit comments