1010#define IS_NOTICE_compress_canceled 0
1111#include " ../lzma/C/Lzma2Dec.h"
1212#include " ../lzma/C/Lzma2Enc.h"
13- #include " ../lzma/C/MtCoder.h"
13+ // The public API deliberately caps LZMA's internal match-finder parallelism
14+ // at two workers, so the C++ wrapper only needs the compressor's bound here.
15+ // MtCoder itself is compiled as C from binding.gyp.
16+ #define MTCODER_THREADS_MAX 2
1417#include " ../HDiffPatch/compress_plugin_demo.h"
1518#include " ../HDiffPatch/decompress_plugin_demo.h"
1619
1720namespace {
1821 // 与 v1.0.6 起的历史产物保持一致的压缩参数(patch 兼容性由 single
1922 // 格式规范保证,这里的一致性只为产物尺寸/确定性稳定)
20- void configure_lzma2 (TCompressPlugin_lzma2& compressPlugin) {
23+ void configure_lzma2 (TCompressPlugin_lzma2& compressPlugin,
24+ size_t compressionThreads) {
25+ if (compressionThreads < 1 || compressionThreads > 2 ) {
26+ throw std::runtime_error (" compressionThreads must be 1 or 2." );
27+ }
2128 const size_t myBestDictSize = (1 << 20 ) * 8 ; // 固定 8MB
2229 compressPlugin = lzma2CompressPlugin;
2330 compressPlugin.compress_level = 9 ;
2431 compressPlugin.dict_size = myBestDictSize;
25- compressPlugin.thread_num = 1 ;
32+ // 2 线程只启用 LZMA 内部并行匹配,不切 LZMA2 block,避免额外的
33+ // block 级内存放大。压缩级别和字典保持历史值不变。
34+ compressPlugin.thread_num = static_cast <int >(compressionThreads);
2635 }
2736
2837 // 升级到 HDiffPatch v5 前的历史参数:匹配分 3、步进内存 256KB。
@@ -255,10 +264,10 @@ namespace {
255264}
256265
257266void hdiff (const uint8_t * old, size_t oldsize, const uint8_t * _new, size_t newsize,
258- std::vector<uint8_t >& out_codeBuf) {
267+ std::vector<uint8_t >& out_codeBuf, size_t compressionThreads ) {
259268 hpatch_TDecompress* decompressPlugin = &lzma2DecompressPlugin;
260269 TCompressPlugin_lzma2 compressPlugin;
261- configure_lzma2 (compressPlugin);
270+ configure_lzma2 (compressPlugin, compressionThreads );
262271
263272 create_single_compressed_diff (_new, _new + newsize, old, old + oldsize, out_codeBuf,
264273 &compressPlugin.base , kPatchStepMemSize ,
@@ -273,14 +282,15 @@ void hdiff(const uint8_t* old, size_t oldsize, const uint8_t* _new, size_t newsi
273282 }
274283}
275284
276- void hdiff_stream (const char * oldPath,const char * newPath,const char * outDiffPath){
285+ void hdiff_stream (const char * oldPath,const char * newPath,const char * outDiffPath,
286+ size_t compressionThreads){
277287 if (!oldPath || !newPath || !outDiffPath) {
278288 throw std::runtime_error (" Invalid file path." );
279289 }
280290
281291 hpatch_TDecompress* decompressPlugin = &lzma2DecompressPlugin;
282292 TCompressPlugin_lzma2 compressPlugin;
283- configure_lzma2 (compressPlugin);
293+ configure_lzma2 (compressPlugin, compressionThreads );
284294
285295 FileStreamGuard streams;
286296 streams.openInputs (oldPath, newPath);
@@ -300,14 +310,14 @@ void hdiff_stream(const char* oldPath,const char* newPath,const char* outDiffPat
300310}
301311
302312void hdiff_window (const char * oldPath,const char * newPath,const char * outDiffPath,
303- size_t windowSize){
313+ size_t windowSize, size_t compressionThreads ){
304314 if (!oldPath || !newPath || !outDiffPath) {
305315 throw std::runtime_error (" Invalid file path." );
306316 }
307317
308318 hpatch_TDecompress* decompressPlugin = &lzma2DecompressPlugin;
309319 TCompressPlugin_lzma2 compressPlugin;
310- configure_lzma2 (compressPlugin);
320+ configure_lzma2 (compressPlugin, compressionThreads );
311321
312322 FileStreamGuard streams;
313323 streams.openInputs (oldPath, newPath);
@@ -336,14 +346,15 @@ void hdiff_window(const char* oldPath,const char* newPath,const char* outDiffPat
336346 streams.closeAllOrThrow ();
337347}
338348
339- void hdiff_single_stream (const char * oldPath,const char * newPath,const char * outDiffPath){
349+ void hdiff_single_stream (const char * oldPath,const char * newPath,const char * outDiffPath,
350+ size_t compressionThreads){
340351 if (!oldPath || !newPath || !outDiffPath) {
341352 throw std::runtime_error (" Invalid file path." );
342353 }
343354
344355 hpatch_TDecompress* decompressPlugin = &lzma2DecompressPlugin;
345356 TCompressPlugin_lzma2 compressPlugin;
346- configure_lzma2 (compressPlugin);
357+ configure_lzma2 (compressPlugin, compressionThreads );
347358
348359 FileStreamGuard streams;
349360 streams.openInputs (oldPath, newPath);
0 commit comments