ProjectNeura / LEADS

Enable your racing car with powerful, data-driven instrumentation, control, and analysis systems, all wrapped up in a gorgeous look.
https://leads.projectneura.org
Apache License 2.0
257 stars 54 forks source link

Improve the Efficiency of Calling the Super Method in `CallbackChain` #220

Closed ATATC closed 5 months ago

ATATC commented 5 months ago

Current algorithm:

def super(self, *args, **kwargs) -> None:
    cf = _currentframe().f_back
    while (cn := cf.f_code.co_name) == "super":
        cf = cf.f_back
    if self._chain:
        getattr(self._chain, cn)(*args, **kwargs)

In cases where self._chain is None, the while loop is purely an O(n) waste of time. Therefore, we propose an improvement here:

def super(self, *args, **kwargs) -> None:
    if not self._chain:
        return
    cf = _currentframe().f_back
    while (cn := cf.f_code.co_name) == "super":
        cf = cf.f_back
    getattr(self._chain, cn)(*args, **kwargs)