matrix-org / synapse

Synapse: Matrix homeserver written in Python/Twisted.
https://matrix-org.github.io/synapse
Apache License 2.0
11.83k stars 2.13k forks source link

Add __slots__ to replication commands. #16429

Closed clokep closed 1 year ago

clokep commented 1 year ago

I'm hoping this will slim down our replication needs very very slightly, but it is also minimal effort.

WIth this change we have the following, sizes are done using _get_size_of.

Class Original size (bytes) Final size (bytes) Savings (bytes)
Any _SimpleCommand subclass 408 360 48
RdataCommand 592 384 208
PositionCommand 672 448 224
UserSyncCommand 728 456 272
ClearUserSyncsCommand 416 360 56
FederationAckCommand 504 400 104
UserIpCommand 752 432 320
LockReleasedCommand 544 376 168
Code to produce the above ```python from synapse.util.caches.lrucache import _get_size_of from synapse.replication.tcp.commands import * def do(inst): print(f"{inst.__class__.__name__}: {_get_size_of(inst)}") do(ServerCommand("")) do(RdataCommand("", "", None, ())) do(PositionCommand("", "", 0, 1)) do(UserSyncCommand("", "", None, True, 1)) do(ClearUserSyncsCommand("")) do(FederationAckCommand("", 1)) do(UserIpCommand("", "", 1, "", None, 1)) do(LockReleasedCommand("", "", "")) ```
clokep commented 1 year ago

(Aside: I was slightly surprised these classes aren't defined with attrs or stdlib dataclasses, which would make it easier to turn on __slots__.)

Due to the NAME class var I was having trouble getting that to work and decided it wasn't worth my effort figuring out how to make those compatible.

clokep commented 1 year ago

Seems reasonable. I guess this only helps a lot if we have a huge queue of replication commands to work through?

It should help just in general with allocating less memory.