Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions build/cmake/CMakeModules/ZstdOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,10 @@ endif()

# Set global definitions
add_definitions(-DXXH_NAMESPACE=ZSTD_)

# Selective lazy evaluation optimization
option(ZSTD_SELECTIVE_LAZY "Enable selective lazy evaluation optimization" OFF)
if(ZSTD_SELECTIVE_LAZY)
message(STATUS "ZSTD_SELECTIVE_LAZY enabled")
add_definitions(-DZSTD_SELECTIVE_LAZY)
endif()
5 changes: 5 additions & 0 deletions build/meson/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ use_lz4 = lz4_dep.found()

add_project_arguments('-DXXH_NAMESPACE=ZSTD_', language: ['c'])

use_selective_lazy = get_option('selective_lazy')
if use_selective_lazy
add_project_arguments('-DZSTD_SELECTIVE_LAZY', language: ['c'])
endif

pzstd_warning_flags = []
if [compiler_gcc, compiler_clang].contains(cc_id)
common_warning_flags = [ '-Wundef', '-Wshadow', '-Wcast-align', '-Wcast-qual' ]
Expand Down
2 changes: 2 additions & 0 deletions build/meson/meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ option('lzma', type: 'feature', value: 'auto',
description: 'Enable lzma support')
option('lz4', type: 'feature', value: 'auto',
description: 'Enable lz4 support')
option('selective_lazy', type: 'boolean', value: false,
description: 'Enable selective lazy evaluation optimization')
7 changes: 7 additions & 0 deletions lib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ VERSION := $(ZSTD_VERSION)
# -mt or -nomt to the build target (like lib-mt for multi-threaded, lib-nomt for single-threaded).


# Selective lazy optimization: reduces lazy evaluation overhead for
# structured binary data workloads with predictable match patterns.
# Enable with: make ZSTD_SELECTIVE_LAZY=1
ifdef ZSTD_SELECTIVE_LAZY
CPPFLAGS += -DZSTD_SELECTIVE_LAZY
endif

CPPFLAGS_DYNLIB += -DZSTD_MULTITHREAD # dynamic library build defaults to multi-threaded
LDFLAGS_DYNLIB += -pthread
CPPFLAGS_STATICLIB += # static library build defaults to single-threaded
Expand Down
20 changes: 20 additions & 0 deletions lib/compress/zstd_lazy.c
Original file line number Diff line number Diff line change
Expand Up @@ -1669,7 +1669,16 @@ size_t ZSTD_compressBlock_lazy_generic(
}

/* let's try to find a better solution */
#ifdef ZSTD_SELECTIVE_LAZY
/* Selective lazy: depth-adaptive threshold skips lazy evaluation
* for strong matches or nearby offsets. Repcode early exit avoids
* expensive searchMax when a repcode match is accepted. */
if (depth>=1
&& matchLength < (ms->cParams.targetLength <= 8 ? 12 : 16)
&& OFFBASE_IS_OFFSET(offBase) && OFFBASE_TO_OFFSET(offBase) > 256)
#else
if (depth>=1)
#endif
while (ip<ilimit) {
DEBUGLOG(7, "search depth 1");
ip ++;
Expand All @@ -1696,6 +1705,13 @@ size_t ZSTD_compressBlock_lazy_generic(
matchLength = mlRep, offBase = REPCODE1_TO_OFFBASE, start = ip;
}
}
#ifdef ZSTD_SELECTIVE_LAZY
/* Repcode early exit: if a repcode match was accepted above,
* skip the expensive searchMax. A regular match would need to be
* 4+ bytes longer to beat a repcode (zero offset encoding cost),
* making searchMax very unlikely to improve the result. */
if (!OFFBASE_IS_OFFSET(offBase)) break;
#endif
{ size_t ofbCandidate=999999999;
size_t const ml2 = ZSTD_searchMax(ms, ip, iend, &ofbCandidate, mls, rowLog, searchMethod, dictMode);
int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)ofbCandidate)); /* raw approx */
Expand Down Expand Up @@ -1732,6 +1748,10 @@ size_t ZSTD_compressBlock_lazy_generic(
matchLength = mlRep, offBase = REPCODE1_TO_OFFBASE, start = ip;
}
}
#ifdef ZSTD_SELECTIVE_LAZY
/* Repcode early exit at depth 2 */
if (!OFFBASE_IS_OFFSET(offBase)) break;
#endif
{ size_t ofbCandidate=999999999;
size_t const ml2 = ZSTD_searchMax(ms, ip, iend, &ofbCandidate, mls, rowLog, searchMethod, dictMode);
int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)ofbCandidate)); /* raw approx */
Expand Down
4 changes: 4 additions & 0 deletions programs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ zstd-release:
LIBZSTD_MK_DIR = ../lib
include $(LIBZSTD_MK_DIR)/libzstd.mk

ifdef ZSTD_SELECTIVE_LAZY
CPPFLAGS += -DZSTD_SELECTIVE_LAZY
endif

ifeq ($(shell $(CC) -v 2>&1 | $(GREP) -c "gcc version "), 1)
ALIGN_LOOP = -falign-loops=32
else
Expand Down
Loading