**Optimize ProQue initialization with OnceLock**

Replaced repeated ProQue initialization with a static OnceLock to ensure efficient and thread-safe reuse. This minimizes the overhead of recreating the ProQue instance while maintaining correct behavior by dynamically updating its dimensions.
This commit is contained in:
2025-04-09 14:39:14 +02:00
parent 47bbf25ac7
commit 1d3d5b3e6e

View File

@@ -1,3 +1,4 @@
use std::sync::OnceLock;
use ocl::{Buffer, MemFlags, ProQue, Platform}; use ocl::{Buffer, MemFlags, ProQue, Platform};
use ocl::SpatialDims::Three; use ocl::SpatialDims::Three;
use schemsearch_common::{Match, SearchBehavior}; use schemsearch_common::{Match, SearchBehavior};
@@ -5,6 +6,8 @@ use math::round::ceil;
const KERNEL: &str = include_str!("kernel.cl"); const KERNEL: &str = include_str!("kernel.cl");
static PRO_QUEU_CELL: OnceLock<ProQue> = OnceLock::new();
pub fn ocl_available() -> bool { pub fn ocl_available() -> bool {
!Platform::list().is_empty() !Platform::list().is_empty()
} }
@@ -40,10 +43,15 @@ fn search_ocl(
let skip_amount = ceil((pattern_blocks * (1.0 - search_behavior.threshold)) as f64, 0) as i32; let skip_amount = ceil((pattern_blocks * (1.0 - search_behavior.threshold)) as f64, 0) as i32;
let pro_que = ProQue::builder() let cell = &PRO_QUEU_CELL;
let mut pro_que = cell.get_or_init(|| {
ProQue::builder()
.src(KERNEL) .src(KERNEL)
.dims(Three(schem_width, schem_height, schem_length)) .build().unwrap()
.build()?; }).clone();
pro_que.set_dims(Three(schem_width, schem_height, schem_length));
let buffer = Buffer::builder() let buffer = Buffer::builder()
.queue(pro_que.queue().clone()) .queue(pro_que.queue().clone())