Skip to content

BUG: chr() leaks #4422

Description

@pmp-p
#
# to run with small heap eg -X heapsize=16384
#

try:
    import utime as time
except:
    import time

import gc
cnt = 1
last = 0

while cnt < 0x110000:
    time.sleep(.005)
    current = gc.mem_free()
    #GOOD print( '[%c]' % cnt , current - last, current )

    #BAD print( '{}'.format( chr(cnt) ), current - last, current )

    #BAD
    print( chr( cnt ), cnt , current - last, current )
    cnt +=1
    last = current
    gc.collect() #no effect maybe a useless volatile interned strings of length 1 somewhere ?

Activity

  1. pfalcon commented on Jan 22, 2019

    @pfalcon
    Contributor
    "".join([chr(x//3) for x in (261, 312, 291, 348, 117, 345, 96, 348, 312, 315, 345, 189)])
    
  2. pmp-p commented on Jan 22, 2019

    @pmp-p
    ContributorAuthor

    Title is explicit enough no ?
    run code, enjoy MemoryError after a while on any port, unix port is good enough for that or pycopy with a esp sized heap.

    edit / finally on a side note i spent 4 hours finding that and sent issue in a hurry before work, so you could give a 30 seconds to at least test the snippet instead of writing your own. if i'm wrong i will deeply apologize for wasting your precious time.

  3. pfalcon commented on Jan 22, 2019

    @pfalcon
    Contributor

    Look how much info can be packed into human words ;-).

    or pycopy with a esp sized heap.

    Good mentioning, because aggressive string interning introduced by @dpgeorge was one of the few patches I reverted (so far) in my fork.

    But otherwise, yeah, looking at mp_builtin_chr(), it interns unconditionally. A solution would be interning only ASCII stuff by default, allow to disable interning in optional cases (like this) altogether.

  4. pmp-p commented on Jan 22, 2019

    @pmp-p
    ContributorAuthor

    "unconditionally" => that may explain why my doubts probably left me speechless at first.

    • short strings should not be interned at all.
    • at worst, interning should be kept on MRU cache decisions not unconditionally.
    • at best interning really long strings should be paged to flash ( like arduino progmem ? ) on user request, maybe via a custom type or simply just with f"" ( like cpython f-strings syntax ) ?.
  5. pfalcon commented on Jan 22, 2019

    @pfalcon
    Contributor

    short strings should not be interned at all.

    Obviously the opposite. And quite obviously actually:

    • Shorter strings have a higher chance of reuse (and that's why they're interned at all).
    • Allocating short strings has highest overhead. E.g. for 1 useful char, a whole 16-byte alloc block is used, 93% wasted. Surely, not allocating them like that is better.
  6. pmp-p commented on Jan 22, 2019

    @pmp-p
    ContributorAuthor

    by short i meant len() < sizeof( hash + storage pointer )

  7. peterhinch commented on Jan 22, 2019

    @peterhinch
    Contributor

    See #2280.

  8. pmp-p commented on Jan 22, 2019

    @pmp-p
    ContributorAuthor

    thx for incrementing the reference counter ;)

  9. pmp-p commented on Jan 24, 2019

    @pmp-p
    ContributorAuthor
  10. changed the title [-]chr() leaks[/-] [+]BUG: chr() leaks[/+] on Jan 31, 2019
  11. added a commit that references this issue on Apr 8, 2021
    fc86475
  12. jonnor commented on Sep 19, 2024

    @jonnor
    Contributor

    No MemoryError when running this on Unix port with version v1.24.0-preview.276.g1897fe6227. But it seems to stop at

    154 0 11296
    
     156 0 11296
    

    which seems way to early?

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

    bugpy-coreRelates to py/ directory in source

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions