config.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * Copyright (c) Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under both the BSD-style license (found in the
  6. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7. * in the COPYING file in the root directory of this source tree).
  8. * You may select, at your option, one of the above-listed licenses.
  9. */
  10. #include "config.h"
  11. /* Define a config for each fast level we want to test with. */
  12. #define FAST_LEVEL(x) \
  13. param_value_t const level_fast##x##_param_values[] = { \
  14. {.param = ZSTD_c_compressionLevel, .value = -x}, \
  15. }; \
  16. config_t const level_fast##x = { \
  17. .name = "level -" #x, \
  18. .cli_args = "--fast=" #x, \
  19. .param_values = PARAM_VALUES(level_fast##x##_param_values), \
  20. }; \
  21. config_t const level_fast##x##_dict = { \
  22. .name = "level -" #x " with dict", \
  23. .cli_args = "--fast=" #x, \
  24. .param_values = PARAM_VALUES(level_fast##x##_param_values), \
  25. .use_dictionary = 1, \
  26. };
  27. /* Define a config for each level we want to test with. */
  28. #define LEVEL(x) \
  29. param_value_t const level_##x##_param_values[] = { \
  30. {.param = ZSTD_c_compressionLevel, .value = x}, \
  31. }; \
  32. param_value_t const level_##x##_param_values_dms[] = { \
  33. {.param = ZSTD_c_compressionLevel, .value = x}, \
  34. {.param = ZSTD_c_enableDedicatedDictSearch, .value = 0}, \
  35. {.param = ZSTD_c_forceAttachDict, .value = ZSTD_dictForceAttach}, \
  36. }; \
  37. param_value_t const level_##x##_param_values_dds[] = { \
  38. {.param = ZSTD_c_compressionLevel, .value = x}, \
  39. {.param = ZSTD_c_enableDedicatedDictSearch, .value = 1}, \
  40. {.param = ZSTD_c_forceAttachDict, .value = ZSTD_dictForceAttach}, \
  41. }; \
  42. param_value_t const level_##x##_param_values_dictcopy[] = { \
  43. {.param = ZSTD_c_compressionLevel, .value = x}, \
  44. {.param = ZSTD_c_enableDedicatedDictSearch, .value = 0}, \
  45. {.param = ZSTD_c_forceAttachDict, .value = ZSTD_dictForceCopy}, \
  46. }; \
  47. param_value_t const level_##x##_param_values_dictload[] = { \
  48. {.param = ZSTD_c_compressionLevel, .value = x}, \
  49. {.param = ZSTD_c_enableDedicatedDictSearch, .value = 0}, \
  50. {.param = ZSTD_c_forceAttachDict, .value = ZSTD_dictForceLoad}, \
  51. }; \
  52. config_t const level_##x = { \
  53. .name = "level " #x, \
  54. .cli_args = "-" #x, \
  55. .param_values = PARAM_VALUES(level_##x##_param_values), \
  56. }; \
  57. config_t const level_##x##_dict = { \
  58. .name = "level " #x " with dict", \
  59. .cli_args = "-" #x, \
  60. .param_values = PARAM_VALUES(level_##x##_param_values), \
  61. .use_dictionary = 1, \
  62. }; \
  63. config_t const level_##x##_dict_dms = { \
  64. .name = "level " #x " with dict dms", \
  65. .cli_args = "-" #x, \
  66. .param_values = PARAM_VALUES(level_##x##_param_values_dms), \
  67. .use_dictionary = 1, \
  68. .advanced_api_only = 1, \
  69. }; \
  70. config_t const level_##x##_dict_dds = { \
  71. .name = "level " #x " with dict dds", \
  72. .cli_args = "-" #x, \
  73. .param_values = PARAM_VALUES(level_##x##_param_values_dds), \
  74. .use_dictionary = 1, \
  75. .advanced_api_only = 1, \
  76. }; \
  77. config_t const level_##x##_dict_copy = { \
  78. .name = "level " #x " with dict copy", \
  79. .cli_args = "-" #x, \
  80. .param_values = PARAM_VALUES(level_##x##_param_values_dictcopy), \
  81. .use_dictionary = 1, \
  82. .advanced_api_only = 1, \
  83. }; \
  84. config_t const level_##x##_dict_load = { \
  85. .name = "level " #x " with dict load", \
  86. .cli_args = "-" #x, \
  87. .param_values = PARAM_VALUES(level_##x##_param_values_dictload), \
  88. .use_dictionary = 1, \
  89. .advanced_api_only = 1, \
  90. };
  91. /* Define a config specifically to test row hash based levels and settings.
  92. */
  93. #define ROW_LEVEL(x, y) \
  94. param_value_t const row_##y##_level_##x##_param_values[] = { \
  95. {.param = ZSTD_c_useRowMatchFinder, .value = y}, \
  96. {.param = ZSTD_c_compressionLevel, .value = x}, \
  97. }; \
  98. param_value_t const row_##y##_level_##x##_param_values_dms[] = { \
  99. {.param = ZSTD_c_useRowMatchFinder, .value = y}, \
  100. {.param = ZSTD_c_compressionLevel, .value = x}, \
  101. {.param = ZSTD_c_enableDedicatedDictSearch, .value = 0}, \
  102. {.param = ZSTD_c_forceAttachDict, .value = ZSTD_dictForceAttach}, \
  103. }; \
  104. param_value_t const row_##y##_level_##x##_param_values_dds[] = { \
  105. {.param = ZSTD_c_useRowMatchFinder, .value = y}, \
  106. {.param = ZSTD_c_compressionLevel, .value = x}, \
  107. {.param = ZSTD_c_enableDedicatedDictSearch, .value = 1}, \
  108. {.param = ZSTD_c_forceAttachDict, .value = ZSTD_dictForceAttach}, \
  109. }; \
  110. param_value_t const row_##y##_level_##x##_param_values_dictcopy[] = { \
  111. {.param = ZSTD_c_useRowMatchFinder, .value = y}, \
  112. {.param = ZSTD_c_compressionLevel, .value = x}, \
  113. {.param = ZSTD_c_enableDedicatedDictSearch, .value = 0}, \
  114. {.param = ZSTD_c_forceAttachDict, .value = ZSTD_dictForceCopy}, \
  115. }; \
  116. param_value_t const row_##y##_level_##x##_param_values_dictload[] = { \
  117. {.param = ZSTD_c_useRowMatchFinder, .value = y}, \
  118. {.param = ZSTD_c_compressionLevel, .value = x}, \
  119. {.param = ZSTD_c_enableDedicatedDictSearch, .value = 0}, \
  120. {.param = ZSTD_c_forceAttachDict, .value = ZSTD_dictForceLoad}, \
  121. }; \
  122. config_t const row_##y##_level_##x = { \
  123. .name = "level " #x " row " #y, \
  124. .cli_args = "-" #x, \
  125. .param_values = PARAM_VALUES(row_##y##_level_##x##_param_values), \
  126. .advanced_api_only = 1, \
  127. }; \
  128. config_t const row_##y##_level_##x##_dict_dms = { \
  129. .name = "level " #x " row " #y " with dict dms", \
  130. .cli_args = "-" #x, \
  131. .param_values = PARAM_VALUES(row_##y##_level_##x##_param_values_dms), \
  132. .use_dictionary = 1, \
  133. .advanced_api_only = 1, \
  134. }; \
  135. config_t const row_##y##_level_##x##_dict_dds = { \
  136. .name = "level " #x " row " #y " with dict dds", \
  137. .cli_args = "-" #x, \
  138. .param_values = PARAM_VALUES(row_##y##_level_##x##_param_values_dds), \
  139. .use_dictionary = 1, \
  140. .advanced_api_only = 1, \
  141. }; \
  142. config_t const row_##y##_level_##x##_dict_copy = { \
  143. .name = "level " #x " row " #y" with dict copy", \
  144. .cli_args = "-" #x, \
  145. .param_values = PARAM_VALUES(row_##y##_level_##x##_param_values_dictcopy), \
  146. .use_dictionary = 1, \
  147. .advanced_api_only = 1, \
  148. }; \
  149. config_t const row_##y##_level_##x##_dict_load = { \
  150. .name = "level " #x " row " #y " with dict load", \
  151. .cli_args = "-" #x, \
  152. .param_values = PARAM_VALUES(row_##y##_level_##x##_param_values_dictload), \
  153. .use_dictionary = 1, \
  154. .advanced_api_only = 1, \
  155. };
  156. #define PARAM_VALUES(pv) \
  157. { .data = pv, .size = sizeof(pv) / sizeof((pv)[0]) }
  158. #include "levels.h"
  159. #undef LEVEL
  160. #undef FAST_LEVEL
  161. #undef ROW_LEVEL
  162. static config_t no_pledged_src_size = {
  163. .name = "no source size",
  164. .cli_args = "",
  165. .param_values = PARAM_VALUES(level_0_param_values),
  166. .no_pledged_src_size = 1,
  167. };
  168. static config_t no_pledged_src_size_with_dict = {
  169. .name = "no source size with dict",
  170. .cli_args = "",
  171. .param_values = PARAM_VALUES(level_0_param_values),
  172. .no_pledged_src_size = 1,
  173. .use_dictionary = 1,
  174. };
  175. static param_value_t const ldm_param_values[] = {
  176. {.param = ZSTD_c_enableLongDistanceMatching, .value = 1},
  177. };
  178. static config_t ldm = {
  179. .name = "long distance mode",
  180. .cli_args = "--long",
  181. .param_values = PARAM_VALUES(ldm_param_values),
  182. };
  183. static param_value_t const mt_param_values[] = {
  184. {.param = ZSTD_c_nbWorkers, .value = 2},
  185. };
  186. static config_t mt = {
  187. .name = "multithreaded",
  188. .cli_args = "-T2",
  189. .param_values = PARAM_VALUES(mt_param_values),
  190. };
  191. static param_value_t const mt_ldm_param_values[] = {
  192. {.param = ZSTD_c_nbWorkers, .value = 2},
  193. {.param = ZSTD_c_enableLongDistanceMatching, .value = 1},
  194. };
  195. static config_t mt_ldm = {
  196. .name = "multithreaded long distance mode",
  197. .cli_args = "-T2 --long",
  198. .param_values = PARAM_VALUES(mt_ldm_param_values),
  199. };
  200. static param_value_t mt_advanced_param_values[] = {
  201. {.param = ZSTD_c_nbWorkers, .value = 2},
  202. {.param = ZSTD_c_literalCompressionMode, .value = ZSTD_ps_disable},
  203. };
  204. static config_t mt_advanced = {
  205. .name = "multithreaded with advanced params",
  206. .cli_args = "-T2 --no-compress-literals",
  207. .param_values = PARAM_VALUES(mt_advanced_param_values),
  208. };
  209. static param_value_t const small_wlog_param_values[] = {
  210. {.param = ZSTD_c_windowLog, .value = 10},
  211. };
  212. static config_t small_wlog = {
  213. .name = "small window log",
  214. .cli_args = "--zstd=wlog=10",
  215. .param_values = PARAM_VALUES(small_wlog_param_values),
  216. };
  217. static param_value_t const small_hlog_param_values[] = {
  218. {.param = ZSTD_c_hashLog, .value = 6},
  219. {.param = ZSTD_c_strategy, .value = (int)ZSTD_btopt},
  220. };
  221. static config_t small_hlog = {
  222. .name = "small hash log",
  223. .cli_args = "--zstd=hlog=6,strat=7",
  224. .param_values = PARAM_VALUES(small_hlog_param_values),
  225. };
  226. static param_value_t const small_clog_param_values[] = {
  227. {.param = ZSTD_c_chainLog, .value = 6},
  228. {.param = ZSTD_c_strategy, .value = (int)ZSTD_btopt},
  229. };
  230. static config_t small_clog = {
  231. .name = "small chain log",
  232. .cli_args = "--zstd=clog=6,strat=7",
  233. .param_values = PARAM_VALUES(small_clog_param_values),
  234. };
  235. static param_value_t const uncompressed_literals_param_values[] = {
  236. {.param = ZSTD_c_compressionLevel, .value = 3},
  237. {.param = ZSTD_c_literalCompressionMode, .value = ZSTD_ps_disable},
  238. };
  239. static config_t uncompressed_literals = {
  240. .name = "uncompressed literals",
  241. .cli_args = "-3 --no-compress-literals",
  242. .param_values = PARAM_VALUES(uncompressed_literals_param_values),
  243. };
  244. static param_value_t const uncompressed_literals_opt_param_values[] = {
  245. {.param = ZSTD_c_compressionLevel, .value = 19},
  246. {.param = ZSTD_c_literalCompressionMode, .value = ZSTD_ps_disable},
  247. };
  248. static config_t uncompressed_literals_opt = {
  249. .name = "uncompressed literals optimal",
  250. .cli_args = "-19 --no-compress-literals",
  251. .param_values = PARAM_VALUES(uncompressed_literals_opt_param_values),
  252. };
  253. static param_value_t const huffman_literals_param_values[] = {
  254. {.param = ZSTD_c_compressionLevel, .value = -1},
  255. {.param = ZSTD_c_literalCompressionMode, .value = ZSTD_ps_enable},
  256. };
  257. static config_t huffman_literals = {
  258. .name = "huffman literals",
  259. .cli_args = "--fast=1 --compress-literals",
  260. .param_values = PARAM_VALUES(huffman_literals_param_values),
  261. };
  262. static param_value_t const explicit_params_param_values[] = {
  263. {.param = ZSTD_c_checksumFlag, .value = 1},
  264. {.param = ZSTD_c_contentSizeFlag, .value = 0},
  265. {.param = ZSTD_c_dictIDFlag, .value = 0},
  266. {.param = ZSTD_c_strategy, .value = (int)ZSTD_greedy},
  267. {.param = ZSTD_c_windowLog, .value = 18},
  268. {.param = ZSTD_c_hashLog, .value = 21},
  269. {.param = ZSTD_c_chainLog, .value = 21},
  270. {.param = ZSTD_c_targetLength, .value = 100},
  271. };
  272. static config_t explicit_params = {
  273. .name = "explicit params",
  274. .cli_args = "--no-check --no-dictID --zstd=strategy=3,wlog=18,hlog=21,clog=21,tlen=100",
  275. .param_values = PARAM_VALUES(explicit_params_param_values),
  276. };
  277. static config_t const* g_configs[] = {
  278. #define FAST_LEVEL(x) &level_fast##x, &level_fast##x##_dict,
  279. #define LEVEL(x) &level_##x, &level_##x##_dict, &level_##x##_dict_dms, &level_##x##_dict_dds, &level_##x##_dict_copy, &level_##x##_dict_load,
  280. #define ROW_LEVEL(x, y) &row_##y##_level_##x, &row_##y##_level_##x##_dict_dms, &row_##y##_level_##x##_dict_dds, &row_##y##_level_##x##_dict_copy, &row_##y##_level_##x##_dict_load,
  281. #include "levels.h"
  282. #undef ROW_LEVEL
  283. #undef LEVEL
  284. #undef FAST_LEVEL
  285. &no_pledged_src_size,
  286. &no_pledged_src_size_with_dict,
  287. &ldm,
  288. &mt,
  289. &mt_ldm,
  290. &small_wlog,
  291. &small_hlog,
  292. &small_clog,
  293. &explicit_params,
  294. &uncompressed_literals,
  295. &uncompressed_literals_opt,
  296. &huffman_literals,
  297. &mt_advanced,
  298. NULL,
  299. };
  300. config_t const* const* configs = g_configs;
  301. int config_skip_data(config_t const* config, data_t const* data) {
  302. return config->use_dictionary && !data_has_dict(data);
  303. }
  304. int config_get_level(config_t const* config)
  305. {
  306. param_values_t const params = config->param_values;
  307. size_t i;
  308. for (i = 0; i < params.size; ++i) {
  309. if (params.data[i].param == ZSTD_c_compressionLevel)
  310. return (int)params.data[i].value;
  311. }
  312. return CONFIG_NO_LEVEL;
  313. }
  314. ZSTD_parameters config_get_zstd_params(
  315. config_t const* config,
  316. uint64_t srcSize,
  317. size_t dictSize)
  318. {
  319. ZSTD_parameters zparams = {};
  320. param_values_t const params = config->param_values;
  321. int level = config_get_level(config);
  322. if (level == CONFIG_NO_LEVEL)
  323. level = 3;
  324. zparams = ZSTD_getParams(
  325. level,
  326. config->no_pledged_src_size ? ZSTD_CONTENTSIZE_UNKNOWN : srcSize,
  327. dictSize);
  328. for (size_t i = 0; i < params.size; ++i) {
  329. unsigned const value = params.data[i].value;
  330. switch (params.data[i].param) {
  331. case ZSTD_c_contentSizeFlag:
  332. zparams.fParams.contentSizeFlag = value;
  333. break;
  334. case ZSTD_c_checksumFlag:
  335. zparams.fParams.checksumFlag = value;
  336. break;
  337. case ZSTD_c_dictIDFlag:
  338. zparams.fParams.noDictIDFlag = !value;
  339. break;
  340. case ZSTD_c_windowLog:
  341. zparams.cParams.windowLog = value;
  342. break;
  343. case ZSTD_c_chainLog:
  344. zparams.cParams.chainLog = value;
  345. break;
  346. case ZSTD_c_hashLog:
  347. zparams.cParams.hashLog = value;
  348. break;
  349. case ZSTD_c_searchLog:
  350. zparams.cParams.searchLog = value;
  351. break;
  352. case ZSTD_c_minMatch:
  353. zparams.cParams.minMatch = value;
  354. break;
  355. case ZSTD_c_targetLength:
  356. zparams.cParams.targetLength = value;
  357. break;
  358. case ZSTD_c_strategy:
  359. zparams.cParams.strategy = (ZSTD_strategy)value;
  360. break;
  361. default:
  362. break;
  363. }
  364. }
  365. return zparams;
  366. }