From 5e83f743d7ceb8f41fbab3e48abc0e3376eb0d63 Mon Sep 17 00:00:00 2001 From: onurogut Date: Sun, 15 Feb 2026 03:51:02 +0300 Subject: [PATCH] Avoid intermediate allocations in Trie::to_strings --- crates/libtiny_tui/src/trie.rs | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/crates/libtiny_tui/src/trie.rs b/crates/libtiny_tui/src/trie.rs index 5a24a49b..0c99b6f2 100644 --- a/crates/libtiny_tui/src/trie.rs +++ b/crates/libtiny_tui/src/trie.rs @@ -61,26 +61,24 @@ impl Trie { } } - // TODO: We need an Iterator instance instead. pub fn to_strings(&self, prefix: &str) -> Vec { - let mut ret = { - if self.word { - vec![prefix.to_owned()] - } else { - vec![] - } - }; + let mut ret = Vec::new(); + let mut buf = String::from(prefix); + self.to_strings_(&mut buf, &mut ret); + ret + } + fn to_strings_(&self, buf: &mut String, ret: &mut Vec) { + if self.word { + ret.push(buf.clone()); + } for &(c, ref t) in &self.vec { - let mut prefix_ = prefix.to_owned(); - prefix_.push(c); - ret.extend(t.to_strings(&prefix_)); + buf.push(c); + t.to_strings_(buf, ret); + buf.pop(); } - - ret } - // TODO: We need an Iterator instance instead. pub fn drop_pfx(&self, prefix: &mut dyn Iterator) -> Vec { let mut trie = self; for char in prefix {