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

Commit

Permalink
Add __slots__ to replication commands. (#16429)
Browse files Browse the repository at this point in the history
To slightly reduce the amount of memory each command takes.
  • Loading branch information
clokep authored Oct 5, 2023
1 parent 009b47b commit 4e302b3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.d/16429.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Reduce the size of each replication command instance.
27 changes: 26 additions & 1 deletion synapse/replication/tcp/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"""
import abc
import logging
from typing import Optional, Tuple, Type, TypeVar
from typing import List, Optional, Tuple, Type, TypeVar

from synapse.replication.tcp.streams._base import StreamRow
from synapse.util import json_decoder, json_encoder
Expand Down Expand Up @@ -74,6 +74,8 @@ def redis_channel_name(self, prefix: str) -> str:
class _SimpleCommand(Command):
"""An implementation of Command whose argument is just a 'data' string."""

__slots__ = ["data"]

def __init__(self, data: str):
self.data = data

Expand Down Expand Up @@ -122,6 +124,8 @@ class RdataCommand(Command):
RDATA presence master 59 ["@baz:example.com", "online", ...]
"""

__slots__ = ["stream_name", "instance_name", "token", "row"]

NAME = "RDATA"

def __init__(
Expand Down Expand Up @@ -179,6 +183,8 @@ class PositionCommand(Command):
of the stream.
"""

__slots__ = ["stream_name", "instance_name", "prev_token", "new_token"]

NAME = "POSITION"

def __init__(
Expand Down Expand Up @@ -235,6 +241,8 @@ class ReplicateCommand(Command):
REPLICATE
"""

__slots__: List[str] = []

NAME = "REPLICATE"

def __init__(self) -> None:
Expand Down Expand Up @@ -264,6 +272,8 @@ class UserSyncCommand(Command):
Where <state> is either "start" or "end"
"""

__slots__ = ["instance_id", "user_id", "device_id", "is_syncing", "last_sync_ms"]

NAME = "USER_SYNC"

def __init__(
Expand Down Expand Up @@ -316,6 +326,8 @@ class ClearUserSyncsCommand(Command):
CLEAR_USER_SYNC <instance_id>
"""

__slots__ = ["instance_id"]

NAME = "CLEAR_USER_SYNC"

def __init__(self, instance_id: str):
Expand Down Expand Up @@ -343,6 +355,8 @@ class FederationAckCommand(Command):
FEDERATION_ACK <instance_name> <token>
"""

__slots__ = ["instance_name", "token"]

NAME = "FEDERATION_ACK"

def __init__(self, instance_name: str, token: int):
Expand All @@ -368,6 +382,15 @@ class UserIpCommand(Command):
USER_IP <user_id>, <access_token>, <ip>, <device_id>, <last_seen>, <user_agent>
"""

__slots__ = [
"user_id",
"access_token",
"ip",
"user_agent",
"device_id",
"last_seen",
]

NAME = "USER_IP"

def __init__(
Expand Down Expand Up @@ -441,6 +464,8 @@ class LockReleasedCommand(Command):
LOCK_RELEASED ["<instance_name>", "<lock_name>", "<lock_key>"]
"""

__slots__ = ["instance_name", "lock_name", "lock_key"]

NAME = "LOCK_RELEASED"

def __init__(
Expand Down

0 comments on commit 4e302b3

Please sign in to comment.