Skip to content

Commit 41f63c5

Browse files
committed
fn COVER_best_init: refactor
1 parent e1a78f5 commit 41f63c5

File tree

2 files changed

+31
-43
lines changed

2 files changed

+31
-43
lines changed

lib/dictBuilder/cover.rs

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,20 @@ pub(super) struct COVER_best_t {
104104
pub(super) compressedSize: size_t,
105105
}
106106

107+
impl COVER_best_t {
108+
pub(crate) fn new() -> Self {
109+
Self {
110+
mutex: Mutex::new(()),
111+
cond: Condvar::new(),
112+
liveJobs: 0,
113+
dict: Box::default(),
114+
dictSize: 0,
115+
compressedSize: -(1 as core::ffi::c_int) as size_t,
116+
parameters: ZDICT_cover_params_t::default(),
117+
}
118+
}
119+
}
120+
107121
#[repr(C)]
108122
struct COVER_tryParameters_data_t<'a, 'b> {
109123
ctx: &'b COVER_ctx_t<'a>,
@@ -942,14 +956,6 @@ pub(super) fn COVER_checkTotalCompressedSize(
942956
totalCompressedSize
943957
}
944958

945-
pub(super) fn COVER_best_init(best: &mut COVER_best_t) {
946-
best.liveJobs = 0;
947-
best.dict = Box::default();
948-
best.dictSize = 0;
949-
best.compressedSize = -(1 as core::ffi::c_int) as size_t;
950-
best.parameters = ZDICT_cover_params_t::default();
951-
}
952-
953959
pub(super) fn COVER_best_wait(best: &mut COVER_best_t) {
954960
let mut guard = best.mutex.lock().unwrap();
955961
#[expect(clippy::while_immutable_condition)]
@@ -1232,15 +1238,6 @@ pub unsafe extern "C" fn ZDICT_optimizeTrainFromBuffer_cover(
12321238
let shrinkDict = 0 as core::ffi::c_uint;
12331239
let displayLevel = (*parameters).zParams.notificationLevel as core::ffi::c_int;
12341240
let mut iteration = 1 as core::ffi::c_uint;
1235-
let mut best = COVER_best_t {
1236-
mutex: Mutex::new(()),
1237-
cond: Condvar::new(),
1238-
liveJobs: 0,
1239-
dict: Box::default(),
1240-
dictSize: 0,
1241-
parameters: ZDICT_cover_params_t::default(),
1242-
compressedSize: 0,
1243-
};
12441241
let mut pool = core::ptr::null_mut();
12451242
let mut warned = 0;
12461243
let mut lastUpdateTime = 0;
@@ -1274,11 +1271,24 @@ pub unsafe extern "C" fn ZDICT_optimizeTrainFromBuffer_cover(
12741271
return Error::memory_allocation.to_error_code();
12751272
}
12761273
}
1277-
COVER_best_init(&mut best);
12781274
if displayLevel >= 2 {
12791275
eprintln!("Trying {} different sets of parameters", kIterations);
12801276
}
12811277

1278+
let samplesSizes = if samplesSizes.is_null() || nbSamples == 0 {
1279+
&[]
1280+
} else {
1281+
core::slice::from_raw_parts(samplesSizes, nbSamples as usize)
1282+
};
1283+
let totalSamplesSize = samplesSizes.iter().sum::<usize>();
1284+
let samples = if samplesBuffer.is_null() || totalSamplesSize == 0 {
1285+
&[]
1286+
} else {
1287+
core::slice::from_raw_parts(samplesBuffer.cast::<u8>(), totalSamplesSize)
1288+
};
1289+
1290+
let mut best = COVER_best_t::new();
1291+
12821292
for d in (kMinD..=kMaxD).step_by(2) {
12831293
let mut ctx = COVER_ctx_t::default();
12841294
if displayLevel >= 3 {
@@ -1290,18 +1300,6 @@ pub unsafe extern "C" fn ZDICT_optimizeTrainFromBuffer_cover(
12901300
displayLevel - 1
12911301
};
12921302

1293-
let samplesSizes = if samplesSizes.is_null() || nbSamples == 0 {
1294-
&[]
1295-
} else {
1296-
core::slice::from_raw_parts(samplesSizes, nbSamples as usize)
1297-
};
1298-
let totalSamplesSize = samplesSizes.iter().sum::<usize>();
1299-
let samples = if samplesBuffer.is_null() || totalSamplesSize == 0 {
1300-
&[]
1301-
} else {
1302-
core::slice::from_raw_parts(samplesBuffer.cast::<u8>(), totalSamplesSize)
1303-
};
1304-
13051303
let initVal = COVER_ctx_init(
13061304
&mut ctx,
13071305
samples,

lib/dictBuilder/fastcover.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use core::ptr;
2-
use std::sync::{Condvar, Mutex};
32

43
use libc::{free, malloc, memcpy, size_t};
54

@@ -9,8 +8,8 @@ use crate::lib::compress::zstd_compress_internal::{
98
ZSTD_hash6Ptr, ZSTD_hash6Ptr_array, ZSTD_hash8Ptr, ZSTD_hash8Ptr_array,
109
};
1110
use crate::lib::dictBuilder::cover::{
12-
COVER_best_destroy, COVER_best_finish, COVER_best_init, COVER_best_start, COVER_best_t,
13-
COVER_best_wait, COVER_computeEpochs, COVER_dictSelectionError, COVER_dictSelectionFree,
11+
COVER_best_destroy, COVER_best_finish, COVER_best_start, COVER_best_t, COVER_best_wait,
12+
COVER_computeEpochs, COVER_dictSelectionError, COVER_dictSelectionFree,
1413
COVER_dictSelectionIsError, COVER_segment_t, COVER_selectDict, COVER_warnOnSmallCorpus,
1514
};
1615
use crate::lib::zdict::experimental::{
@@ -697,15 +696,6 @@ pub unsafe extern "C" fn ZDICT_optimizeTrainFromBuffer_fastCover(
697696
let mut iteration = 1 as core::ffi::c_uint;
698697
let mut d: core::ffi::c_uint = 0;
699698
let mut k: core::ffi::c_uint = 0;
700-
let mut best = COVER_best_t {
701-
mutex: Mutex::new(()),
702-
cond: Condvar::new(),
703-
liveJobs: 0,
704-
dict: Box::default(),
705-
dictSize: 0,
706-
parameters: ZDICT_cover_params_t::default(),
707-
compressedSize: 0,
708-
};
709699
let mut pool = core::ptr::null_mut();
710700
let mut warned = 0;
711701
let mut lastUpdateTime = 0;
@@ -745,7 +735,7 @@ pub unsafe extern "C" fn ZDICT_optimizeTrainFromBuffer_fastCover(
745735
return Error::memory_allocation.to_error_code();
746736
}
747737
}
748-
COVER_best_init(&mut best);
738+
let mut best = COVER_best_t::new();
749739
ptr::write_bytes(
750740
&mut coverParams as *mut ZDICT_cover_params_t as *mut u8,
751741
0,

0 commit comments

Comments
 (0)