From 14b2ebb7c2dda180e026037395abbefe48109eb5 Mon Sep 17 00:00:00 2001 From: kiwi4boy Date: Sat, 14 Nov 2026 21:24:56 -0500 Subject: [PATCH] craft-rtf: one index lookup per insert, and no key String for a known author `Interned::insert` looked the key up and then inserted it, two probes for a new key. It now goes through `HashMap::entry`, one probe either way, and counts that probe itself, so the tests count what the index does. `author()` built a `String` key on every call. It now looks the author up borrowed first, and allocates only for a new one. Review on #1467. --- crates/rtf/src/doc/export.rs | 8 +++++++- crates/rtf/src/doc/tests.rs | 2 ++ crates/rtf/src/write.rs | 20 +++++++++++++------- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/crates/rtf/src/doc/export.rs b/crates/rtf/src/doc/export.rs index c99261718..daa3afbda 100644 --- a/crates/rtf/src/doc/export.rs +++ b/crates/rtf/src/doc/export.rs @@ -306,7 +306,13 @@ impl Writer { if a.is_empty() { return 0; } - authors.insert(a.to_string(), || a.to_string()) + 1 + // A borrowed lookup first: most calls name an author already in the table, and need no + // `String` for the key. + let i = match authors.get(a) { + Some(i) => i, + None => authors.insert(a.to_string(), || a.to_string()), + }; + i + 1 } /// Control words of a tracked insertion and deletion (`\revised`, `\deleted`). diff --git a/crates/rtf/src/doc/tests.rs b/crates/rtf/src/doc/tests.rs index 50d2d0d61..003b32872 100644 --- a/crates/rtf/src/doc/tests.rs +++ b/crates/rtf/src/doc/tests.rs @@ -1207,6 +1207,8 @@ fn many_distinct_fonts_colours_and_authors_take_constant_steps_each() { }; let mut out = String::new(); let steps = crate::write::lookup_steps(|| w.para(&p, false, None, "\\par", &mut out)); + // One lookup each for the font and the colour, and two for a new author: the borrowed + // lookup, then the insert. assert!(steps <= 4 * n, "{steps} steps for {n} runs"); assert!(out.contains(&format!("\\f{n}\\cf{n}")), "the last run names the last font and colour"); assert!(out.contains(&format!("\\crauth{n}")), "the last run names the last author"); diff --git a/crates/rtf/src/write.rs b/crates/rtf/src/write.rs index ce9b1610a..9bd6c7b0c 100644 --- a/crates/rtf/src/write.rs +++ b/crates/rtf/src/write.rs @@ -2,6 +2,7 @@ use std::borrow::Borrow; use std::collections::HashMap; +use std::collections::hash_map::Entry; use std::fmt::Write; use std::hash::Hash; @@ -90,15 +91,20 @@ impl Interned { self.index.get(key).copied() } - /// The place of the entry with `key`, adding `item()` at the end when there is none. + /// The place of the entry with `key`, adding `item()` at the end when there is none: one + /// lookup in the index either way. pub(crate) fn insert(&mut self, key: K, item: impl FnOnce() -> T) -> usize { - if let Some(i) = self.get(&key) { - return i; + #[cfg(test)] + STEPS.with(|s| s.set(s.get() + 1)); + match self.index.entry(key) { + Entry::Occupied(e) => *e.get(), + Entry::Vacant(e) => { + self.items.push(item()); + let i = self.items.len() - 1; + e.insert(i); + i + } } - self.items.push(item()); - let i = self.items.len() - 1; - self.index.insert(key, i); - i } }