Compiler: Skip the union-split subtype query for fully covering matches #65251
Approved these changes.
Approved these changes.
Somewhat unbelievable that an pointer equality comparison could be 1.6% of the instructions, but sure
Are you saying that the subtype check there is always a pointer equality comparison? Will look into that, because it means my instruction measuring setup must be completely wrong.
So when I instrument the call you are talking about I get that 10,519 of 75,421 queries are resolved by a pointer equality check. Here are some queries that are not (which are executed in the workload I measured on):
┌───────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┬────────────┐
│ Count │ Query │ Instr each │
├───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────────┤
│ 1275 │ Tuple{typeof(==), Int64, Int64} <: Tuple{typeof(==), T, T} where T<:Number │ 3541 │
├───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────────┤
│ 979 │ Tuple{typeof(>), Int64, Int64} <: Tuple{typeof(>), Any, Any} │ 572 │
├───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────────┤
│ 973 │ Tuple{typeof(<=), Int64, Int64} <: Tuple{typeof(<=), T, T} where T<:Union{Int128, Int16, Int32, Int64, Int8} │ 3232 │
├───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────────┤
│ 617 │ Tuple{Core.TypeEgal{Int64}, Int64} <: Tuple{Type{Int64}, Union{Bool, Int128, …, UInt8}} │ 6245 │
├───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────────┤
│ 601 │ Tuple{typeof(iterate), UnitRange{Int64}, Int64} <: Tuple{typeof(iterate), OrdinalRange{T}, Any} where T │ 4773 │
├───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────────┤
│ 490 │ Tuple{typeof(iterate), UnitRange{Int64}} <: Tuple{typeof(iterate), OrdinalRange} │ 4596 │
├───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────────┤
│ 354 │ Tuple{typeof(getindex), Vector{Int64}, Int64} <: Tuple{typeof(getindex), Array, Int64} │ 6020 │
├───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────────┤
│ 192 │ Tuple{typeof(getindex), Memory{UInt8}, Int64} <: Tuple{typeof(getindex), GenericMemory, Int64} │ 8917 │
├───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼────────────┤
│ 94 │ Tuple{Core.TypeEgal{NamedTuple{(:sizehint,)}}, Tuple{Int64}} <: Tuple{Type{NamedTuple{names}}, Tuple} where names │ 21193 │
└───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴────────────┘
---
Tallying up things (for covering queries):
┌──────────────────────────────────┬──────────────────┬─────────────────┬───────────────┐
│ Shape │ Share of queries │ Avg instr/query │ Share of cost │
├──────────────────────────────────┼──────────────────┼─────────────────┼───────────────┤
│ Pointer-equal │ 14.0% │ 108 │ 0.4% │
├──────────────────────────────────┼──────────────────┼─────────────────┼───────────────┤
│ Sig is a DataType, not identical │ 62.3% │ 3642 │ 63.0% │
├──────────────────────────────────┼──────────────────┼─────────────────┼───────────────┤
│ Sig is a UnionAll │ 23.7% │ 5554 │ 36.5% │
└──────────────────────────────────┴──────────────────┴─────────────────┴───────────────┘
Replaying the subtype checks for all of these cost 272M instructions, which is 1.05% of the workload's 25.9G.
I was just re-stating what the robot said in the commit message:
A match that fully covers the call has the call signature itself as its spec_typesI wasn't entirely sure how we ended up with getting something out of there (which is the type-intersect walker closure->ti = jl_type_intersection_env_s(closure->type, (jl_value_t*)ml->sig, penv, &closure->issubty)) with closure->ti !== ml->sig, but it looks like the algorithm prefers to return closure->type (type a), even that will likely still cost us this this 1.6% in quite many other places too. We might want to revert this and fix it properly, since it might imply fixing properly would speed up reinfer performance by several %.
topolarity
KristofferC
This saves 1.6% of the instructions of type inference and optimization for the methods the Ferrite TTFX task compiles.
Assisted-by: Claude Code (Claude Fable 5.2)