improved dicitonary segment merge

This commit is contained in:
Yann Collet 2016-10-18 16:34:58 -07:00
parent a7a4690b0a
commit 52c1bf93fe

View File

@ -371,21 +371,22 @@ static dictItem ZDICT_analyzePos(
static U32 ZDICT_checkMerge(dictItem* table, dictItem elt, U32 eltNbToSkip) static U32 ZDICT_checkMerge(dictItem* table, dictItem elt, U32 eltNbToSkip)
{ {
const U32 tableSize = table->pos; const U32 tableSize = table->pos;
const U32 max = elt.pos + (elt.length-1); const U32 eltEnd = elt.pos + elt.length;
/* tail overlap */ /* tail overlap */
U32 u; for (u=1; u<tableSize; u++) { U32 u; for (u=1; u<tableSize; u++) {
if (u==eltNbToSkip) continue; if (u==eltNbToSkip) continue;
if ((table[u].pos > elt.pos) && (table[u].pos < max)) { /* overlap */ if ((table[u].pos > elt.pos) && (table[u].pos <= eltEnd)) { /* overlap, existing > new */
/* append */ /* append */
U32 addedLength = table[u].pos - elt.pos; U32 addedLength = table[u].pos - elt.pos;
table[u].length += addedLength; table[u].length += addedLength;
table[u].pos = elt.pos; table[u].pos = elt.pos;
table[u].savings += elt.savings * addedLength / elt.length; /* rough approx */ table[u].savings += elt.savings * addedLength / elt.length; /* rough approx */
table[u].savings += elt.length / 8; /* rough approx */ table[u].savings += elt.length / 8; /* rough approx bonus */
elt = table[u]; elt = table[u];
/* sort : improve rank */
while ((u>1) && (table[u-1].savings < elt.savings)) while ((u>1) && (table[u-1].savings < elt.savings))
table[u] = table[u-1], u--; table[u] = table[u-1], u--;
table[u] = elt; table[u] = elt;
return u; return u;
} } } }
@ -393,14 +394,15 @@ static U32 ZDICT_checkMerge(dictItem* table, dictItem elt, U32 eltNbToSkip)
/* front overlap */ /* front overlap */
for (u=1; u<tableSize; u++) { for (u=1; u<tableSize; u++) {
if (u==eltNbToSkip) continue; if (u==eltNbToSkip) continue;
if ((table[u].pos + table[u].length > elt.pos) && (table[u].pos < elt.pos)) { /* overlap */ if ((table[u].pos + table[u].length >= elt.pos) && (table[u].pos < elt.pos)) { /* overlap, existing < new */
/* append */ /* append */
int addedLength = (elt.pos + elt.length) - (table[u].pos + table[u].length); int addedLength = (int)eltEnd - (table[u].pos + table[u].length);
table[u].savings += elt.length / 8; /* rough approx */ table[u].savings += elt.length / 8; /* rough approx bonus */
if (addedLength > 0) { /* otherwise, already included */ if (addedLength > 0) { /* otherwise, elt fully included into existing */
table[u].length += addedLength; table[u].length += addedLength;
table[u].savings += elt.savings * addedLength / elt.length; /* rough approx */ table[u].savings += elt.savings * addedLength / elt.length; /* rough approx */
} }
/* sort : improve rank */
elt = table[u]; elt = table[u];
while ((u>1) && (table[u-1].savings < elt.savings)) while ((u>1) && (table[u-1].savings < elt.savings))
table[u] = table[u-1], u--; table[u] = table[u-1], u--;