This package provides a base64 encoder and decoder in pure Swift (without the use of Foundation). The implementation is RFC4648 complient.
Today the implementation is rather simple. No fancy precomputed lookup tables, no fancy SIMD instructions. Therefore, there is definitely room for improvement performance-wise. See also Literature for a faster algorithm.
- support for base64 and base64url
- faster than Foundation
- decoding can ignore line breaks
- encoding can insert line breaks
This package's encoding implementation is used in
swift-nio's websocket implementation.
Super simple performance test to ensure speediness of this implementation. Encoding and decoding 1m times the base64 string:
MacBook Pro (15-inch, late 2016 - the first one with the butterfly keyboard). Quad Core 2.7 GHz Intel Core i7
Whatevar runs GitHub Actions
I have no idea why Foundation base64 encoding is so slow on linux.
Literature for a faster algorithm
I would really like to speed up this project further to be way faster than it is today. Some food for thought of how this could be tackled can be found here:
- Chromium precomputed lookup tables
- Wojciech Muła, Daniel Lemire: Faster Base64 Encoding and Decoding using AVX2 Instructions.
- Daniel Lemire's blog - Ridiculously fast base64 encoding and decoding
- Swift SIMD support
As of today (2019-12-10), the author is aware of only one alternative that offers merely encoding.