-
Notifications
You must be signed in to change notification settings - Fork 227
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
allow sending messages with static or shared data #175
Conversation
Another approach would be to implement #96 similar to the way it was tried to be implemented here: #104 , but to use |
I've changed things around a bit, switching to custom enums for the binary and string data to allow dealing with shared data trough I've currently not added support from shared string data, as there is no way to go from |
I'd eventually like to review this, but I'll probably need a couple more days before I manage so. @jxs, do you want too? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks generally good. I'm trying to understand whether this would make the client side any more inefficient.
The general question we should answer is: are the (efficiency) gains worth increasing the complexity slightly? It would be interesting to see a benchmark quantifying the gains.
Message::Text(string) => string.into_bytes(), | ||
Message::Binary(data) | Message::Ping(data) | Message::Pong(data) => data, | ||
Message::Text(string) => String::from(string).into(), | ||
Message::Binary(data) => data.into(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it true that this would now copy in case when data
is Bytes
, while previously this never copied? Not sure whether that's a concern at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is an issue in practice, since you're probably only interested in the message data when it's an incoming message, in which case it's always unique (and thus stored as an Vec<u8>
) anyway.
0167089
to
a73434b
Compare
I have some mostly finished code that removes the need to make the data a unique copy when applying the mask that I was originally going to put into a follow up pr but I can add it here is desired, |
That would be interesting to check (probably in a separate commit?). |
this removes the need to have unique mutable access to the payload data
pushed |
When will this be fully merged? I think it's a very nice feature and doesn't make stuff too complex. |
I need to send data that allocated by custom allocator. Can it be |
It has some unresolved merge conflicts. It seems like replacing the binary bytes with
Do you mean |
I don't see how I have data allocated with Yes, I can
Yes, something like this, it can be |
What I mean is that imagine that you're allocating memory with
The only universal solution that I have in mind is to make |
@Dushistov, so for your particular question the workaround would be to create The only problem with this approach is that you'll have to copy the data which is not particularly efficient, but if we make the type of In the case of this PR, it's mitigated by making the |
I am already doing so. But this looks like waste, C++ allocate buffer, copy data to it
if tungstenite only want to own data then this is not problem, wraper around allocated data has proper
generic |
Ok, I think I finally got your point :) So basically you don't really need sending the shared data (like this PR mentions), you're more concerned about the fact that currently you can't pass a custom
Hm, not quite. I mean, of course we could define different data types for incoming and outgoing message (strictly speaking there is nothing wrong with it), but we would need to update quite a few things to make it ergonomic with |
Just wanted to say that this PR seems capable of helping to save a lot on allocations in cases where the same messages are being broadcast to lots of web socket connections like it is in our use case. Although, admittedly, it would take some changes in other crates before this can be utilized by end users. |
There is idea for It would make possible to support my use case, when I want pass buffer created by C/C++ code with |
Looks like it is possible now via Bytes::from_owner |
This allows the server to skip allocating buffers holding the message data to be send if the data is static.
Message
fields data make this a breaking change for any code directly creating or deconstructing theMessage
enum.text
andbinary
methods intact requires adding separatestatic_*
methods, an alternative might be to changetext
/binary
to take anInto<Cow<_>>
directly, this would break any code calling those methods with anything that doesn't implementInto<Cow<_>>
but should be fine for the most common parameters ofString/&str/Vec<u8>/&[u8]