Skip to content

apultra_insert_forward_match optimize #20

@ghost

Description

I think it better code 👍

static void apultra_insert_forward_match(apultra_compressor *pCompressor, const unsigned char *pInWindow, const int i, const int nMatchOffset, const int nStartOffset, const int nEndOffset, const int nArrivalsPerPosition, const int nDepth) {

    unsigned char slots_mask = 0xFF;

   const apultra_arrival *arrival = pCompressor->arrival + ((i - nStartOffset) * nArrivalsPerPosition);
   const int *rle_len = (const int*)pCompressor->intervals /* reuse */;
   apultra_visited* visited = ((apultra_visited*)pCompressor->pos_data) - nStartOffset /* reuse */;
   int j;

   for (j = 0; j < nArrivalsPerPosition; j++) {
       if (!(slots_mask & (1 << j))) {
           if (arrival[j].follows_literal) {
               const int nRepOffset = arrival[j].rep_offset;

               if (nMatchOffset != nRepOffset) {
                   const int nRepPos = arrival[j].rep_pos;

                   if (nRepPos >= nStartOffset &&
                       (nRepPos + 1) < nEndOffset &&
                       visited[nRepPos] != nMatchOffset) {

                       visited[nRepPos] = nMatchOffset;

                       apultra_match* fwd_match = pCompressor->match + ((nRepPos - nStartOffset) << MATCHES_PER_INDEX_SHIFT);

                       if (fwd_match[NMATCHES_PER_INDEX - 1].length == 0) {
                           if (nRepPos >= nMatchOffset) {
                               const unsigned char* pInWindowStart = pInWindow + nRepPos;

                               if (!memcmp(pInWindowStart, pInWindowStart - nMatchOffset, 2)) {
                                   if (nRepOffset) {
                                       const int nLen0 = rle_len[nRepPos - nMatchOffset];
                                       const int nLen1 = rle_len[nRepPos];
                                       const int nMinLen = (nLen0 < nLen1) ? nLen0 : nLen1;

                                       unsigned int nMaxRepLen = nEndOffset - nRepPos;
                                       if (nMaxRepLen > LCP_MAX)
                                           nMaxRepLen = LCP_MAX;

                                       const unsigned char* pInWindowMax = pInWindowStart + nMaxRepLen;
                                       const unsigned char* pInWindowAtRepOffset = pInWindowStart + nMinLen;

                                       if (pInWindowAtRepOffset > pInWindowMax)
                                           pInWindowAtRepOffset = pInWindowMax;

                                       while ((pInWindowAtRepOffset + 8) < pInWindowMax && !memcmp(pInWindowAtRepOffset, pInWindowAtRepOffset - nMatchOffset, 8))
                                           pInWindowAtRepOffset += 8;
                                       while ((pInWindowAtRepOffset + 4) < pInWindowMax && !memcmp(pInWindowAtRepOffset, pInWindowAtRepOffset - nMatchOffset, 4))
                                           pInWindowAtRepOffset += 4;
                                       while (pInWindowAtRepOffset < pInWindowMax && pInWindowAtRepOffset[0] == pInWindowAtRepOffset[-nMatchOffset])
                                           pInWindowAtRepOffset++;

                                       const unsigned int nCurRepLen = (const unsigned int)(pInWindowAtRepOffset - pInWindowStart);

                                       unsigned short* fwd_depth = pCompressor->match_depth + ((nRepPos - nStartOffset) << MATCHES_PER_INDEX_SHIFT);
                                       int r;

                                       for (r = 0; fwd_match[r].length; r++) {
                                           if (fwd_match[r].offset == nMatchOffset && (fwd_depth[r] & 0x3fff) == 0) {
                                               if (fwd_match[r].length < nCurRepLen) {
                                                   fwd_match[r].length = nCurRepLen;
                                                   fwd_depth[r] = 0;
                                               }
                                               break;
                                           }
                                       }

                                       if (fwd_match[r].length == 0) {
                                           fwd_match[r].length = nCurRepLen;
                                           fwd_match[r].offset = nMatchOffset;
                                           fwd_depth[r] = 0;

                                           if (nDepth < 9)
                                               apultra_insert_forward_match(pCompressor, pInWindow, nRepPos, nMatchOffset, nStartOffset, nEndOffset, nArrivalsPerPosition, nDepth + 1);
                                       }
                                   }
                               }
                           }
                       }
                   }
               }
           }
       }
       slots_mask >>= 1;
   }
}```

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions