Coverage Report

Created: 2025-08-24 07:05

/src/zstd/lib/compress/zstd_preSplit.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) Meta Platforms, Inc. and affiliates.
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
11
#include "../common/compiler.h" /* ZSTD_ALIGNOF */
12
#include "../common/mem.h" /* S64 */
13
#include "../common/zstd_deps.h" /* ZSTD_memset */
14
#include "../common/zstd_internal.h" /* ZSTD_STATIC_ASSERT */
15
#include "hist.h" /* HIST_add */
16
#include "zstd_preSplit.h"
17
18
19
#define BLOCKSIZE_MIN 3500
20
62.1k
#define THRESHOLD_PENALTY_RATE 16
21
31.0k
#define THRESHOLD_BASE (THRESHOLD_PENALTY_RATE - 2)
22
14.5k
#define THRESHOLD_PENALTY 3
23
24
44.1k
#define HASHLENGTH 2
25
15.9M
#define HASHLOG_MAX 10
26
15.9M
#define HASHTABLESIZE (1 << HASHLOG_MAX)
27
#define HASHMASK (HASHTABLESIZE - 1)
28
133M
#define KNUTH 0x9e3779b9
29
30
/* for hashLog > 8, hash 2 bytes.
31
 * for hashLog == 8, just take the byte, no hashing.
32
 * The speed of this method relies on compile-time constant propagation */
33
FORCE_INLINE_TEMPLATE unsigned hash2(const void *p, unsigned hashLog)
34
135M
{
35
135M
    assert(hashLog >= 8);
36
135M
    if (hashLog == 8) return (U32)((const BYTE*)p)[0];
37
133M
    assert(hashLog <= HASHLOG_MAX);
38
133M
    return (U32)(MEM_read16(p)) * KNUTH >> (32 - hashLog);
39
133M
}
40
41
42
typedef struct {
43
  unsigned events[HASHTABLESIZE];
44
  size_t nbEvents;
45
} Fingerprint;
46
typedef struct {
47
    Fingerprint pastEvents;
48
    Fingerprint newEvents;
49
} FPStats;
50
51
static void initStats(FPStats* fpstats)
52
15.9k
{
53
15.9k
    ZSTD_memset(fpstats, 0, sizeof(FPStats));
54
15.9k
}
55
56
FORCE_INLINE_TEMPLATE void
57
addEvents_generic(Fingerprint* fp, const void* src, size_t srcSize, size_t samplingRate, unsigned hashLog)
58
44.1k
{
59
44.1k
    const char* p = (const char*)src;
60
44.1k
    size_t limit = srcSize - HASHLENGTH + 1;
61
44.1k
    size_t n;
62
44.1k
    assert(srcSize >= HASHLENGTH);
63
135M
    for (n = 0; n < limit; n+=samplingRate) {
64
135M
        fp->events[hash2(p+n, hashLog)]++;
65
135M
    }
66
44.1k
    fp->nbEvents += limit/samplingRate;
67
44.1k
}
68
69
FORCE_INLINE_TEMPLATE void
70
recordFingerprint_generic(Fingerprint* fp, const void* src, size_t srcSize, size_t samplingRate, unsigned hashLog)
71
44.1k
{
72
44.1k
    ZSTD_memset(fp, 0, sizeof(unsigned) * ((size_t)1 << hashLog));
73
44.1k
    fp->nbEvents = 0;
74
44.1k
    addEvents_generic(fp, src, srcSize, samplingRate, hashLog);
75
44.1k
}
76
77
typedef void (*RecordEvents_f)(Fingerprint* fp, const void* src, size_t srcSize);
78
79
58.0k
#define FP_RECORD(_rate) ZSTD_recordFingerprint_##_rate
80
81
#define ZSTD_GEN_RECORD_FINGERPRINT(_rate, _hSize)                                 \
82
    static void FP_RECORD(_rate)(Fingerprint* fp, const void* src, size_t srcSize) \
83
44.1k
    {                                                                              \
84
44.1k
        recordFingerprint_generic(fp, src, srcSize, _rate, _hSize);                \
85
44.1k
    }
zstd_preSplit.c:ZSTD_recordFingerprint_43
Line
Count
Source
83
9.95k
    {                                                                              \
84
9.95k
        recordFingerprint_generic(fp, src, srcSize, _rate, _hSize);                \
85
9.95k
    }
zstd_preSplit.c:ZSTD_recordFingerprint_11
Line
Count
Source
83
7.81k
    {                                                                              \
84
7.81k
        recordFingerprint_generic(fp, src, srcSize, _rate, _hSize);                \
85
7.81k
    }
zstd_preSplit.c:ZSTD_recordFingerprint_5
Line
Count
Source
83
13.5k
    {                                                                              \
84
13.5k
        recordFingerprint_generic(fp, src, srcSize, _rate, _hSize);                \
85
13.5k
    }
