Skip to content

Add a (somewhat simple-minded) cache to the Python 2 version too. - #291

Merged
gvanrossum merged 2 commits into
masterfrom
py2cache
Oct 5, 2016
Merged

gvanrossum merged 2 commits into
masterfrom
py2cache

Conversation

@gvanrossum

@gvanrossum gvanrossum commented Oct 4, 2016 •

Copy link
Copy Markdown
Member

Fixes #290. Timing example:

Without cache:

$ python2 -m timeit -s 'import typing' 'typing.List[typing.List[str]]'
10000 loops, best of 3: 542 usec per loop

With cache:

$ python2 -m timeit -s 'import typing' 'typing.List[typing.List[str]]'
1000000 loops, best of 3: 1.65 usec per loop

That's about a 300x speedup!

@gvanrossum

Copy link
Copy Markdown
Member Author

@ilevkivskyi Do you see anything wrong with this?

Comment thread python2/typing.py Outdated
AnyStr = TypeVar('AnyStr', bytes, unicode)


def _tp_cache(func, maxsize=128):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A small comment: With this definition we could not later write:

@_tp_cache(maxsize=200)
def __getitem__(self, parameters):
    ...

This causes an error (function takes at least 1 argument...). I would propose to either make maxsize a constant MAXSIZE or use the decorator with args pattern here.

Except for this small comment, everything LGTM.

@gvanrossum

Copy link
Copy Markdown
Member Author

It's not tunable in the Python 3 version either, so I just made it a local.

@gvanrossum
gvanrossum merged commit 2dbf3fd into master Oct 5, 2016
@gvanrossum
gvanrossum deleted the py2cache branch October 14, 2016 21:58
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

Successfully merging this pull request may close these issues.

2 participants