| 1 | # Universal Lenient Base64 Decoder |
| 2 | |
| 3 | This is a modified version of the standard base64 decoder from `encoding/base64`. Key differences: |
| 4 | |
| 5 | - Accepts both Standard and URL-safe encoding, no need to specify it explicitly. |
| 6 | - Works for both padded and raw inputs. |
| 7 | - The input can be a `string` or a `[]byte`, no allocation or copy is performed for either. |
| 8 | |
| 9 | The goal was to create a decoder compatible with the one used in nodejs (and possibly elsewhere too). Unfortunately |
| 10 | the standard package does not expose enough API to do this efficiently, and trying all 4 possible variants |
| 11 | (standard/url, padded/raw) seemed wasteful. |
| 12 | |
| 13 | Because there is no need to convert the input from `string` to `[]byte`, decoding strings (specially long ones) |
| 14 | is more efficient than with the standard library. Below is the comparison between `BenchmarkDecodeBase64Std` |
| 15 | and `BenchmarkDecodeBase64` on go1.21.3: |
| 16 | |
| 17 | ``` |
| 18 | goos: linux |
| 19 | goarch: amd64 |
| 20 | pkg: base64dec |
| 21 | cpu: Intel(R) Core(TM) i7-2600S CPU @ 2.80GHz |
| 22 | │ old.txt │ new.txt │ |
| 23 | │ sec/op │ sec/op vs base │ |
| 24 | DecodeBase64/2-8 28.85n ± 3% 22.95n ± 2% -20.45% (p=0.000 n=10) |
| 25 | DecodeBase64/4-8 48.05n ± 0% 41.41n ± 1% -13.83% (p=0.000 n=10) |
| 26 | DecodeBase64/8-8 38.31n ± 2% 29.55n ± 1% -22.89% (p=0.000 n=10) |
| 27 | DecodeBase64/64-8 406.7n ± 5% 113.0n ± 2% -72.20% (p=0.000 n=10) |
| 28 | DecodeBase64/8192-8 31.670µ ± 10% 9.995µ ± 2% -68.44% (p=0.000 n=10) |
| 29 | geomean 232.8n 126.0n -45.89% |
| 30 | |
| 31 | │ old.txt │ new.txt │ |
| 32 | │ B/s │ B/s vs base │ |
| 33 | DecodeBase64/2-8 132.2Mi ± 3% 166.2Mi ± 2% +25.70% (p=0.000 n=10) |
| 34 | DecodeBase64/4-8 158.8Mi ± 0% 184.2Mi ± 1% +16.04% (p=0.000 n=10) |
| 35 | DecodeBase64/8-8 298.7Mi ± 2% 387.3Mi ± 1% +29.67% (p=0.000 n=10) |
| 36 | DecodeBase64/64-8 206.4Mi ± 5% 742.4Mi ± 2% +259.71% (p=0.000 n=10) |
| 37 | DecodeBase64/8192-8 329.0Mi ± 11% 1042.4Mi ± 2% +216.87% (p=0.000 n=10) |
| 38 | geomean 211.8Mi 391.4Mi +84.81% |
| 39 | |
| 40 | │ old.txt │ new.txt │ |
| 41 | │ B/op │ B/op vs base │ |
| 42 | DecodeBase64/2-8 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ |
| 43 | DecodeBase64/4-8 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ |
| 44 | DecodeBase64/8-8 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ |
| 45 | DecodeBase64/64-8 96.00 ± 0% 0.00 ± 0% -100.00% (p=0.000 n=10) |
| 46 | DecodeBase64/8192-8 12.00Ki ± 0% 0.00Ki ± 0% -100.00% (p=0.000 n=10) |
| 47 | geomean ² ? ² ³ |
| 48 | ¹ all samples are equal |
| 49 | ² summaries must be >0 to compute geomean |
| 50 | ³ ratios must be >0 to compute geomean |
| 51 | |
| 52 | │ old.txt │ new.txt │ |
| 53 | │ allocs/op │ allocs/op vs base │ |
| 54 | DecodeBase64/2-8 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ |
| 55 | DecodeBase64/4-8 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ |
| 56 | DecodeBase64/8-8 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ |
| 57 | DecodeBase64/64-8 1.000 ± 0% 0.000 ± 0% -100.00% (p=0.000 n=10) |
| 58 | DecodeBase64/8192-8 1.000 ± 0% 0.000 ± 0% -100.00% (p=0.000 n=10) |
| 59 | geomean ² ? ² ³ |
| 60 | ¹ all samples are equal |
| 61 | ² summaries must be >0 to compute geomean |
| 62 | ³ ratios must be >0 to compute geomean |
| 63 | ``` |