openhuman
openhuman copied to clipboard
Perf: prompt_injection detector allocates Box<dyn Classifier> on every call
Summary
src/openhuman/prompt_injection/detector.rs — optional_classifier() reads OPENHUMAN_PROMPT_INJECTION_CLASSIFIER env var and allocates a new Box<dyn OptionalClassifier> on every invocation of analyze_prompt().
Impact
Low — Unnecessary env var read + heap allocation per prompt analysis. Wastes cycles on a hot path.
Suggested Fix
Cache the classifier choice in a OnceLock<Option<Box<dyn OptionalClassifier>>>:
static CLASSIFIER: OnceLock<Option<Box<dyn OptionalClassifier>>> = OnceLock::new();
fn optional_classifier() -> &'static Option<Box<dyn OptionalClassifier>> {
CLASSIFIER.get_or_init(|| { /* read env, construct once */ })
}