我想使用Rayon的par_iter()
来优化我的功能。Rayon中的每个线程的初始化
的单线程的版本是这样的:
fn verify_and_store(store: &mut Store, txs: Vec<Tx>) {
let result = txs.iter().map(|tx| {
tx.verify_and_store(store)
}).collect();
...
}
每个Store
实例必须由一个线程使用,但只能中Store
多个实例可以同时使用,这样我就可以使通过clone
-ing这个多线程store
:
fn verify_and_store(store: &mut Store, txs: Vec<Tx>) {
let result = txs.par_iter().map(|tx| {
let mut local_store = store.clone();
tx.verify_and_store(&mut local_store)
}).collect();
...
}
然而,这种克隆store
上每迭代,这是WA太慢了。我想每个线程使用一个商店实例。
人造丝这可能吗?还是应该采取手动线程和工作队列?
岂不可惜那里似乎不是是什么范围限定在这个调用(虽然这对于一个体面的案例子集显然是有用的)。 –
@ChrisEmerson是的,我对这个答案感到担忧的是,我无法想象使用安全代码来清理创建的存储(或者在一切完成时运行其他任意命令,比如将其刷新到磁盘)的方法。更糟糕的是,下一次调用'verify_and_store'将继续使用** last **已知的'Store'克隆,这可能与当前的'store'无关。 – user4815162342
谢谢。这可行,但在我个人的情况下,我发现人造丝有'par_chunks'来减少克隆的数量。虽然这可能仍然会导致每个线程有多个克隆,但它没有@ user4815162342描述的范围问题。 – Tomas