https://github.com/python/cpython/commit/bbd3300ae82a71da483dbb0c345175ba090263c4
commit: bbd3300ae82a71da483dbb0c345175ba090263c4
branch: main
author: Bénédikt Tran <[email protected]>
committer: AA-Turner <[email protected]>
date: 2025-01-14T13:25:33Z
summary:

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

Co-authored-by: Adam Turner <[email protected]>
Co-authored-by: Chris Markiewicz <[email protected]>
Co-authored-by: Hugo van Kemenade <[email protected]>

files:
A Misc/NEWS.d/next/Library/2025-01-10-13-06-54.gh-issue-118761.f8oADD.rst
M Doc/whatsnew/3.14.rst
M Lib/base64.py

diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst
index 1152c7a90b7712..ac203b208f4a8a 100644
--- a/Doc/whatsnew/3.14.rst
+++ b/Doc/whatsnew/3.14.rst
@@ -728,6 +728,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
diff --git a/Lib/base64.py b/Lib/base64.py
index 61be4fb856e92c..5d78cc09f40cd3 100644
--- a/Lib/base64.py
+++ b/Lib/base64.py
@@ -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
 
@@ -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)
 
diff --git 
a/Misc/NEWS.d/next/Library/2025-01-10-13-06-54.gh-issue-118761.f8oADD.rst 
b/Misc/NEWS.d/next/Library/2025-01-10-13-06-54.gh-issue-118761.f8oADD.rst
new file mode 100644
index 00000000000000..37c25cb2efd034
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-01-10-13-06-54.gh-issue-118761.f8oADD.rst
@@ -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.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to