Skip to content

Conversation

@JamesWrigley
Copy link
Member

@JamesWrigley JamesWrigley commented Oct 18, 2025

This prevents a bunch of invalidations, specifically these ones in Julia 1.12 with UnsafePointers.jl (CC @cjdoris):

 inserting convert(P::Type{<:Ptr}, p::UnsafePointers.UnsafePtr) @ UnsafePointers ~/git/UnsafePointers.jl/src/UnsafePointers.jl:66 invalidated:
   mt_backedges:  1: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_npointers(::Type{T} where T<:(NamedTuple{names, T} where {T<:Tuple, names})) (4 children)
                  2: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_npointers(::Type{T} where T<:AbstractString) (4 children)
                  3: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_npointers(::Type{T} where T<:Tuple{Symbol, Any}) (4 children)
                  4: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_npointers(::Type{T} where T<:(NamedTuple{(:message, :position, :hint), _A} where _A)) (4 children)
                  5: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_npointers(::Type{T} where T<:(SubString{_A} where _A)) (4 children)
                  6: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_alignment(::Type{T} where T<:(NamedTuple{(:message, :position, :hint), _A} where _A)) (15 children)
                  7: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_alignment(::Type{T} where T<:(SubString{_A} where _A)) (19 children)
                  8: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_alignment(::Type{T} where T<:Tuple{Any, Symbol, Symbol}) (21 children)
                  9: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_alignment(::Type{T} where T<:Tuple{Symbol, Any}) (23 children)
                 10: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_alignment(::Type{T} where T<:AbstractString) (52 children)
                 11: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_alignment(::Type{T} where T<:(NamedTuple{names, T} where {T<:Tuple, names})) (1739 children)

Using Cthulhu to look at the last invalidation with 1739 children showed this:

datatype_alignment(dt::DataType) @ Base ~/.julia/juliaup/julia-1.12.1+0.x64.linux.gnu/share/julia/base/runtime_internals.jl:544
544 function datatype_alignment(dt::Type{T} where T<:(NamedTuple{names, T} where {T<:Tuple, names})::DataType)::Any
545     @_foldable_meta
546     (dt::Type{T} where T<:(NamedTuple{names, T} where {T<:Tuple, names}).layout::Any == C_NULL::Core.Const(Ptr{Nothing}(0x0000000000000000)))::Any && throw(UndefRefError()::Core.Const(UndefRefError()))
547     alignment::Any = unsafe_load(convert(Ptr{DataTypeLayout}::Type{Ptr{Base.DataTypeLayout}}, dt::Type{T} where T<:(NamedTuple{names, T} where {T<:Tuple, names}).layout::Any)::Any)::Any.alignment::Any
548     return Int::Type{Int64}(alignment::Any)::Any
549 end

The .layout field was getting inferred as Any which caused that method to be invalidated when UnsafePointers.jl defined a new convert() method.

Would it be possible to backport this to 1.12? UnsafePointers is a dependency of PythonCall so it's quite widely used.

This prevents some invalidations.
@KristofferC KristofferC added the backport 1.12 Change should be backported to release-1.12 label Oct 18, 2025
@JamesWrigley
Copy link
Member Author

I believe the test failure is fixed by #59879.

JamesWrigley and others added 2 commits October 20, 2025 14:56
Co-authored-by: Shuhei Kadowaki <[email protected]>
Co-authored-by: Shuhei Kadowaki <[email protected]>
@aviatesk aviatesk merged commit 90f4289 into JuliaLang:master Oct 20, 2025
7 checks passed
@JamesWrigley JamesWrigley deleted the ptr-invalidations branch October 20, 2025 18:19
KristofferC pushed a commit that referenced this pull request Oct 21, 2025
This prevents a bunch of invalidations, specifically these ones in Julia
1.12 with UnsafePointers.jl (CC @cjdoris):
```julia
 inserting convert(P::Type{<:Ptr}, p::UnsafePointers.UnsafePtr) @ UnsafePointers ~/git/UnsafePointers.jl/src/UnsafePointers.jl:66 invalidated:
   mt_backedges:  1: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_npointers(::Type{T} where T<:(NamedTuple{names, T} where {T<:Tuple, names})) (4 children)
                  2: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_npointers(::Type{T} where T<:AbstractString) (4 children)
                  3: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_npointers(::Type{T} where T<:Tuple{Symbol, Any}) (4 children)
                  4: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_npointers(::Type{T} where T<:(NamedTuple{(:message, :position, :hint), _A} where _A)) (4 children)
                  5: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_npointers(::Type{T} where T<:(SubString{_A} where _A)) (4 children)
                  6: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_alignment(::Type{T} where T<:(NamedTuple{(:message, :position, :hint), _A} where _A)) (15 children)
                  7: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_alignment(::Type{T} where T<:(SubString{_A} where _A)) (19 children)
                  8: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_alignment(::Type{T} where T<:Tuple{Any, Symbol, Symbol}) (21 children)
                  9: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_alignment(::Type{T} where T<:Tuple{Symbol, Any}) (23 children)
                 10: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_alignment(::Type{T} where T<:AbstractString) (52 children)
                 11: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_alignment(::Type{T} where T<:(NamedTuple{names, T} where {T<:Tuple, names})) (1739 children)
```

