Skip to content

Avoid allocation in axum/tungstenite WebSocket message conversion #1095

@kixelated

Description

@kixelated

Problem

In rs/moq-relay/src/websocket.rs, converting between axum and tungstenite WebSocket messages requires going through Vec<u8> / String, which copies the underlying Bytes buffer:

tungstenite::Message::Binary(bin) => axum::extract::ws::Message::Binary(Vec::from(bin).into()),

This is because axum bundles its own version of tungstenite internally, so the Bytes/Utf8Bytes types are incompatible even though they're structurally identical. The conversion currently allocates and copies for every message.

Cause

axum 0.8 uses tungstenite 0.24 internally, while qmux 0.0.4 exports tungstenite 0.28. The Utf8Bytes and Bytes types across these versions are not directly convertible.

Possible fixes

  • Upgrade axum to a version that uses tungstenite 0.28 (when available)
  • Use unsafe transmute between identical Bytes layouts (not recommended)
  • Bypass axum's WebSocket layer entirely and handle the upgrade manually with matching tungstenite version

🤖 Generated with Claude Code

Metadata

Metadata

Assignees

No one assigned

    Labels

    rustPull requests that update Rust code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions