From f3b68253c2d5c61f696379e65721cb0980131a25 Mon Sep 17 00:00:00 2001 From: "Lei, HUANG" <6406592+v0y4g3r@users.noreply.github.com> Date: Mon, 6 May 2024 18:26:40 +0800 Subject: [PATCH] chore: remove one slice copy in wal encoding (#3861) * chore: remove one slice copy in wal encoding Signed-off-by: Lei, HUANG * fix: cr comments --------- Signed-off-by: Lei, HUANG --- src/log-store/src/kafka/log_store.rs | 9 ++------- src/log-store/src/noop.rs | 9 ++------- src/log-store/src/raft_engine/log_store.rs | 9 ++------- src/mito2/src/wal.rs | 4 ++-- src/store-api/src/logstore.rs | 3 +-- 5 files changed, 9 insertions(+), 25 deletions(-) diff --git a/src/log-store/src/kafka/log_store.rs b/src/log-store/src/kafka/log_store.rs index 14f7ba8df1bb..1a0f96b6587b 100644 --- a/src/log-store/src/kafka/log_store.rs +++ b/src/log-store/src/kafka/log_store.rs @@ -59,14 +59,9 @@ impl LogStore for KafkaLogStore { type Namespace = NamespaceImpl; /// Creates an entry of the associated Entry type. - fn entry>( - &self, - data: D, - entry_id: EntryId, - ns: Self::Namespace, - ) -> Self::Entry { + fn entry(&self, data: &mut Vec, entry_id: EntryId, ns: Self::Namespace) -> Self::Entry { EntryImpl { - data: data.as_ref().to_vec(), + data: std::mem::take(data), id: entry_id, ns, } diff --git a/src/log-store/src/noop.rs b/src/log-store/src/noop.rs index 48668c39ec04..1d28e67dce16 100644 --- a/src/log-store/src/noop.rs +++ b/src/log-store/src/noop.rs @@ -97,12 +97,7 @@ impl LogStore for NoopLogStore { Ok(vec![]) } - fn entry>( - &self, - data: D, - entry_id: EntryId, - ns: Self::Namespace, - ) -> Self::Entry { + fn entry(&self, data: &mut Vec, entry_id: EntryId, ns: Self::Namespace) -> Self::Entry { let _ = data; let _ = entry_id; let _ = ns; @@ -140,7 +135,7 @@ mod tests { #[tokio::test] async fn test_noop_logstore() { let store = NoopLogStore; - let e = store.entry("".as_bytes(), 1, NamespaceImpl); + let e = store.entry(&mut vec![], 1, NamespaceImpl); let _ = store.append(e.clone()).await.unwrap(); assert!(store.append_batch(vec![e]).await.is_ok()); store.create_namespace(&NamespaceImpl).await.unwrap(); diff --git a/src/log-store/src/raft_engine/log_store.rs b/src/log-store/src/raft_engine/log_store.rs index f5fab5f97eed..b2070abbf3ec 100644 --- a/src/log-store/src/raft_engine/log_store.rs +++ b/src/log-store/src/raft_engine/log_store.rs @@ -402,15 +402,10 @@ impl LogStore for RaftEngineLogStore { Ok(namespaces) } - fn entry>( - &self, - data: D, - entry_id: EntryId, - ns: Self::Namespace, - ) -> Self::Entry { + fn entry(&self, data: &mut Vec, entry_id: EntryId, ns: Self::Namespace) -> Self::Entry { EntryImpl { id: entry_id, - data: data.as_ref().to_vec(), + data: std::mem::take(data), namespace_id: ns.id(), ..Default::default() } diff --git a/src/mito2/src/wal.rs b/src/mito2/src/wal.rs index 49b6dd493148..06e38f36f98f 100644 --- a/src/mito2/src/wal.rs +++ b/src/mito2/src/wal.rs @@ -135,7 +135,7 @@ pub struct WalWriter { } impl WalWriter { - /// Add an wal entry for specific region to the writer's buffer. + /// Add a wal entry for specific region to the writer's buffer. pub fn add_entry( &mut self, region_id: RegionId, @@ -157,7 +157,7 @@ impl WalWriter { .context(EncodeWalSnafu { region_id })?; let entry = self .store - .entry(&self.entry_encode_buf, entry_id, namespace); + .entry(&mut self.entry_encode_buf, entry_id, namespace); self.entries.push(entry); diff --git a/src/store-api/src/logstore.rs b/src/store-api/src/logstore.rs index d6ebcb2573c2..33739ac85fb4 100644 --- a/src/store-api/src/logstore.rs +++ b/src/store-api/src/logstore.rs @@ -72,8 +72,7 @@ pub trait LogStore: Send + Sync + 'static + std::fmt::Debug { async fn obsolete(&self, ns: Self::Namespace, entry_id: EntryId) -> Result<(), Self::Error>; /// Makes an entry instance of the associated Entry type - fn entry>(&self, data: D, entry_id: EntryId, ns: Self::Namespace) - -> Self::Entry; + fn entry(&self, data: &mut Vec, entry_id: EntryId, ns: Self::Namespace) -> Self::Entry; /// Makes a namespace instance of the associated Namespace type fn namespace(&self, ns_id: NamespaceId, wal_options: &WalOptions) -> Self::Namespace;