Skip to content

dir(obj) introspection is slow #10414

Open
@raquo

Description

@raquo

CircuitPython version and board name

- CircuitPython 9.2.8 @ Qt Py M0 (similar performance on 9.2.7)
- CircuitPython 9.2.8 @ ItsyBitsy M4 (have not tried other versions)

Code/REPL

import time
import supervisor

class Foo:
    pass

class Bar:
    a = 1
    b = 2
    c = 3
    d = 4
    e = 5
    f = 6
    g = 7
    h = 8
    i = 9
    j = 10

foo = Foo()

bar = Bar()

arr = [1, 2, 3, 4, 5]

dct = {"a": 1, "b": 2, "c": 3}

def measure(label, value):
    now = supervisor.ticks_ms()
    dir(value)
    diff = supervisor.ticks_ms() - now
    print("dir({}) time = {} ms".format(label, diff))

while True:
    time.sleep(1)
    print("\n")
    measure("123", 123)
    measure("\" \"", " ")
    measure("arr", arr)
    measure("dct", dct)
    measure("foo", foo)
    measure("Foo", Foo)
    measure("bar", bar)
    measure("Bar", Bar)

Behavior

CircuitPython 9.2.8 @ Qt Py M0:

dir(123) time = 7 ms
dir(" ") time = 10 ms
dir(arr) time = 9 ms
dir(dct) time = 9 ms
dir(foo) time = 31 ms
dir(Foo) time = 16 ms
dir(bar) time = 58 ms
dir(Bar) time = 30 ms

CircuitPython 9.2.8 @ ItsyBitsy M4:

dir(123) time = 4 ms
dir(" ") time = 9 ms
dir(arr) time = 6 ms
dir(dct) time = 7 ms
dir(foo) time = 14 ms
dir(Foo) time = 7 ms
dir(bar) time = 23 ms
dir(Bar) time = 12 ms

Description

Hello, I have a vague understanding that dir() needs to walk the class hierarchy to collect all the fields, but still I was not at all expecting it to take so long, for simple class instances like foo. I'm not sure if anything can be done to improve the speed, but figured I'd file the issue anyway in case it's a performance bug.

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions