/swift-base64

Base64 encode and decode without the use of Foundation in pure Swift.

Primary LanguageSwiftApache License 2.0Apache-2.0

swift-base64

Swift 5.1 github-actions codecov macOS tuxOS

This project 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

Everything began with an issue on swift-nio.

Status

  • support for base64 and base64url
  • faster than Foundation
  • decoding can ignore line breaks
  • encoding can insert line breaks
  • 100% test coverage

Performance

Super simple performance test to ensure speediness of this implementation. Encoding and decoding 1m times the base64 string:

AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==

macOS

MacBook Pro (15-inch, late 2016 - the first one with the butterfly keyboard). Quad Core 2,7 GHz Intel Core i7

Encoding Decoding
Foundation 2.21s 2.28s
swift-base64 1.01s 1.06s
Speedup 2.18x 2.14x

linux

Whatevar runs GitHub Actions 😉

Encoding Decoding
Foundation 33.64s 3.49s
swift-base64 1.07s 1.27s
Speedup 31.18x 2.74x

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 approached can be found here:

Alternatives

As of today (2019-12-10) the author is only aware of two alternatives that both offer only encoding.