Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seemingly slow capturing of int in closures #8366

Open
stinos opened this issue Feb 28, 2022 · 2 comments
Open

Seemingly slow capturing of int in closures #8366

stinos opened this issue Feb 28, 2022 · 2 comments

Comments

@stinos
Copy link
Contributor

stinos commented Feb 28, 2022

Originally reported here: https://forum.micropython.org/viewtopic.php?f=2&t=12060; 'seemingly' because I'm not sure what exactly is slow. Reproduce with:

import time

def ret(arg):
    yield None

def test(klass=int, count=10000):
    for n in range(count):
        ret(klass())

def measure(what, name):
    start = time.ticks_ms()
    for _ in range(10):
        what()
    end = time.ticks_ms()
    print(name, time.ticks_diff(end, start))

for typ in [int, float, dict, list]:
    measure(lambda: test(typ), typ)

Which yields numbers like this on windows and unix ports and reportedly does the same on RP2/pico:

<class 'int'> 1090
<class 'float'> 18
<class 'dict'> 14
<class 'list'> 19

Given the extreme difference this seems to indicate an underlying problem.

@yjchun
Copy link
Contributor

yjchun commented Feb 28, 2022

Please note that async function is a generator function too. Replace ret() with async_ret() and will see identical results.

async def async_ret(arg):
    pass

This problem is actually more relevant in asyncio.

@dpgeorge
Copy link
Member

dpgeorge commented Mar 1, 2022

It looks like this has to do with the GC doing excessive work to find some memory to create the generator (calling ret). If klass() allocates memory then it's fast. If it doesn't allocate memory then it's slow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants