Skip to content

Commit

Permalink
chore: remove one slice copy in wal encoding (#3861)
Browse files Browse the repository at this point in the history
* chore: remove one slice copy in wal encoding

Signed-off-by: Lei, HUANG <[email protected]>

* fix: cr comments

---------

Signed-off-by: Lei, HUANG <[email protected]>
  • Loading branch information
v0y4g3r authored May 6, 2024
1 parent 6e9e8fa commit f3b6825
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 25 deletions.
9 changes: 2 additions & 7 deletions src/log-store/src/kafka/log_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,9 @@ impl LogStore for KafkaLogStore {
type Namespace = NamespaceImpl;

/// Creates an entry of the associated Entry type.
fn entry<D: AsRef<[u8]>>(
&self,
data: D,
entry_id: EntryId,
ns: Self::Namespace,
) -> Self::Entry {
fn entry(&self, data: &mut Vec<u8>, entry_id: EntryId, ns: Self::Namespace) -> Self::Entry {
EntryImpl {
data: data.as_ref().to_vec(),
data: std::mem::take(data),
id: entry_id,
ns,
}
Expand Down
9 changes: 2 additions & 7 deletions src/log-store/src/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,7 @@ impl LogStore for NoopLogStore {
Ok(vec![])
}

fn entry<D: AsRef<[u8]>>(
&self,
data: D,
entry_id: EntryId,
ns: Self::Namespace,
) -> Self::Entry {
fn entry(&self, data: &mut Vec<u8>, entry_id: EntryId, ns: Self::Namespace) -> Self::Entry {
let _ = data;
let _ = entry_id;
let _ = ns;
Expand Down Expand Up @@ -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();
Expand Down
9 changes: 2 additions & 7 deletions src/log-store/src/raft_engine/log_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,15 +402,10 @@ impl LogStore for RaftEngineLogStore {
Ok(namespaces)
}

fn entry<D: AsRef<[u8]>>(
&self,
data: D,
entry_id: EntryId,
ns: Self::Namespace,
) -> Self::Entry {
fn entry(&self, data: &mut Vec<u8>, 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()
}
Expand Down
4 changes: 2 additions & 2 deletions src/mito2/src/wal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub struct WalWriter<S: LogStore> {
}

impl<S: LogStore> WalWriter<S> {
/// 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,
Expand All @@ -157,7 +157,7 @@ impl<S: LogStore> WalWriter<S> {
.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);

Expand Down
3 changes: 1 addition & 2 deletions src/store-api/src/logstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<D: AsRef<[u8]>>(&self, data: D, entry_id: EntryId, ns: Self::Namespace)
-> Self::Entry;
fn entry(&self, data: &mut Vec<u8>, 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;
Expand Down

0 comments on commit f3b6825

Please sign in to comment.