如何更好地存储字符串以避免出现多次克隆? [英] How can I better store a string to avoid many clones?
问题描述
我正在使用 tokio 的 UdpCodec
特性:
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;
}
我的 In
关联类型是 (SocketAddr, Vec
.Metric
定义为:
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>,
}
我使用了拥有的字符串来避免关联类型的生命周期限制.但是,我也使用这些指标名称进行 HashMap
查找和插入,这涉及大量克隆,因为我在其他函数中借用了指标.
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.
如何更好地在此 Metric
类型中存储字符串以避免许多低效克隆?使用 Cow
类型已经让我想到了,但它显然也有终身关联.
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.
推荐答案
扩展@Josh 的建议,我建议使用实习.
Expanding on @Josh's suggestion, I would suggest using interning.
根据您的任务的内存或 CPU 密集程度,在以下选项之间进行选择:
Depending on how memory or CPU intensive your task is, make your pick between:
- 双哈希映射:
ID
<->String
,在组件之间共享 - 单个哈希映射:
String
->Rc
- A double hash-map:
ID
<->String
, shared between components - A single hash-map:
String
->Rc<str>
如果你能负担得起后者,我绝对建议.另请注意,您可能会在 Rc
中折叠 MetricType
:Rc<(MetricType, 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)>
.
那么你仍然需要向左和向右调用clone
,但每个都只是一个廉价的非原子增量操作......而移动到多线程就像交换Arc
一样简单Rc
的代码>.
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
.
这篇关于如何更好地存储字符串以避免出现多次克隆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!