Avoid unnecessary clone()

This commit is contained in:
Louis Dureuil 2024-08-08 14:57:02 +02:00
parent 2f10273d14
commit c3cdc407ec
No known key found for this signature in database

View File

@ -317,11 +317,15 @@ fn deladd_obkv_cbo_roaring_bitmaps(
} }
/// Truncates a string to the biggest valid LMDB key size. /// Truncates a string to the biggest valid LMDB key size.
fn truncate_string(s: String) -> String { fn truncate_str(s: &str) -> &str {
s.char_indices() let index = s
.take_while(|(idx, _)| idx + 4 < MAX_FACET_VALUE_LENGTH) .char_indices()
.map(|(_, c)| c) .map(|(idx, _)| idx)
.collect() .chain(std::iter::once(s.len()))
.take_while(|idx| idx <= &MAX_FACET_VALUE_LENGTH)
.last();
&s[..index.unwrap_or(0)]
} }
/// Computes the diff between both Del and Add numbers and /// Computes the diff between both Del and Add numbers and
@ -466,7 +470,7 @@ where
obkv.insert(DelAdd::Addition, add)?; obkv.insert(DelAdd::Addition, add)?;
} }
let truncated = truncate_string(normalized.clone()); let truncated = truncate_str(normalized);
key_buffer.extend_from_slice(truncated.as_bytes()); key_buffer.extend_from_slice(truncated.as_bytes());
let bytes = obkv.into_inner()?; let bytes = obkv.into_inner()?;
@ -490,7 +494,7 @@ where
(DelAdd::Addition, normalized, original) (DelAdd::Addition, normalized, original)
} }
}; };
let truncated = truncate_string(normalized.clone()); let truncated = truncate_str(normalized);
key_buffer.extend_from_slice(truncated.as_bytes()); key_buffer.extend_from_slice(truncated.as_bytes());
let mut obkv = KvWriterDelAdd::memory(); let mut obkv = KvWriterDelAdd::memory();