rumqtt icon indicating copy to clipboard operation
rumqtt copied to clipboard

`ValidatedTopic` to prevent redundant topic validation

Open CramBL opened this issue 7 months ago • 0 comments

Topics are validated on every publish e.g. in handle_publish https://github.com/bytebeamio/rumqtt/blob/005f73caf0c2a808210fd73f2db6e1bc38652c5c/rumqttc/src/v5/client.rs#L90

It would be great to avoid this overhead considering that topics are often known at compile-time.

Even though it's not a fantastically great overhead, I think the assembly shows that it is also not completely negligible: https://gcc.godbolt.org/z/evE4P88re

I'm aware of the history surrounding this change, namely:

  • https://github.com/bytebeamio/rumqtt/issues/595
  • https://github.com/bytebeamio/rumqtt/issues/774

Proposed solution

Reference implementation for v5: https://github.com/bytebeamio/rumqtt/pull/980

Introduce a ValidatedTopic newtype that wraps a String, and use traits to differentiate it from regular String types. This is a solution that would maintain the existing API, allowing String/&str as the topic, but make it opt-in to use the "pre-validated" newtype for optimization.

Then we can also differentiate ValidatedTopic for v3(.1.1) and v5 by only allowing an empty string for the v5 version, which would resolve https://github.com/bytebeamio/rumqtt/issues/595.

CramBL avatar Jul 22 '25 11:07 CramBL