From 82cc1402c46ce189234b564f8b57de8c35c22ef2 Mon Sep 17 00:00:00 2001 From: Nugine Date: Sun, 15 Jun 2025 21:01:38 +0800 Subject: [PATCH] feat(ecstore): LocalDisk writes file by spawn_blocking --- ecstore/src/disk/local.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/ecstore/src/disk/local.rs b/ecstore/src/disk/local.rs index 5b163c5bb..e13dbd549 100644 --- a/ecstore/src/disk/local.rs +++ b/ecstore/src/disk/local.rs @@ -671,12 +671,21 @@ impl LocalDisk { } }; - let data: &[u8] = match &data { - InternalBuf::Ref(buf) => buf, - InternalBuf::Owned(buf) => buf.as_ref(), - }; - - f.write_all(data).await.map_err(to_file_error)?; + match data { + InternalBuf::Ref(buf) => { + f.write_all(buf).await.map_err(to_file_error)?; + } + InternalBuf::Owned(buf) => { + // Reduce one copy by using the owned buffer directly. + // It may be more efficient for larger writes. + let mut f = f.into_std().await; + let task = tokio::task::spawn_blocking(move || { + use std::io::Write as _; + f.write_all(buf.as_ref()).map_err(to_file_error) + }); + task.await??; + } + } Ok(()) }