lebrice / SimpleParsing

Simple, Elegant, Typed Argument Parsing with argparse
MIT License
410 stars 52 forks source link

Performance degradation between 0.0.* and 0.1.* releases when using lots of subparsers #249

Closed lebrice closed 1 year ago

lebrice commented 1 year ago

Describe the bug There was a great decrease in performance between the 0.0.21 and 0.1.*. Trying to figure out why.

This is very noticeable when there are many levels of subparses, and lots of options on each level (e.g. in Sequoia, there are about 20 * 12 different subparser combinations across 2-3 levels)

zhiruiluo commented 1 year ago

By adding lru_cache wrapper for inspect.getsource, the _get_attribute_docstring will reduce more than half of the cumulative time.

@functools.lru_cache(2048)
def my_getsource(obj: object):
    return inspect.getsource(obj)

@functools.lru_cache(2048)
def _get_attribute_docstring(dataclass: type, field_name: str) -> AttributeDocString | None:
    """Gets the AttributeDocString of the given field in the given dataclass.
    Doesn't inspect base classes.
    """
    try:
        source = my_getsource(dataclass)

Before adding lru_cache for inspect.getsource:

ncalls  tottime  percall  cumtime percall filename:lineno(function)
2313    0.039    0.000   11.788    0.005 ~/SimpleParsing/simple_parsing/docstring.py:99(_get_attribute_docstring)

After adding lru_cache for inspect.getsource:

ncalls  tottime  percall  cumtime percall filename:lineno(function)
2313    0.036    0.000    4.794    0.002 ~/SimpleParsing/simple_parsing/docstring.py:103(_get_attribute_docstring))