Skip to content
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

Type instability of fieldtypes #43228

Open
mikmoore opened this issue Nov 25, 2021 · 1 comment
Open

Type instability of fieldtypes #43228

mikmoore opened this issue Nov 25, 2021 · 1 comment
Labels
performance Must go faster types and dispatch Types, subtyping and method dispatch

Comments

@mikmoore
Copy link
Contributor

Julia v1.7.0-rc3
The use of fieldtypes results in a type instability, inferring only to Tuple:

julia> @code_warntype fieldtypes(ComplexF64)
MethodInstance for fieldtypes(::Type{ComplexF64})
  from fieldtypes(T::Type) in Base at reflection.jl:791
Arguments
  #self#::Core.Const(fieldtypes)
  T::Core.Const(ComplexF64)
Locals
  #13::Base.var"#13#14"{DataType}
Body::Tuple
1 ─ %1 = Base.:(var"#13#14")::Core.Const(Base.var"#13#14")
│   %2 = Core.typeof(T)::Core.Const(DataType)
│   %3 = Core.apply_type(%1, %2)::Core.Const(Base.var"#13#14"{DataType})
│        (#13 = %new(%3, T))
│   %5 = #13::Core.Const(Base.var"#13#14"{DataType}(ComplexF64))
│   %6 = Base.fieldcount(T)::Core.Const(2)
│   %7 = Base.ntupleany(%5, %6)::Tuple
└──      return %7

This stems from a change introduced in #37163, where the definition of fieldtypes changed from being based on ntuple to a newly-introduced ntupleany. It was related to the Great Invalidation Hunt, but I cannot follow the discussion in that PR well enough to understand if this was an intended consequence.

Personally, when I use fieldtypes I'm wanting a compile-time constant that I can use to produce stable operations downstream. Currently, I must use the workaround of defining a fieldtypes_stable function using the old ntuple definition.

@martinholters
Copy link
Member

CC @timholy
Why does it help reduce invalidations if inference of fieldtypes is less precise? Or was the old fieldtypes prone to invalidation somehow?

@nsajko nsajko added performance Must go faster types and dispatch Types, subtyping and method dispatch labels Feb 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Must go faster types and dispatch Types, subtyping and method dispatch
Projects
None yet
Development

No branches or pull requests

3 participants