I looked into the implementation of b85encode (in _85encode). The memory problems are caused by the fact that internally copies of the data are made. The copies consist of small chuncks, where each chuck has the memory overhead of a python object.
First with words = struct.Struct('!%dI' % (len(b) // 4)).unpack(b). There is a iter_unpack method in the Struct class, but it does not work as expected.
And then with a list comprehension chunks = [ ... for word in words]. In the case padding=False we can replace this with a generator which would save memory. For padding=True we need to modify the last element of the list, which does not work for the generator.
The poor performance (compared to b64encode) is mainly due to the part (chars2[word // 614125] + chars2[word // 85 % 7225] + chars[word % 85]).
To make any significant gains an implementation in C seems the most worthwhile.
Activity
sobolevn commented on Jan 23, 2023
I can confirm, that this still happens on main:
Compare it to almost-instant 64 version (it uses binascii's C implementation):
eendebakpt commented on Feb 1, 2023
I looked into the implementation of b85encode (in _85encode). The memory problems are caused by the fact that internally copies of the data are made. The copies consist of small chuncks, where each chuck has the memory overhead of a python object.
The poor performance (compared to b64encode) is mainly due to the part (chars2[word // 614125] + chars2[word // 85 % 7225] + chars[word % 85]).
To make any significant gains an implementation in C seems the most worthwhile.
Improve performance of Ascii85 and base85
Add Ascii85 and base85 support to binascii
Add Ascii85 and base85 support to binascii
Merge branch 'main' into pythongh-101178-rework-base85
pythongh-101178: refactor base64.b85encode to be memory friendly
pythongh-101178: refactor base64.b85encode to be memory friendly
pythongh-101178: refactor base64.b85encode to be memory friendly
Merge remote-tracking branch 'upstream/main' into pythongh-101178-rew…
Add Ascii85, base85, and Z85 support to binascii
kangtastic commented on Apr 21, 2025
Pinging this issue in hopes of finding a reviewer for my PR gh-102753. It's been over two years 😅
1 remaining item
Merge branch 'main' into pythongh-101178-rework-base85
Merge branch 'main' into pythongh-101178-rework-base85
Merge branch 'main' into pythongh-101178-rework-base85
Merge branch 'main' into pythongh-101178-rework-base85
Merge branch 'main' into pythongh-101178-rework-base85
gh-101178: Add Ascii85, Base85, and Z85 support to binascii (GH-102753)
pythongh-101178: Fix possible integer overflow in Ascii85 encoder wit…
pythongh-101178: Add Ascii85, Base85, and Z85 support to binascii (py…
gh-101178: Fix possible integer overflow in Ascii85 encoder with wrap…
pythongh-101178: Fix possible integer overflow in Ascii85 encoder wit…
gh-101178: Fix UB in `binascii.b2a_ascii85` (#148166)
pythongh-101178: Add Ascii85, Base85, and Z85 support to binascii (py…
pythongh-101178: Fix possible integer overflow in Ascii85 encoder wit…
pythongh-101178: Fix UB in `binascii.b2a_ascii85` (python#148166)