Skip to content

gh-118761: substitute re import in base64.b16decode for a more efficient alternative #128736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,15 @@ asyncio
reduces memory usage.
(Contributed by Kumar Aditya in :gh:`107803`.)


base64
------

* Improve the performance of :func:`base64.b16decode` by up to ten times,
and reduce the import time of :mod:`base64` by up to six times.
(Contributed by Bénédikt Tran, Chris Markiewicz, and Adam Turner in :gh:`118761`.)


io
---
* :mod:`io` which provides the built-in :func:`open` makes less system calls
Expand Down
3 changes: 1 addition & 2 deletions Lib/base64.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# Modified 30-Dec-2003 by Barry Warsaw to add full RFC 3548 support
# Modified 22-May-2007 by Guido van Rossum to use bytes everywhere

import re
import struct
import binascii

Expand Down Expand Up @@ -284,7 +283,7 @@ def b16decode(s, casefold=False):
s = _bytes_from_decode_data(s)
if casefold:
s = s.upper()
if re.search(b'[^0-9A-F]', s):
if s.translate(None, delete=b'0123456789ABCDEF'):
raise binascii.Error('Non-base16 digit found')
return binascii.unhexlify(s)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Improve the performance of :func:`base64.b16decode` by up to ten times
by more efficiently checking the byte-string for hexadecimal digits.
Reduce the import time of :mod:`base64` by up to six times,
by no longer importing :mod:`re`.
Patch by Bénédikt Tran, Chris Markiewicz, and Adam Turner.
Loading