zstd_preSplit.c:ZSTD_recordFingerprint_1
Line
Count
Source
83
12.8k
    {                                                                              \
84
12.8k
        recordFingerprint_generic(fp, src, srcSize, _rate, _hSize);                \
85
12.8k
    }
86
87
ZSTD_GEN_RECORD_FINGERPRINT(1, 10)
88
ZSTD_GEN_RECORD_FINGERPRINT(5, 10)
89
ZSTD_GEN_RECORD_FINGERPRINT(11, 9)
90
ZSTD_GEN_RECORD_FINGERPRINT(43, 8)
91
92
93
23.8M
static U64 abs64(S64 s64) { return (U64)((s64 < 0) ? -s64 : s64); }
94
95
static U64 fpDistance(const Fingerprint* fp1, const Fingerprint* fp2, unsigned hashLog)
96
33.0k
{
97
33.0k
    U64 distance = 0;
98
33.0k
    size_t n;
99
33.0k
    assert(hashLog <= HASHLOG_MAX);
100
23.8M
    for (n = 0; n < ((size_t)1 << hashLog); n++) {
101
23.8M
        distance +=
102
23.8M
            abs64((S64)fp1->events[n] * (S64)fp2->nbEvents - (S64)fp2->events[n] * (S64)fp1->nbEvents);
103
23.8M
    }
104
33.0k
    return distance;
105
33.0k
}
106
107
/* Compare newEvents with pastEvents
108
 * return 1 when considered "too different"
109
 */
110
static int compareFingerprints(const Fingerprint* ref,
111
                            const Fingerprint* newfp,
112
                            int penalty,
113
                            unsigned hashLog)
114
31.0k
{
115
31.0k
    assert(ref->nbEvents > 0);
116
31.0k
    assert(newfp->nbEvents > 0);
117
31.0k
    {   U64 p50 = (U64)ref->nbEvents * (U64)newfp->nbEvents;
118
31.0k
        U64 deviation = fpDistance(ref, newfp, hashLog);
119
31.0k
        U64 threshold = p50 * (U64)(THRESHOLD_BASE + penalty) / THRESHOLD_PENALTY_RATE;
120
31.0k
        return deviation >= threshold;
121
31.0k
    }
122
31.0k
}
123
124
static void mergeEvents(Fingerprint* acc, const Fingerprint* newfp)
125
15.5k
{
126
15.5k
    size_t n;
127
15.9M
    for (n = 0; n < HASHTABLESIZE; n++) {
128
15.9M
        acc->events[n] += newfp->events[n];
129
15.9M
    }
130
15.5k
    acc->nbEvents += newfp->nbEvents;
131
15.5k
}
132
133
static void flushEvents(FPStats* fpstats)
134
0
{
135
0
    size_t n;
136
0
    for (n = 0; n < HASHTABLESIZE; n++) {
137
0
        fpstats->pastEvents.events[n] = fpstats->newEvents.events[n];
138
0
    }
139
0
    fpstats->pastEvents.nbEvents = fpstats->newEvents.nbEvents;
140
0
    ZSTD_memset(&fpstats->newEvents, 0, sizeof(fpstats->newEvents));
141
0
}
142
143
static void removeEvents(Fingerprint* acc, const Fingerprint* slice)
144
0
{
145
0
    size_t n;
146
0
    for (n = 0; n < HASHTABLESIZE; n++) {
147
0
        assert(acc->events[n] >= slice->events[n]);
148
0
        acc->events[n] -= slice->events[n];
149
0
    }
150
0
    acc->nbEvents -= slice->nbEvents;
151
0
}
152
153
104k
#define CHUNKSIZE (8 << 10)
154
static size_t ZSTD_splitBlock_byChunks(const void* blockStart, size_t blockSize,
155
                        int level,
156
                        void* workspace, size_t wkspSize)
