Skip to content

Commit 468e25d

Browse files
committed
move fields into inner struct
1 parent 0a6417e commit 468e25d

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

lib/dictBuilder/cover.rs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,28 @@ pub(super) struct COVER_epoch_info_t {
9494
pub(super) size: u32,
9595
}
9696

97-
pub(super) struct COVER_best_t_inner {}
97+
pub(super) struct COVER_best_t_inner {
98+
pub(super) dict: Box<[u8]>,
99+
pub(super) dictSize: size_t,
100+
pub(super) parameters: ZDICT_cover_params_t,
101+
pub(super) compressedSize: size_t,
102+
}
98103

99104
impl COVER_best_t_inner {
100105
pub(crate) fn new() -> Self {
101-
Self {}
106+
Self {
107+
dict: Box::default(),
108+
dictSize: 0,
109+
compressedSize: -(1 as core::ffi::c_int) as size_t,
110+
parameters: ZDICT_cover_params_t::default(),
111+
}
102112
}
103113
}
104114

105115
pub(super) struct COVER_best_t {
106116
pub(super) mutex: Mutex<COVER_best_t_inner>,
107117
pub(super) cond: Condvar,
108118
pub(super) liveJobs: size_t,
109-
pub(super) dict: Box<[u8]>,
110-
pub(super) dictSize: size_t,
111-
pub(super) parameters: ZDICT_cover_params_t,
112-
pub(super) compressedSize: size_t,
113119
}
114120

115121
impl COVER_best_t {
@@ -118,10 +124,6 @@ impl COVER_best_t {
118124
mutex: Mutex::new(COVER_best_t_inner::new()),
119125
cond: Condvar::new(),
120126
liveJobs: 0,
121-
dict: Box::default(),
122-
dictSize: 0,
123-
compressedSize: -(1 as core::ffi::c_int) as size_t,
124-
parameters: ZDICT_cover_params_t::default(),
125127
}
126128
}
127129
}
@@ -964,12 +966,15 @@ pub(super) fn COVER_checkTotalCompressedSize(
964966
totalCompressedSize
965967
}
966968

967-
pub(super) fn COVER_best_wait(best: &mut COVER_best_t) {
969+
pub(super) fn COVER_best_wait(
970+
best: &mut COVER_best_t,
971+
) -> std::sync::MutexGuard<'_, COVER_best_t_inner> {
968972
let mut guard = best.mutex.lock().unwrap();
969973
#[expect(clippy::while_immutable_condition)]
970974
while best.liveJobs != 0 {
971975
guard = best.cond.wait(guard).unwrap();
972976
}
977+
guard
973978
}
974979

975980
pub(super) fn COVER_best_start(best: &mut COVER_best_t) {
@@ -985,19 +990,19 @@ pub(super) fn COVER_best_finish(
985990
let compressedSize = selection.totalCompressedSize;
986991
let dictSize = selection.dictSize;
987992
let mut liveJobs: size_t = 0;
988-
let _guard = best.mutex.lock().unwrap();
993+
let mut guard = best.mutex.lock().unwrap();
989994
best.liveJobs = (best.liveJobs).wrapping_sub(1);
990995
liveJobs = best.liveJobs;
991-
if compressedSize < best.compressedSize {
992-
if let Some(slice) = best.dict.get_mut(..selection.dictContent.len()) {
996+
if compressedSize < guard.compressedSize {
997+
if let Some(slice) = guard.dict.get_mut(..selection.dictContent.len()) {
993998
slice.copy_from_slice(&selection.dictContent);
994999
} else {
995-
best.dict = selection.dictContent.clone();
1000+
guard.dict = selection.dictContent.clone();
9961001
}
9971002

998-
best.dictSize = dictSize;
999-
best.parameters = parameters;
1000-
best.compressedSize = compressedSize;
1003+
guard.dictSize = dictSize;
1004+
guard.parameters = parameters;
1005+
guard.compressedSize = compressedSize;
10011006
}
10021007
if liveJobs == 0 {
10031008
best.cond.notify_all();
@@ -1316,7 +1321,7 @@ pub unsafe extern "C" fn ZDICT_optimizeTrainFromBuffer_cover(
13161321
if displayLevel >= 1 {
13171322
eprintln!("Failed to initialize context");
13181323
}
1319-
COVER_best_wait(&mut best);
1324+
drop(COVER_best_wait(&mut best));
13201325
POOL_free(pool);
13211326
return initVal;
13221327
}
@@ -1381,15 +1386,15 @@ pub unsafe extern "C" fn ZDICT_optimizeTrainFromBuffer_cover(
13811386
}
13821387
}
13831388

1384-
COVER_best_wait(&mut best);
1389+
drop(COVER_best_wait(&mut best));
13851390
COVER_ctx_destroy(&mut ctx);
13861391
}
13871392

13881393
if displayLevel >= 2 {
13891394
println!("\r{:79 }\r", "");
13901395
}
13911396

1392-
COVER_best_wait(&mut best);
1397+
let best = COVER_best_wait(&mut best);
13931398

13941399
let dictSize = best.dictSize;
13951400
if ERR_isError(best.compressedSize) {

lib/dictBuilder/fastcover.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ pub unsafe extern "C" fn ZDICT_optimizeTrainFromBuffer_fastCover(
787787
if displayLevel >= 1 {
788788
eprintln!("Failed to initialize context");
789789
}
790-
COVER_best_wait(&mut best);
790+
drop(COVER_best_wait(&mut best));
791791
POOL_free(pool);
792792
return initVal;
793793
}
@@ -806,7 +806,7 @@ pub unsafe extern "C" fn ZDICT_optimizeTrainFromBuffer_fastCover(
806806
if displayLevel >= 1 {
807807
eprintln!("Failed to allocate parameters");
808808
}
809-
COVER_best_wait(&mut best);
809+
drop(COVER_best_wait(&mut best));
810810
FASTCOVER_ctx_destroy(&mut ctx);
811811
POOL_free(pool);
812812
return Error::memory_allocation.to_error_code();
@@ -856,15 +856,15 @@ pub unsafe extern "C" fn ZDICT_optimizeTrainFromBuffer_fastCover(
856856
}
857857
k = k.wrapping_add(kStepSize);
858858
}
859-
COVER_best_wait(&mut best);
859+
drop(COVER_best_wait(&mut best));
860860
FASTCOVER_ctx_destroy(&mut ctx);
861861
d = d.wrapping_add(2);
862862
}
863863
if displayLevel >= 2 {
864864
println!("\r{:79 }\r", "");
865865
}
866866

867-
COVER_best_wait(&mut best);
867+
let best = COVER_best_wait(&mut best);
868868

869869
let dictSize = best.dictSize;
870870
if ERR_isError(best.compressedSize) {

0 commit comments

Comments
 (0)