Skip to content

Commit 41e05ad

Browse files
committed
Improve compression ratio
1 parent 4fb79fc commit 41e05ad

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

src/transform/LZCodec.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ bool LZCodec::inverse(SliceArray<byte>& input, SliceArray<byte>& output, int cou
6767
template<>
6868
const uint LZXCodec<false>::HASH_SEED = 0x1E35A7BD;
6969
template<>
70-
const uint LZXCodec<false>::HASH_LOG = 17;
70+
const uint LZXCodec<false>::HASH_LOG = 16;
7171
template<>
7272
const uint LZXCodec<false>::HASH_RSHIFT = 64 - HASH_LOG;
7373
template<>
@@ -176,13 +176,13 @@ bool LZXCodec<T>::forward(SliceArray<byte>& input, SliceArray<byte>& output, int
176176
int srcInc = 0;
177177

178178
while (srcIdx < srcEnd) {
179-
const int minRef = max(srcIdx - maxDist, 0);
180179
int bestLen = 0;
181180
const int32 h0 = hash(&src[srcIdx]);
182181
const int ref0 = _hashes[h0];
183182
_hashes[h0] = srcIdx;
184183
const int srcIdx1 = srcIdx + 1;
185184
int ref = srcIdx1 - repd[repIdx];
185+
const int minRef = max(srcIdx - maxDist, 0);
186186

187187
if ((ref > minRef) && (memcmp(&src[srcIdx1], &src[ref], 4) == 0)) {
188188
// Check repd first
@@ -207,8 +207,7 @@ bool LZXCodec<T>::forward(SliceArray<byte>& input, SliceArray<byte>& output, int
207207

208208
// No good match ?
209209
if (bestLen < minMatch) {
210-
srcIdx++;
211-
srcIdx += (srcInc >> 6);
210+
srcIdx = srcIdx + (srcInc >> 6) + 1;
212211
srcInc++;
213212
repIdx = 0;
214213
continue;
@@ -225,18 +224,19 @@ bool LZXCodec<T>::forward(SliceArray<byte>& input, SliceArray<byte>& output, int
225224

226225
// Select best match
227226
if ((bestLen1 > bestLen) || ((bestLen1 == bestLen) && (ref1 > ref))) {
228-
if ((bestLen >= MAX_MATCH) || (src[srcIdx] != src[ref1 - 1])) {
229-
ref = ref1;
230-
bestLen = bestLen1;
231-
srcIdx++;
232-
}
233-
else {
234-
ref = ref1 - 1;
235-
bestLen = bestLen1 + 1;
236-
}
227+
ref = ref1;
228+
bestLen = bestLen1;
229+
srcIdx = srcIdx1;
237230
}
238231
}
239232
}
233+
234+
// Extend backwards
235+
while ((bestLen < MAX_MATCH) && (srcIdx > anchor) && (ref > minRef) && (src[srcIdx - 1] == src[ref - 1])) {
236+
bestLen++;
237+
ref--;
238+
srcIdx--;
239+
}
240240
}
241241
else {
242242
if ((bestLen >= MAX_MATCH) || (src[srcIdx] != src[ref - 1])) {
@@ -502,17 +502,17 @@ bool LZXCodec<T>::inverse(SliceArray<byte>& input, SliceArray<byte>& output, int
502502
dstIdx += 16;
503503
} while (dstIdx < mEnd);
504504
}
505-
else if (dist >= 4) {
506-
do {
507-
// No overlap
508-
memcpy(&dst[dstIdx], &dst[ref], 4);
509-
ref += 4;
510-
dstIdx += 4;
511-
} while (dstIdx < mEnd);
505+
else if (dist != 1) {
506+
const byte* s = &dst[ref];
507+
byte* p = &dst[dstIdx];
508+
const byte* pend = &p[mLen];
509+
510+
while (p < pend)
511+
*p++ = *s++;
512512
}
513513
else {
514-
for (int i = 0; i < mLen; i++)
515-
dst[dstIdx + i] = dst[ref + i];
514+
// dist = 1
515+
memset(&dst[dstIdx], dst[ref], mLen);
516516
}
517517

518518
dstIdx = mEnd;

0 commit comments

Comments
 (0)