@@ -843,6 +843,11 @@ int fill_codec(blosc2_codec *codec) {
843843 dlclose (lib );
844844 return BLOSC2_ERROR_FAILURE ;
845845 }
846+ if (codec -> compcode == BLOSC_CODEC_GROK ) {
847+ // Initialize grok lib
848+ void (* init_func )(uint32_t , bool ) = dlsym (lib , "blosc2_grok_init" );
849+ (* init_func )(0 , false);
850+ }
846851
847852 return BLOSC2_ERROR_SUCCESS ;
848853}
@@ -1750,8 +1755,7 @@ static int blosc_d(
17501755 }
17511756
17521757 /* The number of compressed data streams for this block */
1753- if (!dont_split && !leftoverblock && !context -> use_dict ) {
1754- // We don't want to split when in a training dict state
1758+ if (!dont_split && !leftoverblock ) {
17551759 nstreams = (int32_t )typesize ;
17561760 }
17571761 else {
@@ -2599,8 +2603,20 @@ int blosc2_compress_ctx(blosc2_context* context, const void* src, int32_t srcsiz
25992603 dict_maxsize = srcsize / 20 ;
26002604 }
26012605 void * samples_buffer = context -> dest + context -> header_overhead ;
2602- unsigned nblocks = 8 ; // the minimum that accepts zstd as of 1.4.0
2603- unsigned sample_fraction = 1 ; // 1 allows to use most of the chunk for training
2606+ unsigned nblocks = (unsigned )context -> nblocks ;
2607+ int dont_split = (context -> header_flags & 0x10 ) >> 4 ;
2608+ if (!dont_split ) {
2609+ nblocks = nblocks * context -> typesize ;
2610+ }
2611+ if (nblocks < 8 ) {
2612+ nblocks = 8 ; // the minimum that accepts zstd as of 1.4.0
2613+ }
2614+
2615+ // 1 allows to use most of the chunk for training, but it is slower,
2616+ // and it does not always seem to improve compression ratio.
2617+ // Let's use 16, which is faster and still gives good results
2618+ // on test_dict_schunk.c, but this seems very dependent on the data.
2619+ unsigned sample_fraction = 16 ;
26042620 size_t sample_size = context -> sourcesize / nblocks / sample_fraction ;
26052621
26062622 // Populate the samples sizes for training the dictionary
@@ -2613,7 +2629,9 @@ int blosc2_compress_ctx(blosc2_context* context, const void* src, int32_t srcsiz
26132629 // Train from samples
26142630 void * dict_buffer = malloc (dict_maxsize );
26152631 BLOSC_ERROR_NULL (dict_buffer , BLOSC2_ERROR_MEMORY_ALLOC );
2616- int32_t dict_actual_size = (int32_t )ZDICT_trainFromBuffer (dict_buffer , dict_maxsize , samples_buffer , samples_sizes , nblocks );
2632+ int32_t dict_actual_size = (int32_t )ZDICT_trainFromBuffer (
2633+ dict_buffer , dict_maxsize ,
2634+ samples_buffer , samples_sizes , nblocks );
26172635
26182636 // TODO: experiment with parameters of low-level fast cover algorithm
26192637 // Note that this API is still unstable. See: https://github.com/facebook/zstd/issues/1599
@@ -2622,7 +2640,9 @@ int blosc2_compress_ctx(blosc2_context* context, const void* src, int32_t srcsiz
26222640 // fast_cover_params.d = nblocks;
26232641 // fast_cover_params.steps = 4;
26242642 // fast_cover_params.zParams.compressionLevel = context->clevel;
2625- //size_t dict_actual_size = ZDICT_optimizeTrainFromBuffer_fastCover(dict_buffer, dict_maxsize, samples_buffer, samples_sizes, nblocks, &fast_cover_params);
2643+ // size_t dict_actual_size = ZDICT_optimizeTrainFromBuffer_fastCover(
2644+ // dict_buffer, dict_maxsize, samples_buffer, samples_sizes, nblocks,
2645+ // &fast_cover_params);
26262646
26272647 if (ZDICT_isError (dict_actual_size ) != ZSTD_error_no_error ) {
26282648 BLOSC_TRACE_ERROR ("Error in ZDICT_trainFromBuffer(): '%s'."
0 commit comments