-
-
Notifications
You must be signed in to change notification settings - Fork 554
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
cached_function and cached_method for unhashable elements #16316
Comments
Branch: u/saraedum/ticket/16316 |
Dependencies: #16251 |
Author: Julian Rueth |
New commits:
|
Commit: |
comment:5
Your proposal is not nearly as scary as your description would indicate. We do not want to cache unhashable things (in fact, we can't). Instead what you're doing is creating a generic method I'm thinking we should also do automatic conversions for mutable python objects such as Simon, do you have any thoughts on this? |
comment:8
It seems that the problem really has to do with key comparison in dictionaries, not with hashing or caching specifically. Given (1) Python's use of One would like a third comparison operator, say
1 I just looked up the different comparison functions ( |
comment:9
Replying to @pjbruin:
This is not really the topic of the ticket, but I do not agree. It depends on what you want to do. If you have sufficient control over which elements are put in the dictionary, then you can use Sage objects. Of course you are right, that our
Such an operator might be helpful and would really fix trouble with caching. Then again, when are two objects
Yes and no. The idea is really to give elements a
I did this on purpose. I replicate the current behaviour in sage, i.e., if you mix elements with different parents in one dictionary, then you really need to know what you are doing. Why not include the parent? Well, are two parents equal if they are I have no strong opinion on whether or not to include the parent but I think it can be more confusing than helpful. Also keys might unnecessarily be rather large. If your application will only get elements from a single parent but you store all the data needed to create the extension field with every element in a cache, that could be really a waste. We could of course only store |
comment:10
Replying to @saraedum:
Sorry if this sounded off-topic; I was thinking of the dictionaries used in caching, but didn't focus on that because I thought my "objections" were applicable to dictionaries in Sage in general. I also wanted to avoid focussing on hashing because actually I don't fully understand what the criteria are for objects to be hashable. The only thing that is clear to me is that if the "mathematical meaning" can change over time (e.g. if the object is a list whose length can change, or a matrix on which you can perform row/column operations), then the object should not be hashable. But what about p-adic elements? These are supposedly fixed in time. Why exactly do we want elements of
Sure, but
I would say the only option (for caching purposes) is to make two elements
Actually that was the option I was thinking of; it should be fast and won't waste too much memory. Alternatively, there could be both an "internal" |
comment:11
Replying to @pjbruin:
Right, there is no mathematical justification for
Absolutely.
I see. I guess this means that we should at least have that if
Ok.
Imho, I'll push a new commit which implements the inclusion of the parent. Thanks for joining this discussion btw. |
Reviewer: Peter Bruin |
Changed branch from u/saraedum/ticket/16316 to u/pbruin/16316-cache_key |
comment:14
You must change the wording of "caching elements which are unhashable"; this is not correct as you cannot cache (i.e. set as the key in a dict) anything without a (well-defined) hash. Instead say something like
|
comment:15
Replying to @tscrim:
I don't think that the word "cache" is necessarily connected to "hashing". A hashtable is one way of caching but there are others. (I just checked, https://en.wikipedia.org/wiki/Cache_%28computing%29 does not contain the word 'hash'.) Is it acceptable for you if we leave the current wording? |
Changed branch from u/tscrim/ticket/16316 to u/saraedum/ticket/16316 |
comment:36
Replying to @saraedum:
It's a private method only used in one place, so the chances of someone finding it randomly are very slim, but okay, it doesn't really matter.
Whoops, forgot about those.
Restoring your branch so I can look again. New commits:
|
comment:37
Ah, now I see why you're trying to hash every time inside of the function I'm also not in favor of raising an error in So overall I feel like this is a heavy-handed approach. However I don't have a strong enough use case to oppose this getting in as is. So positive review. |
comment:38
Replying to @pjbruin:
No, this isn't a problem because equal parents have equal hashes. |
comment:39
Replying to @simon-king-jena:
Ah right. Then perhaps I was thinking a
Hmm...is that an issue with the extension class not necessarily having a |
comment:40
Replying to @tscrim:
That is exactly what causes the problem. Consider the following example with a hypothetical parent class
This already happens before this patch with parent P and Q where there exists a coercion map compatible with hashing (e.g. Z and Q):
However, I think we should avoid this situation whenever possible. |
comment:41
Oh that's subtle. However this usually (almost always?) won't be a problem because the coercion, which allowed the elements to compare as equal, will step in to make the arithmetic work. I would think equal-but-not-identical parents should have coercions between themselves too. |
comment:42
Documentation doesn't build
|
comment:43
Note: seems to only fail on UW mod |
comment:44
Doctests fail everywhere
possibly due to the dependent ticket #16317 |
comment:45
So actual objects and cache keys go into the same dictionary? Isn't that dangerous, one person's key could easily be another one's object? Shouldn't it be different dicts or some special object to hold the key? |
comment:46
Replying to @pjbruin:
As you wrote, this is not a new problem. It does not seem to be related to unhashable elements. Mixing non-unique parents and caching will simply lead to trouble. |
comment:47
Replying to @vbraun:
Though this is very unlikely it is a valid point. I will take care of it. |
Changed branch from u/saraedum/ticket/16316 to |
Caching does currently not work for unhashable elements
In this case
a
should not be hashable since its current operator==
couldnever be made consistent with a non-trivial hash value (cf. #11895).
However, such elements should be cacheable.
This ticket adds a
_cache_key()
for such elements which can be used to hash andcompare unhashable elements. That method should then return a tuple (or
anything else that is hashable) which uniquely identifies the element, e.g. for
p-adics the precision and a lift of the element to the integers.
Depends on #16251
CC: @simon-king-jena
Component: misc
Author: Julian Rueth
Branch/Commit:
aa037de
Reviewer: Peter Bruin, Travis Scrimshaw
Issue created by migration from https://trac.sagemath.org/ticket/16316
The text was updated successfully, but these errors were encountered: