-
Notifications
You must be signed in to change notification settings - Fork 22
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
Seek motivating example for more than 3 simultaneous borrows #27
Comments
Following comments on Reddit, theoretical cost increases as O(N ln N) and ideally we should use a "sorting network" to implement the comparisons. But still no-one has come up with any example code that actually uses that many simultaneous mutable borrows, and is more efficient if coded that way (as opposed to being coded to work with borrows of 1-3 cells at a time). |
I have created the motivating example for more than 3 simultaneous borrows in the separate repository. First approach was discussed in PR #26: the feature could be implemented for array of But I came up with the second approach: the feature could be implemented for tuples of arity 12 and less (which is a common approach in many other crates, even in (A, B, C, D, E, F, G, H, I, J, K, L) where each type could be different from each other. With the first approach, users will not be able to use this feature with different types as it is possible with With tuples existing implementation for 2 and 3 simultaneous borrows could be reused, so users will not notice any difference. But for tuples of arity from 4 to 12 implementation can make owner and distinct checks with array or similar algorithm before obtaining mutable references. |
Also I think it should be possible to implement this feature for not only an array of cells, but for vector of cells of unknown at compile time length. |
Okay, for your code example, I would have put the cells inside of the Storage. If you were using TCell or TLCell, that would have zero overhead in memory or time for access. When it comes to the core part of your code, you'd end up with code like this:
Since all of those However I'm still open to discussing things further if that does not meet your requirements. |
I've been thinking some more about this idea of using QCell/etc to get mutable access to various entries in a hashmap or other collection simultaneously (rather than borrowing just one thing at a time, to which it is more suited). The same could be achieved using RefCell, actually. The relative costs are as follows:
In both cases the code will panic if you get it wrong, but that is a bug that will be fixed just once. In both cases there are runtime costs. Depending on how the cross-checks are implemented, that might or might not involve memory writes, e.g. if implemented as a sort rather than a naive double-scan, there will be writes. So there may be some benefit of the QCell approach in this case (compared to RefCell), but it's in the balance. It's not so clear cut as other uses of QCell, where we can make use of static knowledge and eliminate costs entirely and provide total guarantees of correctness at compile-time. So the question is: Can QCell/TCell/etc actually add much value by supporting this case? |
As suggested in PR #26, to get multiple unique mutable borrows from HashMap, we can also use iterator and use only those borrows which are actually needed. So, I think that QCell/TCell/etc add no value by supporting this case. |
Personally, I don't think that this is required. All the cases so far where I've needed simultaneous borrows have been handled by
rw2
. However if there are any concrete cases where 4+ simultaneous borrows are needed, and it would be inefficient to handle them as a sequence ofrw2
orrw3
borrows, it would be good to document them and analyse them. So please add a comment if you have such a requirement. If it would really work out as more efficient to borrow 4+ items at a time (considering the roughly O(N^2) comparisons), there is a draft PR #26 which could be finished off to provide this functionality.The text was updated successfully, but these errors were encountered: