GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — base64.b85encode uses significant amount of RAM · Issue #101178 · python/cpython

base64.b85encode uses significant amount of RAM #101178

New issue
Copy link
New issue
Copy link
Closed
#102753
Closed
base64.b85encode uses significant amount of RAM#101178
#102753
Copy link
Labels
performancePerformance or resource usagePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@AbdealiLoKo
AbdealiLoKo
opened on Jan 20, 2023
Issue body actions

Bug report

On the same string:

  • b64encode: RAM: 604MB, CPU: 79%, Time taken: 1.18sec and gave a result
  • b85encode: RAM: 6.9GB, CPU: 64%, Time taken: 45sec and crashed due to insufficient RAM (most likely)

b85encode takes up my entire RAM and crashes

On IPython:

In [1]: import base64

In [2]: contents = b'a' * 250 * 1024 * 1024

In [3]: %time len(base64.b64encode(contents).decode("ascii")) / 1024 / 1024
CPU times: user 489 ms, sys: 414 ms, total: 904 ms
Wall time: 974 ms
Out[3]: 333.33333587646484

In [4]: %time len(base64.b85encode(contents).decode("ascii")) / 1024 / 1024
Killed

Here is the GNU time stats:

$ /usr/bin/time /opt/python3.9/bin/python -c "import base64; print(len(base64.b64encode(b'a' * 250 * 1024 * 1024)) / 1024 / 1024)"
333.33333587646484
0.55user 0.39system 0:01.18elapsed 79%CPU (0avgtext+0avgdata 604008maxresident)k
0inputs+0outputs (0major+151155minor)pagefaults 0swaps

$ /usr/bin/time /opt/python3.9/bin/python -c "import base64; print(len(base64.b85encode(b'a' * 250 * 1024 * 1024)) / 1024 / 1024)"
26.08user 3.39system 0:45.86elapsed 64%CPU (0avgtext+0avgdata 6966080maxresident)k
512inputs+0outputs (2major+1756895minor)pagefaults 0swaps

I have gotten same results in Python 3.6, 3.7, 3.8, 3.9

Your environment

  • CPython versions tested on: Python 3.9 installed with miniconda
  • Operating system and architecture: CentOS 7, AWS t3.large machine

Linked PRs

  • gh-101178: Add Ascii85, Base85, and Z85 support to binascii #102753
  • gh-101178: refactor base64.b85encode to be memory friendly #112248
  • gh-101178: Fix possible integer overflow in Ascii85 encoder with wrapcol=1 #144778
  • gh-101178: Fix UB in binascii.b2a_ascii85 #148166

Activity