157
14.5k
{
158
14.5k
    static const RecordEvents_f records_fs[] = {
159
14.5k
        FP_RECORD(43), FP_RECORD(11), FP_RECORD(5), FP_RECORD(1)
160
14.5k
    };
161
14.5k
    static const unsigned hashParams[] = { 8, 9, 10, 10 };
162
14.5k
    const RecordEvents_f record_f = (assert(0<=level && level<=3), records_fs[level]);
163
0
    FPStats* const fpstats = (FPStats*)workspace;
164
14.5k
    const char* p = (const char*)blockStart;
165
14.5k
    int penalty = THRESHOLD_PENALTY;
166
14.5k
    size_t pos = 0;
167
14.5k
    assert(blockSize == (128 << 10));
168
14.5k
    assert(workspace != NULL);
169
14.5k
    assert((size_t)workspace % ZSTD_ALIGNOF(FPStats) == 0);
170
14.5k
    ZSTD_STATIC_ASSERT(ZSTD_SLIPBLOCK_WORKSPACESIZE >= sizeof(FPStats));
171
14.5k
    assert(wkspSize >= sizeof(FPStats)); (void)wkspSize;
172
173
14.5k
    initStats(fpstats);
174
14.5k
    record_f(&fpstats->pastEvents, p, CHUNKSIZE);
175
30.0k
    for (pos = CHUNKSIZE; pos <= blockSize - CHUNKSIZE; pos += CHUNKSIZE) {
176
29.6k
        record_f(&fpstats->newEvents, p + pos, CHUNKSIZE);
177
29.6k
        if (compareFingerprints(&fpstats->pastEvents, &fpstats->newEvents, penalty, hashParams[level])) {
178
14.1k
            return pos;
179
15.5k
        } else {
180
15.5k
            mergeEvents(&fpstats->pastEvents, &fpstats->newEvents);
181
15.5k
            if (penalty > 0) penalty--;
182
15.5k
        }
183
29.6k
    }
184
413
    assert(pos == blockSize);
185
413
    return blockSize;
186
0
    (void)flushEvents; (void)removeEvents;
187
0
}
188
189
/* ZSTD_splitBlock_fromBorders(): very fast strategy :
190
 * compare fingerprint from beginning and end of the block,
191
 * derive from their difference if it's preferable to split in the middle,
192
 * repeat the process a second time, for finer grained decision.
193
 * 3 times did not brought improvements, so I stopped at 2.
194
 * Benefits are good enough for a cheap heuristic.
195
 * More accurate splitting saves more, but speed impact is also more perceptible.
196
 * For better accuracy, use more elaborate variant *_byChunks.
197
 */
198
static size_t ZSTD_splitBlock_fromBorders(const void* blockStart, size_t blockSize,
199
                        void* workspace, size_t wkspSize)
200
1.42k
{
201
10.7k
#define SEGMENT_SIZE 512
202
1.42k
    FPStats* const fpstats = (FPStats*)workspace;
203
1.42k
    Fingerprint* middleEvents = (Fingerprint*)(void*)((char*)workspace + 512 * sizeof(unsigned));
204
1.42k
    assert(blockSize == (128 << 10));
205
1.42k
    assert(workspace != NULL);
206
1.42k
    assert((size_t)workspace % ZSTD_ALIGNOF(FPStats) == 0);
207
1.42k
    ZSTD_STATIC_ASSERT(ZSTD_SLIPBLOCK_WORKSPACESIZE >= sizeof(FPStats));
208
1.42k
    assert(wkspSize >= sizeof(FPStats)); (void)wkspSize;
209
210
1.42k
    initStats(fpstats);
211
1.42k
    HIST_add(fpstats->pastEvents.events, blockStart, SEGMENT_SIZE);
212
1.42k
    HIST_add(fpstats->newEvents.events, (const char*)blockStart + blockSize - SEGMENT_SIZE, SEGMENT_SIZE);
213
1.42k
    fpstats->pastEvents.nbEvents = fpstats->newEvents.nbEvents = SEGMENT_SIZE;
214
1.42k
    if (!compareFingerprints(&fpstats->pastEvents, &fpstats->newEvents, 0, 8))
215
412
        return blockSize;
216
217
1.00k
    HIST_add(middleEvents->events, (const char*)blockStart + blockSize/2 - SEGMENT_SIZE/2, SEGMENT_SIZE);
218
1.00k
    middleEvents->nbEvents = SEGMENT_SIZE;
219
1.00k
    {   U64 const distFromBegin = fpDistance(&fpstats->pastEvents, middleEvents, 8);
220
1.00k
        U64 const distFromEnd = fpDistance(&fpstats->newEvents, middleEvents, 8);
221
1.00k
        U64 const minDistance = SEGMENT_SIZE * SEGMENT_SIZE / 3;
222
1.00k
        if (abs64((S64)distFromBegin - (S64)distFromEnd) < minDistance)
223
470
            return 64 KB;
224
538
        return (distFromBegin > distFromEnd) ? 32 KB : 96 KB;
225
1.00k
    }
226
1.00k
}
227
228
size_t ZSTD_splitBlock(const void* blockStart, size_t blockSize,
229
                    int level,
230
                    void* workspace, size_t wkspSize)
231
15.9k
{
232
15.9k
    DEBUGLOG(6, "ZSTD_splitBlock (level=%i)", level);
233
15.9k
    assert(0<=level && level<=4);
234
15.9k
    if (level == 0)
235
1.42k
        return ZSTD_splitBlock_fromBorders(blockStart, blockSize, workspace, wkspSize);
236
    /* level >= 1*/
237
14.5k
    return ZSTD_splitBlock_byChunks(blockStart, blockSize, level-1, workspace, wkspSize);
238
15.9k
}