Skip to content

Commit c55000a

Browse files
authored
add a hash value to Typeofwrapper objects (#49725)
We probably should not do this in full correctness, but the performance gain is too great to ignore.
1 parent 78fbf1b commit c55000a

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/jltypes.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1672,11 +1672,14 @@ void jl_precompute_memoized_dt(jl_datatype_t *dt, int cacheable)
16721672
}
16731673
assert(dt->isconcretetype || dt->isdispatchtuple ? dt->maybe_subtype_of_cache : 1);
16741674
if (dt->name == jl_type_typename) {
1675-
cacheable = 0; // n.b. the cache for Type ignores parameter normalization, so it can't be used to make a stable hash value
16761675
jl_value_t *p = jl_tparam(dt, 0);
16771676
if (!jl_is_type(p) && !jl_is_typevar(p)) // Type{v} has no subtypes, if v is not a Type
16781677
dt->has_concrete_subtype = 0;
16791678
dt->maybe_subtype_of_cache = 1;
1679+
jl_value_t *uw = jl_unwrap_unionall(p);
1680+
// n.b. the cache for Type ignores parameter normalization except for Typeofwrapper, so it can't be used to make a stable hash value
1681+
if (!jl_is_datatype(uw) || ((jl_datatype_t*)uw)->name->wrapper != p)
1682+
cacheable = 0;
16801683
}
16811684
dt->hash = typekey_hash(dt->name, jl_svec_data(dt->parameters), l, cacheable);
16821685
}

test/hashing.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,11 @@ let t1 = Tuple{AbstractVector,AbstractVector{<:Integer},UnitRange{<:Integer}},
302302
@test hash(t1) == hash(t2)
303303
@test length(Set{Type}([t1, t2])) == 1
304304
end
305+
306+
struct AUnionParam{T<:Union{Nothing,Float32,Float64}} end
307+
@test AUnionParam.body.hash == 0
308+
@test Type{AUnionParam}.hash != 0
309+
@test Type{AUnionParam{<:Union{Float32,Float64}}}.hash == 0
310+
@test Type{AUnionParam{<:Union{Nothing,Float32,Float64}}} === Type{AUnionParam}
311+
@test Type{AUnionParam.body}.hash == 0
312+
@test Type{Base.Broadcast.Broadcasted}.hash != 0

0 commit comments

Comments
 (0)