A Tale of Two Optimisations
While I applaud the efforts to document the optimization processes, they are fundamentally slow. The easiest and portable optimization would be converting 8 full bits at a time:<p><pre><code>    for (int i = 0; i &lt; 256; ++i) {
        const char c[4] = {
            encode_lookup_tbl[i &gt;&gt; 0 &amp; 3],
            encode_lookup_tbl[i &gt;&gt; 2 &amp; 3],
            encode_lookup_tbl[i &gt;&gt; 4 &amp; 3],
            encode_lookup_tbl[i &gt;&gt; 6 &amp; 3],
        };
        encode_lookup_tbl2[i] = *(uint32_t*)c;
    }

    for (x = 0; x &lt; bytes; x++) {
        ((uint32_t*)ws_out)[x] = encode_lookup_tbl2[bytes_in[x]];
    }
</code></pre>
Note that the resulting encode_lookup_tbl2 depends on the endianness, but as long as we make the table on demand it can remain portable. Also `ws_out` might have to be aligned for the potability (unaligned memory access can be slow at best and trap at worst) but it is easy to fulfill when those functions are only used internally. In my measurement this alone made it ~3.5x faster.<p>The next obvious approach is to use SIMD. Pretty much every modern CPU gives you 16-byte shuffle, which can be used to convert 4 bits at a time. (EDIT: This turned out to be not very correct, see below.) I tried to write a quick example but it takes quite a bit of time to write and debug [1] ;-) (I may update this post if I get a working version.)<p>Lastly, it might be possible to use mmap to eliminate the intermediate buffer at all. I think the actual performance gain with and without mmap can vary much and you need to profile it, but it&#x27;s worth considering.<p>[1] I realized that there is no bytewise shift operator in x86 SIMD (_mm_srli_epi8). Oops!<p>---<p>Added later: So the following is my crude AVX2 implementation. It is 20% faster than the bytewise version and 10% faster than the original SSSE3 version. It is not that satisfactory though because it had to do a lot of unpacking to get bytes into correct positions (I&#x27;m not a great SIMD coder after all).<p><pre><code>    const __m256i MASK = _mm256_set1_epi8(0xf);

    _Alignas(32) char c[32];
    for (int i = 0; i &lt; 32; ++i) c[i] = encode_lookup_tbl[i &amp; 3];
    const __m256i MAPLO = _mm256_load_si256((void*)c);
    for (int i = 0; i &lt; 32; ++i) c[i] = encode_lookup_tbl[i &gt;&gt; 2 &amp; 3];
    const __m256i MAPHI = _mm256_load_si256((void*)c);

    for (x = 0; x &lt; bytes; x += 32) {
        &#x2F;&#x2F; abcdefghijklmnop ABCDEFGHIJKLMNOP
        __m256i block = _mm256_loadu_si256((void*)(bytes_in + x));
        &#x2F;&#x2F; ah bh ch dh ... mh nh oh ph
        __m256i hi = _mm256_and_si256(_mm256_srli_epi16(block, 4), MASK);
        &#x2F;&#x2F; al bl cl dl ... ml nl ol pl
        __m256i lo = _mm256_and_si256(block, MASK);
        &#x2F;&#x2F; MAPLO[al] ... MAPLO[pl]
        &#x2F;&#x2F; MAPHI[al] ... MAPLO[pl]
        &#x2F;&#x2F; MAPLO[ah] ... MAPHI[ph]
        &#x2F;&#x2F; MAPHI[ah] ... MAPHI[ph]
        __m256i mapped1 = _mm256_shuffle_epi8(MAPLO, lo);
        __m256i mapped2 = _mm256_shuffle_epi8(MAPHI, lo);
        __m256i mapped3 = _mm256_shuffle_epi8(MAPLO, hi);
        __m256i mapped4 = _mm256_shuffle_epi8(MAPHI, hi);
        &#x2F;&#x2F; MAPLO[al] MAPLO[ah] ... MAPLO[hl] MAPLO[hh]
        &#x2F;&#x2F; MAPLO[il] MAPLO[ih] ... MAPLO[pl] MAPLO[ph]
        &#x2F;&#x2F; MAPHI[al] MAPHI[ah] ... MAPHI[hl] MAPHI[hh]
        &#x2F;&#x2F; MAPHI[il] MAPHI[ih] ... MAPHI[pl] MAPHI[ph]
        __m256i shuf1 = _mm256_unpacklo_epi8(mapped1, mapped3);
        __m256i shuf2 = _mm256_unpackhi_epi8(mapped1, mapped3);
        __m256i shuf3 = _mm256_unpacklo_epi8(mapped2, mapped4);
        __m256i shuf4 = _mm256_unpackhi_epi8(mapped2, mapped4);
        &#x2F;&#x2F; MAPLO[al] MAPHI[al] MAPLO[ah] MAPHI[ah] ... MAPLO[dl] MAPHI[dl] MAPLO[dh] MAPHI[dh] etc.
        &#x2F;&#x2F; bytewise:
        &#x2F;&#x2F; aaaabbbbccccdddd AAAABBBBCCCCDDDD
        &#x2F;&#x2F; eeeeffffgggghhhh EEEEFFFFGGGGHHHH
        &#x2F;&#x2F; iiiijjjjkkkkllll IIIIJJJJKKKKLLLL
        &#x2F;&#x2F; mmmmnnnnoooopppp MMMMNNNNOOOOPPPP
        __m256i out1 = _mm256_unpacklo_epi8(shuf1, shuf3);
        __m256i out2 = _mm256_unpackhi_epi8(shuf1, shuf3);
        __m256i out3 = _mm256_unpacklo_epi8(shuf2, shuf4);
        __m256i out4 = _mm256_unpackhi_epi8(shuf2, shuf4);
        _mm_store_si128((void*)(ws_out + x * 4),       _mm256_extracti128_si256(out1, 0));
        _mm_store_si128((void*)(ws_out + x * 4 + 16),  _mm256_extracti128_si256(out2, 0));
        _mm_store_si128((void*)(ws_out + x * 4 + 32),  _mm256_extracti128_si256(out3, 0));
        _mm_store_si128((void*)(ws_out + x * 4 + 48),  _mm256_extracti128_si256(out4, 0));
        _mm_store_si128((void*)(ws_out + x * 4 + 64),  _mm256_extracti128_si256(out1, 1));
        _mm_store_si128((void*)(ws_out + x * 4 + 80),  _mm256_extracti128_si256(out2, 1));
        _mm_store_si128((void*)(ws_out + x * 4 + 96),  _mm256_extracti128_si256(out3, 1));
        _mm_store_si128((void*)(ws_out + x * 4 + 112), _mm256_extracti128_si256(out4, 1));
    }</code></pre>