Visitar URL original
bpo-41220: Add make key lru cache by Itayazolay · Pull Request #21353 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20260221024243/https://github.com/python/cpython/pull/21353
Skip to content
/ cpython Public

Comments

bpo-41220: Add make key lru cache#21353

Closed
Itayazolay wants to merge 9 commits intopython:masterfrom
Itayazolay:add-make-key-lru_cache
Closed

bpo-41220: Add make key lru cache#21353
Itayazolay wants to merge 9 commits intopython:masterfrom
Itayazolay:add-make-key-lru_cache

Conversation

@Itayazolay
Copy link
Contributor

@Itayazolay Itayazolay commented Jul 6, 2020 •

bpo-41220: added additional make_key argument to lru_cache

I'd like to add optional argument to lru_cache.
This argument is a user given function that will replace the default behaviour of creating a key from the args/kwds of the function.

for example:

def my_make_key(my_list):
return my_list[0]

@lru_cache(128, make_key=my_make_key)
def cached_func(my_list):
return sum(my_list)

This will creating a cached function that accepts immutable.
Also, It will allow user to add custom functions from knowledge about the expected function input, without the need to create custom classes and/or overriding hash

https://bugs.python.org/issue41220

@Itayazolay Itayazolay requested a review from rhettinger as a code owner July 6, 2020 16:52
@Itayazolay Itayazolay changed the title Add make key lru cache bpo-41220: Add make key lru cache Jul 6, 2020
Copy link
Contributor

@bharel bharel left a comment •

Choose a reason for hiding this comment

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

Make sure to Py_Clear make_key on tp_clear.
Make sure you update the docs in C (see lru_cache_doc).
Make sure you update the news with blurb.

obj->root.next = &obj->root;
obj->wrapper = wrapper;
obj->typed = typed;
obj->make_key = make_key;
Copy link
Contributor

Choose a reason for hiding this comment

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

Will segfault. Py_INCREF the make_key func.

PyErr_SetString(PyExc_TypeError, "the make_key argument must be callable");
return NULL;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

If typed is passed and make_key exists, flow should be identical to the .py implementation.

@rhettinger rhettinger self-assigned this Jul 7, 2020
list = lru_cache_unlink_list(obj);
Py_XDECREF(obj->cache);
Py_XDECREF(obj->func);
if (obj->key != NULL)
Copy link
Contributor

Choose a reason for hiding this comment

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

no need to check for NULL

obj->wrapper = wrapper;
obj->typed = typed;
obj->make_key = make_key;
if (key != NULL)
Copy link
Contributor

Choose a reason for hiding this comment

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

Py_XINCREF, no need for NULL check

Py_CLEAR(self->cache);
Py_CLEAR(self->cache_info_type);
Py_CLEAR(self->dict);
if (self->key != NULL)
Copy link
Contributor

Choose a reason for hiding this comment

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

No need to check for NULL

Py_VISIT(self->cache);
Py_VISIT(self->cache_info_type);
Py_VISIT(self->dict);
//if (self->key != NULL)
Copy link
Contributor

Choose a reason for hiding this comment

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

commented?

PyErr_SetString(PyExc_TypeError, "the key argument must be callable");
return NULL;
}
if (key && typed){
Copy link
Contributor

Choose a reason for hiding this comment

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

No need to check key again

Copy link
Contributor

@bharel bharel left a comment

Choose a reason for hiding this comment

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

Awesome 👍

You're welcome to add yourself on MISC/ACKS.
Other than that, LGTM technical wise.
Conceptual wise, waiting for bpo decision.

Lib/functools.py Outdated

If *key* is a callable, it will be called with the given arguments of
function. It is expected to return a hashable object. If *key* is
provided, arguments of the function doesn't have to be hashable.
Copy link
Contributor

Choose a reason for hiding this comment

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

Grammar fix: 'don't need to be hashable'

Lib/functools.py Outdated
root = [] # root of the circular doubly linked list
root[:] = [root, root, None, None] # initialize by pointing to self

make_key = key # a user defined function of to make key from args.
Copy link
Contributor

@bharel bharel Jul 9, 2020 •

Choose a reason for hiding this comment

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

make_key = _make_key if key is None else key

Copy link
Contributor Author

@Itayazolay Itayazolay Jul 9, 2020 •

Choose a reason for hiding this comment

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

I wanted to do that, the problem is that _make_key also accepts typed as an argument.
I would have used partial here, but I _make_key accepts args, kwargs as 2 paramters, and for user defined make_key I want to pass them as *args, **kwargs

Lib/functools.py Outdated
# Simple caching without ordering or size limit
nonlocal hits, misses
key = make_key(args, kwds, typed)
if make_key:
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove key checks

Lib/functools.py Outdated
# Size limited caching that tracks accesses by recency
nonlocal root, hits, misses, full
key = make_key(args, kwds, typed)
if make_key:
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove key checks

@Itayazolay
Copy link
Contributor Author

@bharel Thanks for your CR (and mental support), really appreciate it 😄
I found a way to remove the if key.... I would like to do it also for the C implementation.
Do you have an idea of how (or if I should)?

@bharel
Copy link
Contributor

bharel commented Jul 10, 2020 •

@bharel Thanks for your CR (and mental support), really appreciate it 😄
I found a way to remove the if key.... I would like to do it also for the C implementation.
Do you have an idea of how (or if I should)?

The only way to maybe do it is by converting the existing implementation to direct funcproc, or vectorcall.
The amount of saving you'll receive is close to 0. In fact, I actually think it'll be slower, so no need for that. Keep it simple here 😉

@rhettinger
Copy link
Contributor

Closing this for the reasons listed in the BPO.

@bharel Thank for doing a review. I think we need to have some way to forestall working out the small details before we even decide when the idea itself is acceptable, even in its most perfect form.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants