Skip to content

Fix type inference and performance problems of munge_data#392

Merged
ChrisRackauckas merged 2 commits intomasterfrom
dw/munge_data
Feb 7, 2025
Merged

Fix type inference and performance problems of munge_data#392
ChrisRackauckas merged 2 commits intomasterfrom
dw/munge_data

Conversation

@devmotion
Copy link
Member

@devmotion devmotion commented Feb 7, 2025

The PR fixes a few issues related to #389.

On the master branch:

julia> using DataInterpolations, JET, Chairmarks, Random, Test

julia> @inferred(DataInterpolations.munge_data(["A","B","C"], [0.1,0.2,0.3]))
ERROR: return type Tuple{Vector{String}, Vector{Float64}} does not match inferred return type Tuple{Any, Any}
Stacktrace:
 [1] error(s::String)
   @ Base ./error.jl:35
 [2] top-level scope
   @ REPL[33]:1

julia> @report_call ConstantInterpolation(["A", "B", "C"], [0.1, 0.2, 0.3])
═════ 2 possible errors found ═════
┌ ConstantInterpolation(u::Vector{String}, t::Vector{Float64}) @ DataInterpolations /Users/david/.julia/packages/DataInterpolations/dFVzN/src/interpolation_caches.jl:353
│┌ ConstantInterpolation(u::Vector{…}, t::Vector{…}; dir::Symbol, extrapolation::DataInterpolations.ExtrapolationType.T, extrapolation_left::DataInterpolations.ExtrapolationType.T, extrapolation_right::DataInterpolations.ExtrapolationType.T, cache_parameters::Bool, assume_linear_t::Float64) @ DataInterpolations /Users/david/.julia/packages/DataInterpolations/dFVzN/src/interpolation_caches.jl:360
││┌ munge_data(u::Vector{String}, t::Vector{Float64}) @ DataInterpolations /Users/david/.julia/packages/DataInterpolations/dFVzN/src/interpolation_utils.jl:120
│││┌ materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, _A, <:Tuple{Vector}} where _A) @ Base.Broadcast ./broadcast.jl:872
││││┌ copy(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{…}, Tuple{…}, _A, <:Tuple{…}} where _A) @ Base.Broadcast ./broadcast.jl:897
│││││┌ copyto!(dest::BitVector, bc::Base.Broadcast.Broadcasted{…} where _A) @ Base.Broadcast ./broadcast.jl:925
││││││┌ copyto!(dest::BitVector, bc::Base.Broadcast.Broadcasted{Nothing, Tuple{Base.OneTo{…}}, _A, <:Tuple{Vector}} where _A) @ Base.Broadcast ./broadcast.jl:1002
│││││││┌ getindex(::Base.Broadcast.Broadcasted{Nothing, Tuple{…}, _A, <:Tuple{…}} where _A, ::Int64, ::CartesianIndex{0}) @ Base.Broadcast ./broadcast.jl:610
││││││││┌ _broadcast_getindex(bc::Base.Broadcast.Broadcasted{Nothing, Tuple{Base.OneTo{Int64}}, _A, Union{}} where _A, I::Int64) @ Base.Broadcast ./broadcast.jl:669
│││││││││┌ getproperty(x::Base.Broadcast.Broadcasted{Nothing, Tuple{Base.OneTo{Int64}}, _A, Union{}} where _A, f::Symbol) @ Base ./Base.jl:49
││││││││││ invalid builtin function call: Base.getfield(x::Base.Broadcast.Broadcasted{Nothing, Tuple{Base.OneTo{Int64}}, _A, Union{}} where _A, f::Symbol)
│││││││││└────────────────────
││││┌ copy(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{…}, Tuple{…}, _A, <:Tuple{…}} where _A) @ Base.Broadcast ./broadcast.jl:911
│││││┌ getindex(bc::Base.Broadcast.Broadcasted{…} where _A, Is::Int64) @ Base.Broadcast ./broadcast.jl:610
││││││┌ _broadcast_getindex(bc::Base.Broadcast.Broadcasted{…} where _A, I::Int64) @ Base.Broadcast ./broadcast.jl:669
│││││││┌ getproperty(x::Base.Broadcast.Broadcasted{…} where _A, f::Symbol) @ Base ./Base.jl:49
││││││││ invalid builtin function call: Base.getfield(x::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Tuple{Base.OneTo{Int64}}, _A, Union{}} where _A, f::Symbol)
│││││││└────────────────────


