-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Define a method for hash(::Type, ::UInt)
#49636
Conversation
Currently, `hash(::Type, ::UInt)` uses `objectid`, which can have some odd behavior for types: in particular, subsequent identical type-valued variable definitions can have `objectid`s which differ from the first such definition. This has some bizarre downstream effects when e.g. using types as the values of a `Set` or the keys of a `Dict`. See issue 49620 for examples. There is an internal `type_hash` C function used for caching types but isn't exposed to Julia, as Jameson pointed out in the linked issue. This commit exposes it as `jl_type_hash` which is then used via `ccall` to define a method `hash(::Type, ::UInt)`. This method then fixes #49620. Note, however, that this does not affect the differing `objectid`s for otherwise identical types.
error looks unrelated (to this PR)
|
Marking for backport to 1.6 and 1.9 since it's a bugfix but feel free to remove the labels if it's not appropriate to backport due to the addition to the C API |
I thought the C API for this was fairly recent, but if not, it seems worthwhile to backport |
Currently, `hash(::Type, ::UInt)` uses `objectid`, which can have some odd behavior for types: in particular, subsequent identical type-valued variable definitions can have `objectid`s which differ from the first such definition. This has some bizarre downstream effects when e.g. using types as the values of a `Set` or the keys of a `Dict`. See issue 49620 for examples. There is an internal `type_hash` C function used for caching types but isn't exposed to Julia, as Jameson pointed out in the linked issue. This commit exposes it as `jl_type_hash` which is then used via `ccall` to define a method `hash(::Type, ::UInt)`. This method then fixes #49620. Note, however, that this does not affect the differing `objectid`s for otherwise identical types. (cherry picked from commit 633d1ae)
If it is then how do we fix #49620 for prior versions? |
Is this intended: julia> typeof(a)
Destroy{FockSpace{Symbol}, Symbol, Int64, Nothing}
julia> typeof(a')
Create{FockSpace{Symbol}, Symbol, Int64, Nothing}
julia> hash(a)
0xc35039ec05f5ae3b
julia> hash(a')
0xc35039ec05f5ae3b
julia> hash(a) == hash(a')
true It causes https://github.com/qojulia/QuantumCumulants.jl/blob/7a00cbbdbc3c702668689900f96303607ed65c6a/test/test_fock.jl#LL8C1-L13C34 to fail with this backported. |
I assume not, and I'm not sure why that's happening. @vtjnash, any ideas? |
It is not wrong, but is fairly unexpected. We might not be bixmixing with the TypeName hash enough? |
I droped this PR from backports 1.9 until the effects are better understood (#49975). |
This triggers an annoying bug when building StatsBase docs (#49620) so it would be nice to find a way to backport it. |
Currently,
hash(::Type, ::UInt)
usesobjectid
, which can have some odd behavior for types: in particular, subsequent identical type-valued variable definitions can haveobjectid
s which differ from the first such definition. This has some bizarre downstream effects when e.g. using types as the values of aSet
or the keys of aDict
. See #49620 for examples.There is an internal
type_hash
C function used for caching types but isn't exposed to Julia, as Jameson pointed out in the linked issue. This commit exposes it asjl_type_hash
which is then used viaccall
to define a methodhash(::Type, ::UInt)
. This method then fixes #49620. Note, however, that this does not affect the differingobjectid
s for otherwise identical types.I've marked this as a bugfix and notably the linked issue is present at least as far back as Julia 1.6, so it could be worth backporting to
release-1.6
,release-1.9
, and perhapsrelease-1.8
if we're going to do any more of those. I'm not sure whether that's allowed though for changes that add to the C API.