Next
  1. AbdealiLoKo
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Jan 20, 2023
  2. mdboom
    added
    performancePerformance or resource usagePerformance or resource usage
    on Jan 20, 2023
  3. sobolevn commented on Jan 23, 2023

    @sobolevn
    sobolevn
    on Jan 23, 2023
    Member
    More actions

    I can confirm, that this still happens on main:

    » time ./python.exe -c "import base64; print(len(base64.b85encode(b'a' * 250 * 1024 * 1024)) / 1024 / 1024)"
    312.5
    ./python.exe -c   160.32s user 74.28s system 87% cpu 4:29.26 total
    

    Compare it to almost-instant 64 version (it uses binascii's C implementation):

    » time ./python.exe -c "import base64; print(len(base64.b64encode(b'a' * 250 * 1024 * 1024)) / 1024 / 1024)"
    333.33333587646484
    ./python.exe -c   0.91s user 0.26s system 98% cpu 1.187 total
    
  4. eendebakpt commented on Feb 1, 2023

    @eendebakpt
    eendebakpt
    on Feb 1, 2023
    Contributor
    More actions

    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.

  5. kangtastic
    added 4 commits that reference this issue on Mar 13, 2023

    Improve performance of Ascii85 and base85

    This issue will close once commit 41c613c is merged into the 'main' branch.
    41c613c

    Add Ascii85 and base85 support to binascii

    This issue will close once commit 880b548 is merged into the 'main' branch.
    880b548

    Add Ascii85 and base85 support to binascii

    This issue will close once commit b50cdb1 is merged into the 'main' branch.
    b50cdb1

    Merge branch 'main' into pythongh-101178-rework-base85

    This issue will close once commit 9814dab is merged into the 'main' branch.
    9814dab
  6. romuald
    added 2 commits that reference this issue on Nov 18, 2023

    pythongh-101178: refactor base64.b85encode to be memory friendly

    389638b

    pythongh-101178: refactor base64.b85encode to be memory friendly

    4a300d7
  7. iritkatriel
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Nov 26, 2023
  8. romuald
    added a commit that references this issue on Mar 3, 2024

    pythongh-101178: refactor base64.b85encode to be memory friendly

    8770f87
  9. kangtastic
    added a commit that references this issue on Mar 19, 2024

    Merge remote-tracking branch 'upstream/main' into pythongh-101178-rew…

    d5a726f
  10. kangtastic
    added a commit that references this issue on Apr 21, 2025

    Add Ascii85, base85, and Z85 support to binascii

    This issue will close once commit 05ae5ad is merged into the 'main' branch.
    05ae5ad
  11. kangtastic commented on Apr 21, 2025

    @kangtastic
    kangtastic
    on Apr 21, 2025
    Contributor
    More actions

    Pinging this issue in hopes of finding a reviewer for my PR gh-102753. It's been over two years 😅

  12. 1 remaining item

  13. serhiy-storchaka
    added a commit that references this issue on Dec 25, 2025

    Merge branch 'main' into pythongh-101178-rework-base85

    a070887
  14. kangtastic
    added a commit that references this issue on Dec 28, 2025

    Merge branch 'main' into pythongh-101178-rework-base85

    dc1d3fc
  15. serhiy-storchaka
    added a commit that references this issue on Jan 14, 2026

    Merge branch 'main' into pythongh-101178-rework-base85

    3582492
  16. kangtastic
    added a commit that references this issue on Jan 18, 2026

    Merge branch 'main' into pythongh-101178-rework-base85

    74f6ceb
  17. serhiy-storchaka
    added 2 commits that reference this issue on Feb 6, 2026

    Merge branch 'main' into pythongh-101178-rework-base85

    4ba3e50

    gh-101178: Add Ascii85, Base85, and Z85 support to binascii (GH-102753)

    45d4a34
  18. serhiy-storchaka
    closed this as completedin #102753on Feb 6, 2026
  19. serhiy-storchaka
    added a commit that references this issue on Feb 13, 2026

    pythongh-101178: Fix possible integer overflow in Ascii85 encoder wit…

    8e20fed
  20. thunder-coding
    added a commit that references this issue on Feb 15, 2026

    pythongh-101178: Add Ascii85, Base85, and Z85 support to binascii (py…

    a7a1b29
  21. serhiy-storchaka
    added a commit that references this issue on Feb 24, 2026

    gh-101178: Fix possible integer overflow in Ascii85 encoder with wrap…

    b32c830
  22. brijkapadia
    added a commit that references this issue on Feb 28, 2026

    pythongh-101178: Fix possible integer overflow in Ascii85 encoder wit…

    eec5555
  23. StanFromIreland
    added a commit that references this issue on Apr 7, 2026

    gh-101178: Fix UB in `binascii.b2a_ascii85` (#148166)

    63d3c49
  24. ljfp
    added 3 commits that reference this issue on Apr 25, 2026

    pythongh-101178: Add Ascii85, Base85, and Z85 support to binascii (py…

    7e8406a

    pythongh-101178: Fix possible integer overflow in Ascii85 encoder wit…

    e43afd6

    pythongh-101178: Fix UB in `binascii.b2a_ascii85` (python#148166)

    20ec1a7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance or resource usagePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.