-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Client support for ReadOnlySequence as payload #2017
Conversation
Hi @chkr1011, I am now looking into the option of passing a RecyclableMemoryStream (or a custom MemoryStream) into the reader to also benefit from better buffer management using ReadOnlySequence on the receiver side. |
@mregen I never used that RecyclableMemoryStream before. The idea of not increasing the buffer every time (only doing this one time when GetBuffer is called) looks like a reasonable optimization. Isn't this something we can also do in the current writer implementation? Shall we finish this brach first and move the change to another branch? I am afraid that this branch will grow too much. |
Hi @chkr1011, currently I am trying to work out the details which we need at the interface level to allow for the lifetime management of the ReadOnlySequence Payload using the PayloadOwner if we use buffers for the payload. It doesn't have to be RecyclablaMemoryStream, but something that can take advantage of buffers and ReadOnlySequence (My preference is currently ArrayPool based bufferning because it allows for readjustable buffer size). |
Hi @chkr1011, please have a look now and provide feedback. From my side the functionality for buffer ownership handling is included. Apparently there is a breaking change in the client for the lifetime of the payload, its disposed when the event callback returns, to make efficent use of buffers. |
Do you have sample code for this? What do you think about having an option for this. I am afraid that managing the buffers is too complicated for most users which just want to receive and send some messages and never complained about memory consumption. My idea is to copy the buffers by default so that users can deal with the messages later or in other places easily. Users who want so to optimize the memory allocations can choose this in the options and then have to copy/release the buffer on their own (including more complexity). What do you think about this idea?
Please let us finish this branch first and continue in new branches so that I can start importing the changes from main (version 4) which happened in the meantime. |
Hi @chkr1011, thanks for the feedback..
I will provide some, also there should be some samples for using a RecyclableMemoryStream. I have been running into some trouble with this implementation myself on the reader side, so I am trying to make it safe for user. Your next statement sounds like a really good idea..
Yes, that is probably a good idea, so by default the apps can just recompile, once you set the option you need to do more. Where would you put such an option? Thanks, Martin |
In my opinion we should introduce a new property in the client and server options with the same naming. For example, "EnableManualPayloadManagement" or similar. Then the summary tag should contain all information required to manage the payload buffers properly. |
@chkr1011 Sounds good, I can take a look today. I had some improvements for this branch to catch memory issues, I will post it for the records. But I rather then start again with a clean branch. Important for me is currently more the send path, where the lifetime can be managed externally, but it should be possible to upgrade the MQTT lib later for the receive path without breaking change, that would be good. |
Support
ReadOnlySequence
as a payload in the MQTTnet client, for the new version5 release.The MQTTNet write method only accepts an
ArraySegment
which is a view into a single buffer. It is currently necessary to callToArray
to support a RecyclableMemoryStream.As a result, perfomance is affected by additional copies and memory is fragment by the additional random allocation of a big buffer.
Starting the port to the lates code to gather feedback.
see #1917 and #1918 with the issue explained.