Draft: refactoring to eliminate redundant data copying
Summary
Explanation
This MR is attempting to remove some amount of redundant copies that we're doing in different parts of our code base (but mostly pluing subsystem for now). This is a work-in-progress, because the first attempt shows how time-consuming this endavour is. The udnerlying problem is -- rust language is too rigid when it comes to refactoring especially when it's related to lifetime parameters. As a result it enforces an inefficient style of programming, that is cloning data everywhere. The usual crutch of wrapping everything in a Rc<T>
may help us in some places, but unfortunately in our case most of our structs are created by deserializing tuples from the database, and constructing the Rc<str>
from that would still involve allocating a redundant string on the heap (instead of simply storing a reference into the serialized data).
Conclusion
It seems to me that the best way forward is to ingore the problem until it inevitably surfaces on flamegraphs at which point we'll have to start doing the gruelling work of optimizing rust code but only in the hot spots which really affect the overall performance.
This doesn't mean however that we should just completely ignore the problem and just clone() values everywhere it's possible. We should still try passing thing's by reference and consider creating new structs with the support for storing data by reference rather than by value. ServiceRouteKey
is a good example, but sometimes you do need to store a field by value, so Cow<'a, T>
may be used (see ServiceRouteItem
in this MR)
- Cherry-pick to: none
- Docs follow-up: not necessary