julia> @b ([randstring(3) for _ in 1:100],rand(100)) DataInterpolations.munge_data(_[1], _[2])
7.938 μs (230 allocs: 9.188 KiB)

julia> @code_warntype ConstantInterpolation(["A", "B", "C"], [0.1, 0.2, 0.3])
MethodInstance for ConstantInterpolation(::Vector{String}, ::Vector{Float64})
  from ConstantInterpolation(u, t; dir, extrapolation, extrapolation_left, extrapolation_right, cache_parameters, assume_linear_t) @ DataInterpolations ~/.julia/packages/DataInterpolations/dFVzN/src/interpolation_caches.jl:353
Arguments
  #self#::Type{ConstantInterpolation}
  u::Vector{String}
  t::Vector{Float64}
Body::ConstantInterpolation
1%1 = DataInterpolations.:(var"#ConstantInterpolation#6")::Core.Const(DataInterpolations.var"#ConstantInterpolation#6")
│   %2 = DataInterpolations.ExtrapolationType.None::Core.Const(DataInterpolations.ExtrapolationType.None)
│   %3 = DataInterpolations.ExtrapolationType.None::Core.Const(DataInterpolations.ExtrapolationType.None)
│   %4 = DataInterpolations.ExtrapolationType.None::Core.Const(DataInterpolations.ExtrapolationType.None)
│   %5 = (%1)(:left, %2, %3, %4, false, 0.01, #self#, u, t)::ConstantInterpolation
└──      return %5

With this PR:

julia> using DataInterpolations, JET, Chairmarks, Random, Test

julia> @inferred(DataInterpolations.munge_data(["A","B","C"], [0.1,0.2,0.3]))
(["A", "B", "C"], [0.1, 0.2, 0.3])

julia> @report_call ConstantInterpolation(["A", "B", "C"], [0.1, 0.2, 0.3])
No errors detected

julia> @report_call ConstantInterpolation(["A";; "B";; "C"], [0.1, 0.2, 0.3])
No errors detected

julia> @report_call ConstantInterpolation(["A";;; "B";;; "C"], [0.1, 0.2, 0.3])
No errors detected

julia> @b ([randstring(3) for _ in 1:100],rand(100)) DataInterpolations.munge_data(_[1], _[2])
1.993 ns

julia> @code_warntype ConstantInterpolation(["A", "B", "C"], [0.1, 0.2, 0.3])
MethodInstance for ConstantInterpolation(::Vector{String}, ::Vector{Float64})
  from ConstantInterpolation(u, t; dir, extrapolation, extrapolation_left, extrapolation_right, cache_parameters, assume_linear_t) @ DataInterpolations ~/.julia/dev/DataInterpolations/src/interpolation_caches.jl:353
Arguments
  #self#::Type{ConstantInterpolation}
  u::Vector{String}
  t::Vector{Float64}
Body::ConstantInterpolation{Vector{String}, Vector{Float64}, _A, String} where _A
1%1 = DataInterpolations.:(var"#ConstantInterpolation#6")::Core.Const(DataInterpolations.var"#ConstantInterpolation#6")
│   %2 = DataInterpolations.ExtrapolationType.None::Core.Const(DataInterpolations.ExtrapolationType.None)
│   %3 = DataInterpolations.ExtrapolationType.None::Core.Const(DataInterpolations.ExtrapolationType.None)
│   %4 = DataInterpolations.ExtrapolationType.None::Core.Const(DataInterpolations.ExtrapolationType.None)
│   %5 = (%1)(:left, %2, %3, %4, false, 0.01, #self#, u, t)::ConstantInterpolation{Vector{String}, Vector{Float64}, _A, String} where _A
└──      return %5

@ChrisRackauckas ChrisRackauckas merged commit 209762f into master Feb 7, 2025
17 checks passed
@ChrisRackauckas ChrisRackauckas deleted the dw/munge_data branch February 7, 2025 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants