8000 gh-118761: substitute `re` import in `base64.b16decode` for a more ef… · python/cpython@bbd3300 · GitHub
[go: up one dir, main page]

Skip to content

Commit bbd3300

Browse files
picnixzAA-Turnereffigieshugovk
authored
gh-118761: substitute re import in base64.b16decode for a more efficient alternative (#128736)
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Co-authored-by: Chris Markiewicz <effigies@gmail.com> Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
1 parent 859db49 commit bbd3300

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

Doc/whatsnew/3.14.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,15 @@ asyncio
728728
reduces memory usage.
729729
(Contributed by Kumar Aditya in :gh:`107803`.)
730730

731+
732+
base64
733+
------
734+
735+
* Improve the performance of :func:`base64.b16decode` by up to ten times,
736+
and reduce the import time of :mod:`base64` by up to six times.
737+
(Contributed by Bénédikt Tran, Chris Markiewicz, and Adam Turner in :gh:`118761`.)
738+
739+
731740
io
732741
---
733742
* :mod:`io` which provides the built-in :func:`open` makes less system calls

Lib/base64.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# Modified 30-Dec-2003 by Barry Warsaw to add full RFC 3548 support
55
# Modified 22-May-2007 by Guido van Rossum to use bytes everywhere
66

7-
import re
87
import struct
98
import binascii
109

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Improve the performance of :func:`base64.b16decode` by up to ten times
2+
by more efficiently checking the byte-string for hexadecimal digits.
3+
Reduce the import time of :mod:`base64` by up to six times,
4+
by no longer importing :mod:`re`.
5+
Patch by Bénédikt Tran, Chris Markiewicz, and Adam Turner.

0 commit comments

Comments
 (0)
0