Reduce memcpy to coalesce WebSocket messages.

In most cases its faster to avoid making the inner
payload and than copying it again into the outer
payload.
This commit is contained in:
J. Nick Koston 2025-03-29 20:13:22 -10:00
parent 9f2232fad1
commit c4133b9972
No known key found for this signature in database

View File

@ -7,6 +7,7 @@ from collections import deque
from collections.abc import Callable, Coroutine from collections.abc import Callable, Coroutine
import datetime as dt import datetime as dt
from functools import partial from functools import partial
from itertools import chain
import logging import logging
from typing import TYPE_CHECKING, Any, Final from typing import TYPE_CHECKING, Any, Final
@ -162,7 +163,8 @@ class WebSocketHandler:
await send_bytes_text(message) await send_bytes_text(message)
continue continue
coalesced_messages = b"".join((b"[", b",".join(message_queue), b"]")) messages = tuple(chain.from_iterable((x, b",") for x in message_queue))
coalesced_messages = b"".join((b"[", *messages[:-1], b"]"))
message_queue.clear() message_queue.clear()
if is_debug_log_enabled(): if is_debug_log_enabled():
debug("%s: Sending %s", self.description, coalesced_messages) debug("%s: Sending %s", self.description, coalesced_messages)