Using Cthulhu to look at the last invalidation with 1739 children showed
this:
```julia
datatype_alignment(dt::DataType) @ Base ~/.julia/juliaup/julia-1.12.1+0.x64.linux.gnu/share/julia/base/runtime_internals.jl:544
544 function datatype_alignment(dt::Type{T} where T<:(NamedTuple{names, T} where {T<:Tuple, names})::DataType)::Any
545     @_foldable_meta
546     (dt::Type{T} where T<:(NamedTuple{names, T} where {T<:Tuple, names}).layout::Any == C_NULL::Core.Const(Ptr{Nothing}(0x0000000000000000)))::Any && throw(UndefRefError()::Core.Const(UndefRefError()))
547     alignment::Any = unsafe_load(convert(Ptr{DataTypeLayout}::Type{Ptr{Base.DataTypeLayout}}, dt::Type{T} where T<:(NamedTuple{names, T} where {T<:Tuple, names}).layout::Any)::Any)::Any.alignment::Any
548     return Int::Type{Int64}(alignment::Any)::Any
549 end
```

The `.layout` field was getting inferred as `Any` which caused that
method to be invalidated when UnsafePointers.jl defined a new
`convert()` method.

Would it be possible to backport this to 1.12? UnsafePointers is a
dependency of PythonCall so it's quite widely used.

---------

Co-authored-by: Shuhei Kadowaki <[email protected]>
(cherry picked from commit 90f4289)
KristofferC pushed a commit that referenced this pull request Oct 21, 2025
This prevents a bunch of invalidations, specifically these ones in Julia
1.12 with UnsafePointers.jl (CC @cjdoris):
```julia
 inserting convert(P::Type{<:Ptr}, p::UnsafePointers.UnsafePtr) @ UnsafePointers ~/git/UnsafePointers.jl/src/UnsafePointers.jl:66 invalidated:
   mt_backedges:  1: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_npointers(::Type{T} where T<:(NamedTuple{names, T} where {T<:Tuple, names})) (4 children)
                  2: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_npointers(::Type{T} where T<:AbstractString) (4 children)
                  3: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_npointers(::Type{T} where T<:Tuple{Symbol, Any}) (4 children)
                  4: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_npointers(::Type{T} where T<:(NamedTuple{(:message, :position, :hint), _A} where _A)) (4 children)
                  5: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_npointers(::Type{T} where T<:(SubString{_A} where _A)) (4 children)
                  6: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_alignment(::Type{T} where T<:(NamedTuple{(:message, :position, :hint), _A} where _A)) (15 children)
                  7: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_alignment(::Type{T} where T<:(SubString{_A} where _A)) (19 children)
                  8: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_alignment(::Type{T} where T<:Tuple{Any, Symbol, Symbol}) (21 children)
                  9: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_alignment(::Type{T} where T<:Tuple{Symbol, Any}) (23 children)
                 10: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_alignment(::Type{T} where T<:AbstractString) (52 children)
                 11: signature Tuple{typeof(convert), Type{Ptr{Base.DataTypeLayout}}, Any} triggered MethodInstance for Base.datatype_alignment(::Type{T} where T<:(NamedTuple{names, T} where {T<:Tuple, names})) (1739 children)
```

Using Cthulhu to look at the last invalidation with 1739 children showed
this:
```julia
datatype_alignment(dt::DataType) @ Base ~/.julia/juliaup/julia-1.12.1+0.x64.linux.gnu/share/julia/base/runtime_internals.jl:544
544 function datatype_alignment(dt::Type{T} where T<:(NamedTuple{names, T} where {T<:Tuple, names})::DataType)::Any
545     @_foldable_meta
546     (dt::Type{T} where T<:(NamedTuple{names, T} where {T<:Tuple, names}).layout::Any == C_NULL::Core.Const(Ptr{Nothing}(0x0000000000000000)))::Any && throw(UndefRefError()::Core.Const(UndefRefError()))
547     alignment::Any = unsafe_load(convert(Ptr{DataTypeLayout}::Type{Ptr{Base.DataTypeLayout}}, dt::Type{T} where T<:(NamedTuple{names, T} where {T<:Tuple, names}).layout::Any)::Any)::Any.alignment::Any
548     return Int::Type{Int64}(alignment::Any)::Any
549 end
```

The `.layout` field was getting inferred as `Any` which caused that
method to be invalidated when UnsafePointers.jl defined a new
`convert()` method.

Would it be possible to backport this to 1.12? UnsafePointers is a
dependency of PythonCall so it's quite widely used.

---------

Co-authored-by: Shuhei Kadowaki <[email protected]>
(cherry picked from commit 90f4289)
@KristofferC KristofferC mentioned this pull request Oct 21, 2025
18 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport 1.12 Change should be backported to release-1.12

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants