Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: avoid unnecessary allocation #3747

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions src/common/meta/src/state_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use common_procedure::store::util::multiple_value_stream;
use common_procedure::Result as ProcedureResult;
use futures::future::try_join_all;
use futures::StreamExt;
use itertools::Itertools;
use snafu::ResultExt;

use crate::error::Result;
Expand Down Expand Up @@ -80,21 +79,17 @@ fn decode_kv(kv: KeyValue) -> Result<(String, Vec<u8>)> {
Ok((key, value))
}

enum SplitValue {
Single(Vec<u8>),
Multiple(Vec<Vec<u8>>),
enum SplitValue<'a> {
Single(&'a [u8]),
Multiple(Vec<&'a [u8]>),
}

fn split_value(value: Vec<u8>, max_value_size: Option<usize>) -> SplitValue {
fn split_value(value: &[u8], max_value_size: Option<usize>) -> SplitValue<'_> {
if let Some(max_value_size) = max_value_size {
if value.len() <= max_value_size {
SplitValue::Single(value)
} else {
let mut values = vec![];
for chunk in value.into_iter().chunks(max_value_size).into_iter() {
values.push(chunk.collect());
}
SplitValue::Multiple(values)
SplitValue::Multiple(value.chunks(max_value_size).collect::<Vec<_>>())
}
} else {
SplitValue::Single(value)
Expand All @@ -104,7 +99,7 @@ fn split_value(value: Vec<u8>, max_value_size: Option<usize>) -> SplitValue {
#[async_trait]
impl StateStore for KvStateStore {
async fn put(&self, key: &str, value: Vec<u8>) -> ProcedureResult<()> {
let split = split_value(value, self.max_value_size);
let split = split_value(&value, self.max_value_size);
let key = with_prefix(key);
match split {
SplitValue::Single(value) => {
Expand Down