Skip to content

Commit 2fdb8b8

Browse files
committed
gh-124951: Use the SSSE3 base64 encoder for URL-safe and altchars
The SSSE3 encoder depends on the alphabet only through the two table entries for the values 62 and 63. Fill those in from the alphabet in use, and run the kernel for any alphabet whose first 62 characters are the standard ones. That covers base64.urlsafe_b64encode() and base64.b64encode() with altchars. Decoding is unchanged: its lookup tables are tied to '+' and '/'. For 64 KiB inputs on an AMD Zen 2 with GCC 15, urlsafe_b64encode() goes from 2.2 to 9.5 GB/s. Co-authored with Claude Fable 5.2.
1 parent 6069d29 commit 2fdb8b8

4 files changed

Lines changed: 35 additions & 9 deletions

File tree

‎Doc/whatsnew/3.16.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,8 @@ base64 and binascii
11721172
instructions on x86-64 CPUs that support them (everything since 2014).
11731173
For inputs of 64 KiB, :func:`base64.b64encode` and :func:`base64.b64decode`
11741174
are about 4 times faster.
1175+
:func:`base64.urlsafe_b64encode` and encoding with *altchars* get the same
1176+
speedup.
11751177
(Contributed by Gregory P. Smith in :gh:`124951`.)
11761178

11771179

‎Lib/test/test_binascii.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,11 +462,23 @@ def test_base64_long_inputs(self):
462462
b'ABCDEFGHIJKLMNPQRSTUVXYZ[`abcdefhijklmpqr'),
463463
bytes(range(0x80, 0xc0)),
464464
]
465+
# Alphabets that differ from the standard one only in the last
466+
# two characters.
467+
variants = [
468+
binascii.URLSAFE_BASE64_ALPHABET,
469+
binascii.BASE64_ALPHABET[:-2] + b'\xff\x80',
470+
]
465471
raw = bytes(range(256)) * 2
466472
for n in range(len(raw) + 1):
467473
data = self.type2test(raw[:n])
468474
encoded = binascii.b2a_base64(data, newline=False)
469475
self.assertEqual(binascii.a2b_base64(encoded), raw[:n])
476+
for alphabet in variants:
477+
trans = bytes.maketrans(b'+/', alphabet[-2:])
478+
self.assertEqual(
479+
binascii.b2a_base64(data, newline=False,
480+
alphabet=alphabet),
481+
encoded.translate(trans))
470482
for alphabet in alphabets:
471483
encoded = binascii.b2a_base64(data, newline=False,
472484
alphabet=alphabet)
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
Speed up base64 encoding and decoding with the standard alphabet in
22
:mod:`binascii` and :mod:`base64` on x86-64 CPUs that support SSSE3.
3-
Large inputs are up to 4 times faster.
3+
Large inputs are up to 4 times faster. Encoding is sped up in the same way
4+
for the URL-safe alphabet and for any other alphabet that differs from the
5+
standard one only in its last two characters.

‎Modules/binascii.c‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ base64_encode_trio(const unsigned char *in, unsigned char *out,
137137

138138
#ifdef BASE64_HAVE_SSSE3
139139
/*
140-
* SSSE3 base64 kernels for x86-64, standard alphabet only.
140+
* SSSE3 base64 kernels for x86-64. The decoder handles the standard
141+
* alphabet only. The encoder takes any alphabet whose first 62 characters
142+
* are the standard ones, which includes the URL-safe alphabet.
141143
*
142144
* PSHUFB serves both as a byte shuffle and as a 16-entry lookup table,
143145
* which stands in for the per-character table lookups of the scalar code.
@@ -192,19 +194,23 @@ base64_cpu_has_ssse3(void)
192194
*/
193195
BASE64_SSSE3_TARGET static Py_ssize_t
194196
base64_encode_ssse3(const unsigned char *in, Py_ssize_t in_len,
195-
unsigned char *out)
197+
unsigned char *out, const unsigned char *table)
196198
{
197199
/* Put each group [b0 b1 b2] in a 32-bit lane as [b1 b0 b2 b1], so
198200
* that every 6-bit field lies within one 16-bit half of the lane. */
199201
const __m128i spread = _mm_setr_epi8(
200202
1, 0, 2, 1, 4, 3, 5, 4, 7, 6, 8, 7, 10, 9, 11, 10);
201203
/* The difference between a character and its 6-bit value. The index
202204
* is 13 for 0..25 (A-Z), 0 for 26..51 (a-z), 1..10 for 52..61 (0-9),
203-
* 11 for 62 (+) and 12 for 63 (/). */
204-
const __m128i offsets = _mm_setr_epi8(
205+
* 11 for 62 and 12 for 63. The last two entries depend on the
206+
* alphabet and are filled in below. */
207+
const __m128i fixed_offsets = _mm_setr_epi8(
205208
'a' - 26, '0' - 52, '0' - 52, '0' - 52, '0' - 52, '0' - 52,
206-
'0' - 52, '0' - 52, '0' - 52, '0' - 52, '0' - 52, '+' - 62,
207-
'/' - 63, 'A', 0, 0);
209+
'0' - 52, '0' - 52, '0' - 52, '0' - 52, '0' - 52, 0, 0, 'A', 0, 0);
210+
int last = (unsigned char)(table[62] - 62)
211+
| ((unsigned char)(table[63] - 63) << 8);
212+
const __m128i offsets = _mm_or_si128(
213+
fixed_offsets, _mm_slli_si128(_mm_cvtsi32_si128(last), 11));
208214
Py_ssize_t done = 0;
209215

210216
for (; in_len - done >= 16; done += 12, out += 16) {
@@ -291,8 +297,12 @@ base64_encode_fast(const unsigned char *in, Py_ssize_t in_len,
291297
{
292298
Py_ssize_t done = 0;
293299
#ifdef BASE64_HAVE_SSSE3
294-
if (table == table_b2a_base64 && in_len >= 16 && base64_cpu_has_ssse3()) {
295-
done = base64_encode_ssse3(in, in_len, out);
300+
if (in_len >= 16
301+
&& (table == table_b2a_base64
302+
|| memcmp(table, table_b2a_base64, 62) == 0)
303+
&& base64_cpu_has_ssse3())
304+
{
305+
done = base64_encode_ssse3(in, in_len, out, table);
296306
in += done;
297307
out += done / 3 * 4;
298308
in_len -= done;

0 commit comments

Comments
 (0)