-
Notifications
You must be signed in to change notification settings - Fork 100
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
Allocation heavy #32
Comments
It may also be wise to use some sort of interning for things like tags or commands. Servo uses the excellent string_cache library. |
Yeah, I've been meaning to reduce the allocations, but have been putting it off as likely premature optimization. It should probably be done though. I'll look into interning. |
I've attempted to do this and after about three-four days of work I honestly think it's not worth it. To avoid allocations as far as possible, the
and for those two cases, either
The case of separate strings for each argument is trivial: we stick But if we have one string for the whole message, and we don't want to allocate for each token, we have to
This makes the library a lot more complex, at least to me; many functions need to be duplicated and slightly tweaked to parse into ranges or parse into slices, and there is really no way to use a macro or something to reduce this semi-repetition. Or, perhaps, my Rust ability is not good enough to do this. I don't know. You can take a look at my abandoned attempt here. It does not compile. |
To complement the suggestion of |
The best example that I have encountered of such an implementation would have to be |
I'm going to start working on this in my own fork. If you all would like to review and contribute to it, maybe we can move it into a branch here to eventually be merged further. |
I would prefer it on 0.14 over develop, since that's the release I'd like to include the change in. I had attempted some work in this direction on the fewer_allocations branch as well. I think there were some issues when I merged |
I'll take a look at it, thanks! @angelsl While I understand the sentiment of zero allocation, building it up from separate string slices, I think producing a single owned, allocated buffer from the parts would be a decent compromise on performance that will reduce the complexity. Your idea, in theory, could be retained as part of a builder pattern that eventually can get converted into an owned message or formatted with Display. |
Update: I put together an optimized version of the I toyed around with the idea of a separate DST type based around EDIT: Rebased on 0.14 |
Going to put some of my thoughts here to give some focus and direction to what I'm doing. Feedback is welcome! When designing the new protocol implementation, we need to think about the ways that the end-user is going to use it. The two main ways I can think of are:
In both of these scenarios, we want to avoid as much copying and allocation as possible, as that is the whole point of this issue. Other variables include the complexity of the API, which we should try to minimize. I'm not sure whether that should be prioritized over the first point, and if so, how much we should compromise. For the parsing scenario, we are given a stream of data or a string slice that we need to read, parse, and return string slices from, with the given goals:
For building messages, it makes sense to employ some kind of builder pattern that simply holds on to references for the message parts and then constructs the message in some buffer or writer. I'm a bit wary about using borrowing and lifetimes right now, because it gets hairy when you try to give borrowed data to a future that is supposed to be In the meantime, a good alternative might the |
Every occurence of
String
orVec
is an allocation. Especially for sending messages, this seems a bit wasteful! TheCow
(clone-on-write) type (andInto<Cow<str>>
) can ease this burden, by allowing the flexibility of the user providing either a slice or aString
, and the library only cloning it for unique ownership where necessary (if ever). This isn't a very practical concern, admittedly, and is more about good style and creating the best IRC library in existence.The text was updated successfully, but these errors were encountered: