Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Enable comparison of C-like enums by identity #3061
base: main
Are you sure you want to change the base?
Enable comparison of C-like enums by identity #3061
Changes from 3 commits
7592d1d
7e8b9df
286ee1e
42b0a4e
82353cc
c3f628b
b32ba3e
ede1897
50cab8a
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use of
GILOnceCell
seems reasonable, however at the same time if we ever want to have a stab at #2274 (which is admittedly a long way off) then it'd be better to not introduce new statics containing Python objects.I think an alternative implementation could look up variants as attributes from the enum's Python class object, however I think there's potentially a chicken-or-egg problem of how we create those attributes (as I think that currently uses
IntoPy
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think given that we're still no closer to solving the statics problem, and it'll require coming up with new patterns which will probably apply to all statics equally, let's just stick with a static
GILOnceCell
for now and leave migration off that for the future 😆There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason to use a
HashMap
here instead of a fixed-size array?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
#cls::#variant_name as usize
could be sparse, e.g.so while we could store those as sorted pairs in a
[(usize, Py<#cls>); 2]
, we would need to do a binary search for the variants instead of a hash table look-up as direct access into a[(usize, Option<Py<#cls>>); 1000000]
seems wasteful.Personally, I see the simplicity of using the hash table but would probably opt for the sorted array in a follow-up. (But let's see whether this lands using the
static
s at all.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The discriminant can indeed be sparse which is also tested by
test_custom_discriminant_comparison_by_identity
. But using binary search in a sorted array is definitely a possible replacement for the hashmap.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can know from parsing the enum whether it's dense or sparse?
For the dense case we can just index into an array, no binary search needed, and for the sparse case we can generate a
match
in front to convert into a dense set of indices first.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would really be nice as the compiler is pretty clever in how to handle
match
AFAIK, e.g. producing linear or binary searches depending on cardinality estimates.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I noticed a test above
test_enum_class_attr
with the following line:This is potentially a problem; that is a new instance of
MyEnum
type and so it won't compare successfully with identity.Now, the question is, is that a good thing? Pro could be that it gives users who need it an escape hatch. However, it is probably also a footgun.
Unfortunately, changing how
Py::new
works would require a refactoring of our initialization machinery. I'm sure there's plenty of scope for improvement in that area (e.g. #2384), however it would likely be a much bigger patch than this PR...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that this possibility is a footgun which we should prevent. Maybe we should put this a bit on hold until the scope for changes to
Py::new
is defined?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By "this on hold" do you mean solving this particular problem or the whole PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant the whole pull request but I don't know what the best approach would be. It seems that comparing enums by identity is a desired feature but there are also two concerns #3061 (comment) and #3061 (comment) that would both require quite a refactor. Also happy to help in that regard but I don't know a lot about the existing problems and where to start. But it seems that it might not be the right time to make this change as I can imagine you don't want to complicate that work further by including some changes now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we've just had #3287 merged which might be enough of a tweak to make this possible. Given that
Py::new
relies onInto<PyClassInitializer<T>>
we might be able to adjust that trait implementation for enums to somehow usePyClassInitializerImpl::Existing
(by fetching the value from aGILOnceCell
which is itself initialized by aPyClassInitializerImpl::New
invocation toPy::new
.