Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for RecyclableMemoryManager, client write support for ReadOnlySequence as payload #1917

Open
mregen opened this issue Jan 27, 2024 · 0 comments
Labels
feature-request New feature or request

Comments

@mregen
Copy link

mregen commented Jan 27, 2024

Describe the feature request

In a streaming scenario for telemetry sending Megabytes per second to a MQTT Broker with JSON or compressed payload,
it is beneficial to encode into fixed size buffers in the processing pipeline to avoid memory fragmentation.
Using a normal MemoryStream or ToArray leads to memory fragmentation and specifically if the client is hosted in Kubernetes, it can cause eviction of the pod.

The solution is the RecyclableMemoryStream which can be used to encode the payload. After encoding the stream, it has a method to pass a ReadOnlySequence along the encoding pipeline which is valid until the memory stream is disposed. The ReadOnlySequence contains a list of buffers which contain the payload in chunks.
The usage is quite efficent because the buffers are recycled without allocations when the RecyclableMemoryStream is disposed.

However, the MQTTNet write method only accepts an ArraySegment which is a view into a single buffer. It is currently necessary to call ToArray which leads to a full size memory allocation and copy of the whole payload.
As a result, perfomance is affected by additional copies and memory is fragment by the additional random allocation of a big buffer.

In order to natively support ReadOnlySequence, MqttPublishPacket should support a PayloadSequence instead of a PayloadSegment in the MQTTnet client.

Turns out the support can be added with a few surgical changes. see #1918

Which project is your feature request related to?

  • Client

Describe the solution you'd like

#1918 is a draft PR. Please provide the feedback if and how such an enhancement can be accepted by the project.

Describe alternatives you've considered

A clear and concise description of any alternative solutions or features you've considered.

Additional context

Add any other context or screenshots about the feature request here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant