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

Cache 'redbaron_classname_to_baron_type', giving ~30% performance boost on 'find_all' heavy code. #158

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion redbaron/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
def baron_type_to_redbaron_classname(baron_type):
return "".join(map(lambda x: x.capitalize(), baron_type.split("_"))) + "Node"


from functools import lru_cache

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imports should be top level

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

functools.lru_cache is python3 only and will not work in python2. It might be easy to write a cache decorator with no max size:

def memoize(function):
    memo = {}

    @wraps(function)
    def wrapper(*args):
        if args in memo:
            return memo[args]
        else:
            rv = function(*args)
            memo[args] = rv
            return rv
    return wrapper

@lru_cache(maxsize=None)
def redbaron_classname_to_baron_type(name):
name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name.replace("Node", ""))
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower()
Expand Down