|
4 | 4 | import os |
5 | 5 | import sys |
6 | 6 | import termios |
| 7 | +import time |
7 | 8 | from collections import defaultdict |
8 | 9 | from collections.abc import Generator, Sequence |
9 | 10 | from contextlib import contextmanager, suppress |
@@ -96,27 +97,56 @@ def checked_terminfo_dir() -> str | None: |
96 | 97 | return terminfo_dir if os.path.isdir(terminfo_dir) else None |
97 | 98 |
|
98 | 99 |
|
| 100 | +_pgmap_cache: DefaultDict[int, list[int]] | None = None |
| 101 | +_pgmap_cache_at: float = 0.0 |
| 102 | +_pgmap_ttl: float = 0.0 |
| 103 | + |
| 104 | + |
| 105 | +def _refresh_pgmap_cache() -> DefaultDict[int, list[int]]: |
| 106 | + global _pgmap_cache, _pgmap_cache_at |
| 107 | + try: |
| 108 | + _pgmap_cache = process_group_map() |
| 109 | + except Exception: |
| 110 | + _pgmap_cache = defaultdict(list) |
| 111 | + _pgmap_cache_at = time.monotonic() |
| 112 | + return _pgmap_cache |
| 113 | + |
| 114 | + |
99 | 115 | def processes_in_group(grp: int) -> list[int]: |
100 | | - gmap: DefaultDict[int, list[int]] | None = getattr(process_group_map, 'cached_map', None) |
101 | | - if gmap is None: |
102 | | - try: |
103 | | - gmap = process_group_map() |
104 | | - except Exception: |
105 | | - gmap = defaultdict(list) |
| 116 | + if _pgmap_cache is not None: |
| 117 | + if _pgmap_ttl <= 0 or (time.monotonic() - _pgmap_cache_at) < _pgmap_ttl: |
| 118 | + return _pgmap_cache.get(grp, []) |
| 119 | + try: |
| 120 | + gmap = process_group_map() |
| 121 | + except Exception: |
| 122 | + gmap = defaultdict(list) |
106 | 123 | return gmap.get(grp, []) |
107 | 124 |
|
108 | 125 |
|
109 | 126 | @contextmanager |
110 | | -def cached_process_data() -> Generator[None, None, None]: |
111 | | - try: |
112 | | - cm = process_group_map() |
113 | | - except Exception: |
114 | | - cm = defaultdict(list) |
115 | | - setattr(process_group_map, 'cached_map', cm) |
| 127 | +def cached_process_data(ttl: float = 0.0) -> Generator[None, None, None]: |
| 128 | + """Cache process_group_map() results within this context. |
| 129 | +
|
| 130 | + With ttl=0 (default), a fresh snapshot is taken on entry and cleared |
| 131 | + on exit. Suitable for short-lived operations like listing windows. |
| 132 | + With ttl>0, the snapshot is reused across calls if still within the |
| 133 | + TTL window. Suitable for high-frequency callers like tab bar rendering. |
| 134 | + """ |
| 135 | + global _pgmap_cache, _pgmap_cache_at, _pgmap_ttl |
| 136 | + prev_cache, prev_at, prev_ttl = _pgmap_cache, _pgmap_cache_at, _pgmap_ttl |
| 137 | + _pgmap_ttl = ttl |
| 138 | + if ttl > 0 and _pgmap_cache is not None and (time.monotonic() - _pgmap_cache_at) < ttl: |
| 139 | + pass # reuse existing cache |
| 140 | + else: |
| 141 | + _refresh_pgmap_cache() |
116 | 142 | try: |
117 | 143 | yield |
118 | 144 | finally: |
119 | | - delattr(process_group_map, 'cached_map') |
| 145 | + if ttl <= 0: |
| 146 | + # no TTL, restore previous state so cache doesn't leak |
| 147 | + _pgmap_cache, _pgmap_cache_at, _pgmap_ttl = prev_cache, prev_at, prev_ttl |
| 148 | + else: |
| 149 | + _pgmap_ttl = prev_ttl |
120 | 150 |
|
121 | 151 |
|
122 | 152 | def session_id(pids: Iterable[int]) -> int: |
|
0 commit comments