From 175e92dde672a3ab712cf16c01e498dd44a02fb3 Mon Sep 17 00:00:00 2001 From: chasse20 Date: Mon, 23 Nov 2026 10:03:38 -0500 Subject: [PATCH] Check translation lookup behavior without timing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Account for 1c2f9175 by removing its standalone benchmark. Replace the lookup_is_fast wall-clock check (F2) with language-switch and missing-translation assertions in saved_language_codes_and_spanish_lookup, following PhotoCraft’s translation tests. Verified all seven i18n tests with coverage instrumentation and pinned catalogs, formatting, layering, and strict UI Clippy. --- crates/ui-egui/Cargo.toml | 5 --- crates/ui-egui/benches/translation_lookup.rs | 45 -------------------- crates/ui-egui/src/i18n.rs | 18 ++++---- 3 files changed, 10 insertions(+), 58 deletions(-) delete mode 100644 crates/ui-egui/benches/translation_lookup.rs diff --git a/crates/ui-egui/Cargo.toml b/crates/ui-egui/Cargo.toml index 8414598a..5b3ed2d3 100644 --- a/crates/ui-egui/Cargo.toml +++ b/crates/ui-egui/Cargo.toml @@ -8,11 +8,6 @@ license.workspace = true [lints] workspace = true -[[bench]] -name = "translation_lookup" -harness = false -test = false - [dependencies] chrono = { workspace = true } texturecraft-engine = { workspace = true } diff --git a/crates/ui-egui/benches/translation_lookup.rs b/crates/ui-egui/benches/translation_lookup.rs deleted file mode 100644 index c01f5875..00000000 --- a/crates/ui-egui/benches/translation_lookup.rs +++ /dev/null @@ -1,45 +0,0 @@ -//! Time warm lookups with the Japanese catalog embedded by the regular build. -//! With `CRAFT_I18N_DIR` pointing at the pinned catalog checkout, run -//! `cargo bench -p texturecraft-ui-egui --bench translation_lookup --profile dev`. - -use std::{hint::black_box, time::Instant}; - -use texturecraft_ui_egui::i18n::{set_language, tr}; - -include!(concat!(env!("OUT_DIR"), "/craft_i18n.rs")); - -fn main() -> Result<(), String> { - // Cargo can run this binary under test --all-targets too. Only bench passes --bench. - if !std::env::args().any(|arg| arg == "--bench") { - return Ok(()); - } - - let &(_, shared, app) = CRAFT_I18N.iter().find(|(code, _, _)| *code == "ja").ok_or("set CRAFT_I18N_DIR to the pinned craft-i18n checkout")?; - - // The plain rows with literal English keys are representative of the labels tr() looks up. - let mut keys: Vec<_> = shared - .lines() - .chain(app.lines()) - .filter_map(|row| row.strip_prefix('\t')?.split_once('\t').map(|(key, _)| key)) - .filter(|key| !key.is_empty() && !key.contains('\\')) - .collect(); - keys.sort_unstable(); - keys.dedup(); - if keys.is_empty() { - return Err("the Japanese catalog has no plain translation keys".into()); - } - - set_language("ja"); - black_box(tr(keys[0])); - const ROUNDS: usize = 100; - let started = Instant::now(); - for _ in 0..ROUNDS { - for key in &keys { - black_box(tr(black_box(key))); - } - } - let ns_per_lookup = started.elapsed().as_secs_f64() * 1e9 / (ROUNDS * keys.len()) as f64; - println!("{ns_per_lookup:.0} ns per lookup across {} Japanese keys", keys.len()); - assert!(ns_per_lookup < 1000.0, "{ns_per_lookup:.0} ns per lookup exceeded the 1000 ns budget"); - Ok(()) -} diff --git a/crates/ui-egui/src/i18n.rs b/crates/ui-egui/src/i18n.rs index 2073a222..235ebc5e 100644 --- a/crates/ui-egui/src/i18n.rs +++ b/crates/ui-egui/src/i18n.rs @@ -428,17 +428,19 @@ mod tests", set_language(language.code); assert_eq!(current().code, if available(language.code) { language.code } else { "en" }); } - let key = String::from("Layers"); + const MISSING: &str = "missing test label"; set_language("es"); - let label = tr(&key); + let spanish = tr("Layers"); + let missing = tr(MISSING); set_language("en"); - let english = tr(&key); + let english = tr("Layers"); set_language("es"); - let reused = tr(&key); + let spanish_again = tr("Layers"); set_language("en"); - assert_eq!(label, if available("es") { "Capas" } else { "Layers" }); - assert!(std::ptr::eq(english, key.as_str())); - // Switching away and back reuses the translation already stored in the catalog. - assert!(std::ptr::eq(reused, label)); + + assert_eq!(spanish, if available("es") { "Capas" } else { "Layers" }); + assert_eq!(missing, MISSING); + assert_eq!(english, "Layers"); + assert_eq!(spanish_again, spanish); } }