optimize more code

This commit is contained in:
Hongze Cheng 2022-09-29 14:54:58 +08:00
parent cb554920aa
commit ce087d3dbc
1 changed files with 45 additions and 28 deletions

View File

@ -1228,6 +1228,7 @@ struct SCompressor {
int32_t i_selector;
int32_t i_start;
int32_t i_end;
int32_t i_nEle;
uint64_t i_aZigzag[241];
int8_t i_aBitN[241];
};
@ -1427,6 +1428,18 @@ static const uint8_t BIT_TO_SELECTOR[] = {0, 2, 3, 4, 5, 6, 7, 8, 9, 10
13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15};
static const int32_t NEXT_IDX[] = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66,
67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132,
133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154,
155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176,
177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198,
199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220,
221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 0};
static int32_t tCompIntStart(SCompressor *pCmprsor, int8_t type, int8_t cmprAlg) {
int32_t code = 0;
@ -1435,6 +1448,7 @@ static int32_t tCompIntStart(SCompressor *pCmprsor, int8_t type, int8_t cmprAlg)
pCmprsor->i_selector = 0;
pCmprsor->i_start = 0;
pCmprsor->i_end = 0;
pCmprsor->i_nEle = 0;
pCmprsor->nBuf = 1;
code = tRealloc(&pCmprsor->pBuf, pCmprsor->nBuf);
@ -1463,11 +1477,12 @@ static int32_t tCompIntSwitchToCopy(SCompressor *pCmprsor) {
memcpy(&b, pCmprsor->pBuf + n, sizeof(b));
n += sizeof(b);
int32_t i_selector = (b & 0xf);
int32_t nEle = SELECTOR_TO_ELEMS[i_selector];
uint8_t bits = BIT_PER_INTEGER[i_selector];
int32_t i_selector = (b & 0xf);
int32_t nEle = SELECTOR_TO_ELEMS[i_selector];
uint8_t bits = BIT_PER_INTEGER[i_selector];
uint64_t mask = (((uint64_t)1) << bits) - 1;
for (int32_t iEle = 0; iEle < nEle; iEle++) {
uint64_t vZigzag = (b >> (bits * iEle + 4)) & (((uint64_t)1 << bits) - 1);
uint64_t vZigzag = (b >> (bits * iEle + 4)) & mask;
vPrev = ZIGZAG_DECODE(int64_t, vZigzag) + vPrev;
DATA_TYPE_INFO[pCmprsor->type].putI64(vPrev, pCmprsor->aBuf[0] + nBuf);
@ -1475,11 +1490,14 @@ static int32_t tCompIntSwitchToCopy(SCompressor *pCmprsor) {
}
}
for (; pCmprsor->i_start != pCmprsor->i_end; pCmprsor->i_start = (pCmprsor->i_start + 1) % 241) {
while (pCmprsor->i_nEle) {
vPrev = ZIGZAG_DECODE(int64_t, pCmprsor->i_aZigzag[pCmprsor->i_start]) + vPrev;
memcpy(pCmprsor->aBuf[0] + nBuf, &vPrev, DATA_TYPE_INFO[pCmprsor->type].bytes);
nBuf += DATA_TYPE_INFO[pCmprsor->type].bytes;
pCmprsor->i_start = NEXT_IDX[pCmprsor->i_start];
pCmprsor->i_nEle--;
}
ASSERT(n == pCmprsor->nBuf && nBuf == size);
@ -1520,35 +1538,35 @@ static int32_t tCompInt(SCompressor *pCmprsor, const void *pData, int32_t nData)
pCmprsor->i_prev = val;
for (;;) {
int32_t nEle = (pCmprsor->i_end + 241 - pCmprsor->i_start) % 241;
if (nEle + 1 <= SELECTOR_TO_ELEMS[pCmprsor->i_selector] && nEle + 1 <= SELECTOR_TO_ELEMS[BIT_TO_SELECTOR[nBit]]) {
if (pCmprsor->i_nEle + 1 <= SELECTOR_TO_ELEMS[pCmprsor->i_selector] &&
pCmprsor->i_nEle + 1 <= SELECTOR_TO_ELEMS[BIT_TO_SELECTOR[nBit]]) {
if (pCmprsor->i_selector < BIT_TO_SELECTOR[nBit]) {
pCmprsor->i_selector = BIT_TO_SELECTOR[nBit];
}
pCmprsor->i_aZigzag[pCmprsor->i_end] = vZigzag;
pCmprsor->i_aBitN[pCmprsor->i_end] = nBit;
pCmprsor->i_end = (pCmprsor->i_end + 1) % 241;
pCmprsor->i_end = NEXT_IDX[pCmprsor->i_end];
pCmprsor->i_nEle++;
break;
} else {
if (nEle < SELECTOR_TO_ELEMS[pCmprsor->i_selector]) {
if (pCmprsor->i_nEle < SELECTOR_TO_ELEMS[pCmprsor->i_selector]) {
int32_t lidx = pCmprsor->i_selector + 1;
int32_t ridx = 15;
while (lidx <= ridx) {
pCmprsor->i_selector = (lidx + ridx) >> 1;
if (nEle < SELECTOR_TO_ELEMS[pCmprsor->i_selector]) {
if (pCmprsor->i_nEle < SELECTOR_TO_ELEMS[pCmprsor->i_selector]) {
lidx = pCmprsor->i_selector + 1;
} else if (nEle > SELECTOR_TO_ELEMS[pCmprsor->i_selector]) {
} else if (pCmprsor->i_nEle > SELECTOR_TO_ELEMS[pCmprsor->i_selector]) {
ridx = pCmprsor->i_selector - 1;
} else {
break;
}
}
if (nEle < SELECTOR_TO_ELEMS[pCmprsor->i_selector]) pCmprsor->i_selector++;
if (pCmprsor->i_nEle < SELECTOR_TO_ELEMS[pCmprsor->i_selector]) pCmprsor->i_selector++;
}
nEle = SELECTOR_TO_ELEMS[pCmprsor->i_selector];
int32_t nEle = SELECTOR_TO_ELEMS[pCmprsor->i_selector];
if (pCmprsor->autoAlloc && (code = tRealloc(&pCmprsor->pBuf, pCmprsor->nBuf + sizeof(uint64_t)))) {
return code;
@ -1559,13 +1577,14 @@ static int32_t tCompInt(SCompressor *pCmprsor, const void *pData, int32_t nData)
bp[0] = pCmprsor->i_selector;
uint8_t bits = BIT_PER_INTEGER[pCmprsor->i_selector];
for (int32_t iVal = 0; iVal < nEle; iVal++) {
bp[0] |= ((pCmprsor->i_aZigzag[pCmprsor->i_start] & ((((uint64_t)1) << bits) - 1)) << (bits * iVal + 4));
pCmprsor->i_start = (pCmprsor->i_start + 1) % 241;
bp[0] |= (pCmprsor->i_aZigzag[pCmprsor->i_start] << (bits * iVal + 4));
pCmprsor->i_start = NEXT_IDX[pCmprsor->i_start];
pCmprsor->i_nEle--;
}
// reset and continue
pCmprsor->i_selector = 0;
for (int32_t iVal = pCmprsor->i_start; iVal < pCmprsor->i_end; iVal = (iVal + 1) % 241) {
for (int32_t iVal = pCmprsor->i_start; iVal < pCmprsor->i_end; iVal = NEXT_IDX[iVal]) {
if (pCmprsor->i_selector < BIT_TO_SELECTOR[pCmprsor->i_aBitN[iVal]]) {
pCmprsor->i_selector = BIT_TO_SELECTOR[pCmprsor->i_aBitN[iVal]];
}
@ -1588,28 +1607,25 @@ static int32_t tCompInt(SCompressor *pCmprsor, const void *pData, int32_t nData)
static int32_t tCompIntEnd(SCompressor *pCmprsor, const uint8_t **ppData, int32_t *nData) {
int32_t code = 0;
for (;;) {
int32_t nEle = (pCmprsor->i_end + 241 - pCmprsor->i_start) % 241;
if (nEle == 0) break;
if (nEle < SELECTOR_TO_ELEMS[pCmprsor->i_selector]) {
for (; pCmprsor->i_nEle;) {
if (pCmprsor->i_nEle < SELECTOR_TO_ELEMS[pCmprsor->i_selector]) {
int32_t lidx = pCmprsor->i_selector + 1;
int32_t ridx = 15;
while (lidx <= ridx) {
pCmprsor->i_selector = (lidx + ridx) >> 1;
if (nEle < SELECTOR_TO_ELEMS[pCmprsor->i_selector]) {
if (pCmprsor->i_nEle < SELECTOR_TO_ELEMS[pCmprsor->i_selector]) {
lidx = pCmprsor->i_selector + 1;
} else if (nEle > SELECTOR_TO_ELEMS[pCmprsor->i_selector]) {
} else if (pCmprsor->i_nEle > SELECTOR_TO_ELEMS[pCmprsor->i_selector]) {
ridx = pCmprsor->i_selector - 1;
} else {
break;
}
}
if (nEle < SELECTOR_TO_ELEMS[pCmprsor->i_selector]) pCmprsor->i_selector++;
if (pCmprsor->i_nEle < SELECTOR_TO_ELEMS[pCmprsor->i_selector]) pCmprsor->i_selector++;
}
nEle = SELECTOR_TO_ELEMS[pCmprsor->i_selector];
int32_t nEle = SELECTOR_TO_ELEMS[pCmprsor->i_selector];
if (pCmprsor->autoAlloc && (code = tRealloc(&pCmprsor->pBuf, pCmprsor->nBuf + sizeof(uint64_t)))) {
return code;
@ -1620,8 +1636,9 @@ static int32_t tCompIntEnd(SCompressor *pCmprsor, const uint8_t **ppData, int32_
bp[0] = pCmprsor->i_selector;
uint8_t bits = BIT_PER_INTEGER[pCmprsor->i_selector];
for (int32_t iVal = 0; iVal < nEle; iVal++) {
bp[0] |= ((pCmprsor->i_aZigzag[pCmprsor->i_start] & ((((uint64_t)1) << bits) - 1)) << (bits * iVal + 4));
pCmprsor->i_start = (pCmprsor->i_start + 1) % 241;
bp[0] |= (pCmprsor->i_aZigzag[pCmprsor->i_start] << (bits * iVal + 4));
pCmprsor->i_start = NEXT_IDX[pCmprsor->i_start];
pCmprsor->i_nEle--;
}
pCmprsor->i_selector = 0;