Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit bae9665

Browse files
committed
Use a set for WheelTimer to better handle duplicates.
1 parent 4ffe7a9 commit bae9665

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

synapse/util/wheel_timer.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,20 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
from typing import Generic, List, TypeVar
14+
from typing import Generic, Hashable, List, Set, TypeVar
1515

16-
T = TypeVar("T")
16+
T = TypeVar("T", bound=Hashable)
1717

1818

1919
class _Entry(Generic[T]):
2020
__slots__ = ["end_key", "queue"]
2121

2222
def __init__(self, end_key: int) -> None:
2323
self.end_key: int = end_key
24-
self.queue: List[T] = []
24+
25+
# We use a set here as otherwise we can end up with a lot of duplicate
26+
# entries.
27+
self.queue: Set[T] = set()
2528

2629

2730
class WheelTimer(Generic[T]):
@@ -55,7 +58,7 @@ def insert(self, now: int, obj: T, then: int) -> None:
5558

5659
if then_key <= max_key:
5760
# The max here is to protect against inserts for times in the past
58-
self.entries[max(min_key, then_key) - min_key].queue.append(obj)
61+
self.entries[max(min_key, then_key) - min_key].queue.add(obj)
5962
return
6063

6164
next_key = int(now / self.bucket_size) + 1
@@ -71,7 +74,7 @@ def insert(self, now: int, obj: T, then: int) -> None:
7174
# to insert. This ensures there are no gaps.
7275
self.entries.extend(_Entry(key) for key in range(last_key, then_key + 1))
7376

74-
self.entries[-1].queue.append(obj)
77+
self.entries[-1].queue.add(obj)
7578

7679
def fetch(self, now: int) -> List[T]:
7780
"""Fetch any objects that have timed out
@@ -84,7 +87,7 @@ def fetch(self, now: int) -> List[T]:
8487
"""
8588
now_key = int(now / self.bucket_size)
8689

87-
ret = []
90+
ret: List[T] = []
8891
while self.entries and self.entries[0].end_key <= now_key:
8992
ret.extend(self.entries.pop(0).queue)
9093

0 commit comments

Comments
 (0)