refactor: use OnceLock for log dedup statics in processor.rs

This commit is contained in:
2026-03-31 23:41:10 +08:00
parent 98d827f5c3
commit 8d1ebf02e3

View File

@@ -13,28 +13,26 @@ use opencv::{
}; };
use rand::Rng; use rand::Rng;
use std::collections::HashSet; use std::collections::HashSet;
use std::sync::{Arc, Mutex, MutexGuard}; use std::sync::{Arc, Mutex, MutexGuard, OnceLock};
use std::time::Instant; use std::time::Instant;
type WindowRectKey = (String, i32, i32, i32, i32); type WindowRectKey = (String, i32, i32, i32, i32);
type LoggedSizes = ((i32, i32), (i32, i32), (i32, i32)); type LoggedSizes = ((i32, i32), (i32, i32), (i32, i32));
fn log_mat_size(label: &str, frame: &Mat) { fn log_mat_size(label: &str, frame: &Mat) {
use std::sync::Mutex; static SEEN: OnceLock<Mutex<HashSet<(String, i32, i32)>>> = OnceLock::new();
static SEEN: Mutex<Option<HashSet<(String, i32, i32)>>> = Mutex::new(None);
let key = (label.to_string(), frame.cols(), frame.rows()); let key = (label.to_string(), frame.cols(), frame.rows());
let mut guard = SEEN.lock().unwrap(); let mut guard = SEEN
let set = guard.get_or_insert_with(HashSet::new); .get_or_init(|| Mutex::new(HashSet::new()))
if set.insert(key) { .lock()
.unwrap();
if guard.insert(key) {
println!("{}: cols={} rows={}", label, frame.cols(), frame.rows()); println!("{}: cols={} rows={}", label, frame.cols(), frame.rows());
} }
} }
fn log_window_image_rect(window_name: &str) { fn log_window_image_rect(window_name: &str) {
use std::sync::Mutex; static SEEN: OnceLock<Mutex<HashSet<WindowRectKey>>> = OnceLock::new();
static SEEN: Mutex<Option<HashSet<WindowRectKey>>> = Mutex::new(None);
match highgui::get_window_image_rect(window_name) { match highgui::get_window_image_rect(window_name) {
Ok(rect) => { Ok(rect) => {
let key = ( let key = (
@@ -44,9 +42,11 @@ fn log_window_image_rect(window_name: &str) {
rect.width, rect.width,
rect.height, rect.height,
); );
let mut guard = SEEN.lock().unwrap(); let mut guard = SEEN
let set = guard.get_or_insert_with(HashSet::new); .get_or_init(|| Mutex::new(HashSet::new()))
if set.insert(key) { .lock()
.unwrap();
if guard.insert(key) {
println!( println!(
"window.image_rect {}: x={} y={} width={} height={}", "window.image_rect {}: x={} y={} width={} height={}",
window_name, rect.x, rect.y, rect.width, rect.height, window_name, rect.x, rect.y, rect.width, rect.height,