Allow user properties as pre-encoded UTF-8 binary buffer #2228
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.
Issue:
When extensively using MQTT v5 user properties, handling the key and value as string has two major downsides (allocation time and garbage collection). Keys can use string interning or defined as const but the values (when they are changing) can cause high allocation rate and latency spikes when the garbage collector has to collect them.
Proposal:
Allow value of MQTT v5 user properties to be added as bytes (pre-encoded UTF-8 binary buffer), using performance optimized dotnet types like ReadOnlyMemory. The same optimization was already implemented in version 5.x for the payload of the MQTT message itself.
Outlook:
The current implementation optimizes the MQTT message send path, it would be also possible to optimize the receive path and handling values of user properties only as ReadOnlyMemory and just allocate the string on demand. I didn't implement that yet, as this would require more API changes and might even have negative consequences (when user property values are read multiple times, caching would be required).
Pull Request contains the implementation of the proposal, including tests for binary user properties, a sample demonstrating publishing a message with binary user properties (
Samples/Client/Client_Publish_Samples.cs) and minor fixes for consistency and correctness.