From e17a38b1c44215911c1a84d94413e7f486edcaa3 Mon Sep 17 00:00:00 2001 From: Andreas Grois Date: Sat, 22 Oct 2022 20:15:27 +0200 Subject: Pre-Allocate resulting password. It's not perfect, but a much better guess than previously. --- src/passwordmaker/mod.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/passwordmaker/mod.rs b/src/passwordmaker/mod.rs index a96698d..390836a 100644 --- a/src/passwordmaker/mod.rs +++ b/src/passwordmaker/mod.rs @@ -186,13 +186,18 @@ impl<'a> PasswordAssemblyParameters<'a> { } fn combine_prefix_password_suffix<'a, T : Iterator>>(password: T, assembly_settings : &PasswordAssemblyParameters<'a>) -> String { - Grapheme::iter_from_str(assembly_settings.prefix) + //Rust's collect only uses the lower hint for pre-allocation. UnicodeSegmentation is giving correct hints, + //meaning that the lower bound is 1 (or 0 for empty strings). + //We know however, that assembly_settings.password_length is a much better lower bound. Still too low for + //passwords that contain characters that take more than 1 byte though. Still, this value should reduce the number of needed re-allocations drastically. + let mut result = String::with_capacity(assembly_settings.password_length); + result.extend(Grapheme::iter_from_str(assembly_settings.prefix) .chain(password) .take(assembly_settings.password_length.saturating_sub(assembly_settings.suffix_length)) .chain(Grapheme::iter_from_str(assembly_settings.suffix)) .take(assembly_settings.password_length)//cut end if suffix_length is larger than password_length... - .map(|g| g.get()) - .collect() + .map(|g| g.get())); + result } enum GetGraphemesIteratorInner { -- cgit v1.2.3