perf: some inline magic for Draft

This would improve 8-14% CPU for all API
This commit is contained in:
Tunglies
2025-11-10 07:15:33 +08:00
parent 204bfa36e5
commit e66a4f6aca
3 changed files with 7 additions and 51 deletions

47
src-tauri/Cargo.lock generated
View File

@@ -2,15 +2,6 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 4 version = 4
[[package]]
name = "addr2line"
version = "0.25.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b5d307320b3181d6d7954e663bd7c774a838b8220fe0593c86d9fb09f498b4b"
dependencies = [
"gimli",
]
[[package]] [[package]]
name = "adler2" name = "adler2"
version = "2.0.1" version = "2.0.1"
@@ -581,21 +572,6 @@ dependencies = [
"tokio", "tokio",
] ]
[[package]]
name = "backtrace"
version = "0.3.76"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb531853791a215d7c62a30daf0dde835f381ab5de4589cfe7c649d2cbe92bd6"
dependencies = [
"addr2line",
"cfg-if",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
"windows-link 0.2.1",
]
[[package]] [[package]]
name = "base62" name = "base62"
version = "2.2.3" version = "2.2.3"
@@ -2826,12 +2802,6 @@ dependencies = [
"polyval", "polyval",
] ]
[[package]]
name = "gimli"
version = "0.32.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e629b9b98ef3dd8afe6ca2bd0f89306cec16d43d907889945bc5d6687f2f13c7"
[[package]] [[package]]
name = "gio" name = "gio"
version = "0.18.4" version = "0.18.4"
@@ -4941,15 +4911,6 @@ dependencies = [
"objc2-security", "objc2-security",
] ]
[[package]]
name = "object"
version = "0.37.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff76201f031d8863c38aa7f905eca4f53abbfa15f609db4277d44cd8938f33fe"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.21.3" version = "1.21.3"
@@ -5158,10 +5119,8 @@ version = "0.9.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1"
dependencies = [ dependencies = [
"backtrace",
"cfg-if", "cfg-if",
"libc", "libc",
"petgraph",
"redox_syscall 0.5.18", "redox_syscall 0.5.18",
"smallvec", "smallvec",
"windows-link 0.2.1", "windows-link 0.2.1",
@@ -6409,12 +6368,6 @@ dependencies = [
"ordered-multimap", "ordered-multimap",
] ]
[[package]]
name = "rustc-demangle"
version = "0.1.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56f7d92ca342cea22a06f2121d944b4fd82af56988c270852495420f961d4ace"
[[package]] [[package]]
name = "rustc-hash" name = "rustc-hash"
version = "2.1.1" version = "2.1.1"

View File

@@ -171,10 +171,6 @@ crate-type = ["staticlib", "cdylib", "rlib"]
[dev-dependencies] [dev-dependencies]
criterion = { version = "0.7.0", features = ["async_tokio"] } criterion = { version = "0.7.0", features = ["async_tokio"] }
parking_lot = { version = "0.12.5", features = [
"hardware-lock-elision",
"deadlock_detection",
] }
[lints.clippy] [lints.clippy]
# Core categories - most important for code safety and correctness # Core categories - most important for code safety and correctness

View File

@@ -12,12 +12,14 @@ pub struct Draft<T: Clone> {
} }
impl<T: Clone> Draft<T> { impl<T: Clone> Draft<T> {
#[inline]
pub fn new(data: T) -> Self { pub fn new(data: T) -> Self {
Self { Self {
inner: Arc::new(RwLock::new((Arc::new(Box::new(data)), None))), inner: Arc::new(RwLock::new((Arc::new(Box::new(data)), None))),
} }
} }
/// 以 Arc<Box<T>> 的形式获取当前“已提交(正式)”数据的快照(零拷贝,仅 clone Arc /// 以 Arc<Box<T>> 的形式获取当前“已提交(正式)”数据的快照(零拷贝,仅 clone Arc
#[inline]
pub fn data_arc(&self) -> SharedBox<T> { pub fn data_arc(&self) -> SharedBox<T> {
let guard = self.inner.read(); let guard = self.inner.read();
Arc::clone(&guard.0) Arc::clone(&guard.0)
@@ -25,6 +27,7 @@ impl<T: Clone> Draft<T> {
/// 获取当前(草稿若存在则返回草稿,否则返回已提交)的快照 /// 获取当前(草稿若存在则返回草稿,否则返回已提交)的快照
/// 这也是零拷贝:只 clone Arc不 clone T /// 这也是零拷贝:只 clone Arc不 clone T
#[inline]
pub fn latest_arc(&self) -> SharedBox<T> { pub fn latest_arc(&self) -> SharedBox<T> {
let guard = self.inner.read(); let guard = self.inner.read();
guard.1.clone().unwrap_or_else(|| Arc::clone(&guard.0)) guard.1.clone().unwrap_or_else(|| Arc::clone(&guard.0))
@@ -33,6 +36,7 @@ impl<T: Clone> Draft<T> {
/// 通过闭包以可变方式编辑草稿(在闭包中我们给出 &mut T /// 通过闭包以可变方式编辑草稿(在闭包中我们给出 &mut T
/// - 延迟拷贝:如果只有这一个 Arc 引用,则直接修改,不会克隆 T /// - 延迟拷贝:如果只有这一个 Arc 引用,则直接修改,不会克隆 T
/// - 若草稿被其他读者共享Arc::make_mut 会做一次 T.clone最小必要拷贝 /// - 若草稿被其他读者共享Arc::make_mut 会做一次 T.clone最小必要拷贝
#[inline]
pub fn edit_draft<F, R>(&self, f: F) -> R pub fn edit_draft<F, R>(&self, f: F) -> R
where where
F: FnOnce(&mut T) -> R, F: FnOnce(&mut T) -> R,
@@ -56,6 +60,7 @@ impl<T: Clone> Draft<T> {
} }
/// 将草稿提交到已提交位置(替换),并清除草稿 /// 将草稿提交到已提交位置(替换),并清除草稿
#[inline]
pub fn apply(&self) { pub fn apply(&self) {
let mut guard = self.inner.write(); let mut guard = self.inner.write();
if let Some(d) = guard.1.take() { if let Some(d) = guard.1.take() {
@@ -64,6 +69,7 @@ impl<T: Clone> Draft<T> {
} }
/// 丢弃草稿(如果存在) /// 丢弃草稿(如果存在)
#[inline]
pub fn discard(&self) { pub fn discard(&self) {
let mut guard = self.inner.write(); let mut guard = self.inner.write();
guard.1 = None; guard.1 = None;
@@ -71,6 +77,7 @@ impl<T: Clone> Draft<T> {
/// 异步地以拥有 Box<T> 的方式修改已提交数据:将克隆一次已提交数据到本地, /// 异步地以拥有 Box<T> 的方式修改已提交数据:将克隆一次已提交数据到本地,
/// 异步闭包返回新的 Box<T>(替换已提交数据)和业务返回值 R。 /// 异步闭包返回新的 Box<T>(替换已提交数据)和业务返回值 R。
#[inline]
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,