How can I better store a string to avoid many clones?

孤街浪徒 提交于 2021-02-05 06:31:21

问题


I am using tokio's UdpCodec trait:

pub trait UdpCodec {
    type In;
    type Out;
    fn decode(&mut self, src: &SocketAddr, buf: &[u8]) -> Result<Self::In>;
    fn encode(&mut self, msg: Self::Out, buf: &mut Vec<u8>) -> SocketAddr;
}

My associated type for In is a (SocketAddr, Vec<Metric>). Metric is defined as:

#[derive(Debug, PartialEq)]
pub struct Metric {
    pub name: String,
    pub value: f64,
    pub metric_type: MetricType,
    pub sample_rate: Option<f64>,
}

I have used owned strings to avoid lifetime constraints with the associated types. However I also do HashMap lookups and inserts with these metric names which involves a lot of cloning since I borrow metrics in other functions.

How can I better store a string within this Metric type to avoid many inefficient clones? Using the Cow type has crossed my mind but it also obviously has a lifetime association.


回答1:


Expanding on @Josh's suggestion, I would suggest using interning.

Depending on how memory or CPU intensive your task is, make your pick between:

  • A double hash-map: ID <-> String, shared between components
  • A single hash-map: String -> Rc<str>

If you can afford the latter, I definitely advise it. Also note that you can likely fold MetricType within the Rc: Rc<(MetricType, str)>.

Then you still need to call clone left and right, but each is just a cheap non-atomic increment operation... and moving to multithread is as simple as swapping Arc for Rc.



来源:https://stackoverflow.com/questions/42097611/how-can-i-better-store-a-string-to-avoid-many-clones

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!