Conversation
small gateway messages will usually fit into a single websocket frame, making the `extend buffer + decompress + clear` steps pretty unnecessary
Enegg
reviewed
Jun 28, 2026
Comment on lines
+129
to
+135
| def __post_init__(self) -> None: | ||
| if self.encoding != "json": | ||
| msg = "Gateway encodings other than `json` are currently not supported." | ||
| raise ValueError(msg) | ||
| if self.compress not in ("zlib-stream", "zstd-stream", None): | ||
| msg = "Gateway transport compression modes other than `zlib-stream`, `zstd-stream`, or None are currently not supported." | ||
| raise ValueError(msg) |
Contributor
There was a problem hiding this comment.
What do you think of placing such checks behind if __debug__?
Suggested change
| def __post_init__(self) -> None: | |
| if self.encoding != "json": | |
| msg = "Gateway encodings other than `json` are currently not supported." | |
| raise ValueError(msg) | |
| if self.compress not in ("zlib-stream", "zstd-stream", None): | |
| msg = "Gateway transport compression modes other than `zlib-stream`, `zstd-stream`, or None are currently not supported." | |
| raise ValueError(msg) | |
| if __debug__: | |
| def __post_init__(self) -> None: | |
| if self.encoding != "json": | |
| msg = "Gateway encodings other than `json` are currently not supported." | |
| raise ValueError(msg) | |
| if self.compress not in ("zlib-stream", "zstd-stream", None): | |
| msg = "Gateway transport compression modes other than `zlib-stream`, `zstd-stream`, or None are currently not supported." | |
| raise ValueError(msg) |
Member
Author
There was a problem hiding this comment.
Hmm, not sure. Since this is going to be run at most once per gateway connection, I don't really see a reason to.
Enegg
reviewed
Jun 30, 2026
Enegg
left a comment
Contributor
There was a problem hiding this comment.
Half of the review before sleep, half after 👍
|
|
||
|
|
||
| class _DecompressionContext(Protocol): | ||
| def decompress(self, data: bytes | bytearray, /) -> bytes | None: ... |
Contributor
There was a problem hiding this comment.
If ctx.decompress support accepting typing.Buffer, we could replace bytes | bytearray with it
Member
Author
There was a problem hiding this comment.
I considered this as well, but Buffer doesn't trivially work with .endswith or bytearray.extend, unfortunately
Contributor
There was a problem hiding this comment.
Now I've checked that it's a 3.12+ import, oh well
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds support for
zstd-streamtransport compression in gateway connections, in addition to the usualzlib-stream, and uses it as the default compression method if available.This uses the builtin
compression.zstdmodule on Python 3.14+, or optionallybackports.zstdon earlier Python versions, which is now also part ofdisnake[speed](would've been included byaiohttp[speedups]already anyway),.At least based on some rudimentary benchmarking and publicly available comparisons, zstd should achieve a little better compression and is a fair bit faster.
Running this branch with two instances of one of my bots in prod at the same time for ~5 minutes, one with each compression mode:
The one minor disadvantage is potentially slightly higher memory usage due to larger lookback buffers, but only to a very limited and negligible degree, especially since it scales linearly with the number of shards.
https://docs.discord.com/developers/events/gateway#zstd-stream
Checklist
uv run nox -s lintuv run nox -s pyright