mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-04-17 07:50:33 +08:00
perf(draft): update with_data_modify to use T instead of Box<T> for better performance
Performance improved around 11.7%, avoid to allocate stack and copy to heap.
This commit is contained in:
parent
772b87e733
commit
c06c15450f
@ -71,13 +71,13 @@ impl<T: Clone> Draft<T> {
|
|||||||
pub async fn with_data_modify<F, Fut, R>(&self, f: F) -> Result<R, anyhow::Error>
|
pub async fn with_data_modify<F, Fut, R>(&self, f: F) -> Result<R, anyhow::Error>
|
||||||
where
|
where
|
||||||
T: Send + Sync + 'static,
|
T: Send + Sync + 'static,
|
||||||
F: FnOnce(Box<T>) -> Fut + Send,
|
F: FnOnce(T) -> Fut + Send,
|
||||||
Fut: std::future::Future<Output = Result<(Box<T>, R), anyhow::Error>> + Send,
|
Fut: std::future::Future<Output = Result<(T, R), anyhow::Error>> + Send,
|
||||||
{
|
{
|
||||||
let (local, original_arc) = {
|
let (local, original_arc) = {
|
||||||
let guard = self.inner.read();
|
let guard = self.inner.read();
|
||||||
let arc = Arc::clone(&guard.0);
|
let arc = Arc::clone(&guard.0);
|
||||||
(Box::new((*arc).clone()), arc)
|
((*arc).clone(), arc)
|
||||||
};
|
};
|
||||||
let (new_local, res) = f(local).await?;
|
let (new_local, res) = f(local).await?;
|
||||||
let mut guard = self.inner.write();
|
let mut guard = self.inner.write();
|
||||||
|
|||||||
@ -195,7 +195,7 @@ mod tests {
|
|||||||
// 使用 with_data_modify 异步(立即就绪)地更新 committed
|
// 使用 with_data_modify 异步(立即就绪)地更新 committed
|
||||||
let res = block_on_ready(draft.with_data_modify(|mut v| async move {
|
let res = block_on_ready(draft.with_data_modify(|mut v| async move {
|
||||||
v.enable_auto_launch = Some(true);
|
v.enable_auto_launch = Some(true);
|
||||||
Ok((Box::new(*v), "done")) // Dereference v to get Box<T>
|
Ok((v, "done"))
|
||||||
}));
|
}));
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
{
|
{
|
||||||
@ -217,7 +217,7 @@ mod tests {
|
|||||||
#[allow(clippy::unwrap_used)]
|
#[allow(clippy::unwrap_used)]
|
||||||
let err = block_on_ready(draft.with_data_modify(|v| async move {
|
let err = block_on_ready(draft.with_data_modify(|v| async move {
|
||||||
drop(v);
|
drop(v);
|
||||||
Err::<(Box<IVerge>, ()), _>(anyhow!("boom"))
|
Err::<(IVerge, ()), _>(anyhow!("boom"))
|
||||||
}))
|
}))
|
||||||
.unwrap_err();
|
.unwrap_err();
|
||||||
|
|
||||||
@ -243,7 +243,7 @@ mod tests {
|
|||||||
#[allow(clippy::unwrap_used)]
|
#[allow(clippy::unwrap_used)]
|
||||||
block_on_ready(draft.with_data_modify(|mut v| async move {
|
block_on_ready(draft.with_data_modify(|mut v| async move {
|
||||||
v.enable_auto_launch = Some(false); // 与草稿不同
|
v.enable_auto_launch = Some(false); // 与草稿不同
|
||||||
Ok((Box::new(*v), ())) // Dereference v to get Box<T>
|
Ok((v, ()))
|
||||||
}))
|
}))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user