openhuman icon indicating copy to clipboard operation
openhuman copied to clipboard

Perf: prompt_injection detector allocates Box<dyn Classifier> on every call

Open Liohtml opened this issue 6 months ago • 0 comments

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 */ })
}

Liohtml avatar May 16 '26 14:05 Liohtml