Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge branch 'main' into base64-encode-wrapcol
  • Loading branch information
serhiy-storchaka committed Jan 2, 2026
commit bb5ff5755617da17ffe3e828b893c0e5fe4bc277
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,13 @@ Optimizations
(Contributed by Chris Eibl, Ken Jin, and Brandt Bucher in :gh:`143068`.
Special thanks to the MSVC team including Hulon Jenkins.)

base64 & binascii
-----------------

* CPython's underlying base64 implementation now encodes 2x faster and decodes 3x
faster thanks to simple CPU pipelining optimizations.
(Contributed by Gregory P. Smith and Serhiy Storchaka in :gh:`143262`.)

csv
---

Expand Down
27 changes: 15 additions & 12 deletions Modules/binascii.c
Original file line number Diff line number Diff line change
Expand Up @@ -659,9 +659,6 @@ binascii_b2a_base64_impl(PyObject *module, Py_buffer *data, size_t wrapcol,
int newline)
/*[clinic end generated code: output=2edc7311a9515eac input=2ee4214e6d489e2e]*/
{
int leftbits = 0;
unsigned int leftchar = 0;

const unsigned char *bin_data = data->buf;
Py_ssize_t bin_len = data->len;
assert(bin_len >= 0);
Expand Down Expand Up @@ -703,18 +700,24 @@ binascii_b2a_base64_impl(PyObject *module, Py_buffer *data, size_t wrapcol,
ascii_data += (fast_bytes / 3) * 4;
bin_len -= fast_bytes;

/* See if there are 6-bit groups ready */
while ( leftbits >= 6 ) {
unsigned char this_ch = (leftchar >> (leftbits-6)) & 0x3f;
leftbits -= 6;
*ascii_data++ = table_b2a_base64[this_ch];
}
}
if ( leftbits == 2 ) {
*ascii_data++ = table_b2a_base64[(leftchar&3) << 4];
/* Handle remaining 0-2 bytes */
if (bin_len == 1) {
/* 1 byte remaining: produces 2 base64 chars + 2 padding */
unsigned int val = bin_data[0];
*ascii_data++ = table_b2a_base64[(val >> 2) & 0x3f];
*ascii_data++ = table_b2a_base64[(val << 4) & 0x3f];
*ascii_data++ = BASE64_PAD;
*ascii_data++ = BASE64_PAD;
}
else if (bin_len == 2) {
/* 2 bytes remaining: produces 3 base64 chars + 1 padding */
unsigned int val = ((unsigned int)bin_data[0] << 8) | bin_data[1];
*ascii_data++ = table_b2a_base64[(val >> 10) & 0x3f];
*ascii_data++ = table_b2a_base64[(val >> 4) & 0x3f];
*ascii_data++ = table_b2a_base64[(val << 2) & 0x3f];
*ascii_data++ = BASE64_PAD;
}

if (wrapcol) {
unsigned char *start = PyBytesWriter_GetData(writer);
ascii_data = start + wraplines(start, ascii_data - start, wrapcol);
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.