From 1d3d5b3e6e16ea3f47e8a633e6170525764bdcd1 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Wed, 9 Apr 2025 14:39:14 +0200 Subject: [PATCH] **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. --- schemsearch-ocl-matcher/src/lib.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/schemsearch-ocl-matcher/src/lib.rs b/schemsearch-ocl-matcher/src/lib.rs index 4c860a6..967c858 100644 --- a/schemsearch-ocl-matcher/src/lib.rs +++ b/schemsearch-ocl-matcher/src/lib.rs @@ -1,3 +1,4 @@ +use std::sync::OnceLock; use ocl::{Buffer, MemFlags, ProQue, Platform}; use ocl::SpatialDims::Three; use schemsearch_common::{Match, SearchBehavior}; @@ -5,6 +6,8 @@ use math::round::ceil; const KERNEL: &str = include_str!("kernel.cl"); +static PRO_QUEU_CELL: OnceLock = OnceLock::new(); + pub fn ocl_available() -> bool { !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 pro_que = ProQue::builder() - .src(KERNEL) - .dims(Three(schem_width, schem_height, schem_length)) - .build()?; + let cell = &PRO_QUEU_CELL; + let mut pro_que = cell.get_or_init(|| { + ProQue::builder() + .src(KERNEL) + .build().unwrap() + }).clone(); + + + pro_que.set_dims(Three(schem_width, schem_height, schem_length)); let buffer = Buffer::builder() .queue(pro_que.queue().clone())