From dc617780e89c131fa9f5ddbe9f8128709c25830c Mon Sep 17 00:00:00 2001 From: David Anthoff Date: Mon, 18 Jul 2022 17:00:14 -0700 Subject: [PATCH] Fix some TimeArray performance problems on 1.8 --- src/core/time_arrays.jl | 38 +++++++++++++++++++------------------- src/mcs/montecarlo.jl | 2 +- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/core/time_arrays.jl b/src/core/time_arrays.jl index b4244dd2e..473e05ce9 100644 --- a/src/core/time_arrays.jl +++ b/src/core/time_arrays.jl @@ -387,21 +387,21 @@ last_period(obj::TimestepArray{VariableTimestep{TIMES}, T, N, ti}) where {TIMES, time_labels(obj::TimestepArray{FixedTimestep{FIRST, STEP, LAST}, T, N, ti}) where {FIRST, STEP, LAST, T, N, ti} = collect(FIRST:STEP:(FIRST + (size(obj, 1) - 1) * STEP)) time_labels(obj::TimestepArray{VariableTimestep{TIMES}, T, N, ti}) where {TIMES, T, N, ti} = collect(TIMES) -split_indices(idxs, ti) = idxs[1:ti - 1], idxs[ti], idxs[ti + 1:end] +# split_indices(idxs, ti) = idxs[1:ti - 1], idxs[ti], idxs[ti + 1:end] function Base.getindex(arr::TimestepArray{FixedTimestep{A_FIRST, A_STEP, A_LAST}, T, N, ti}, idxs::Union{FixedTimestep{T_FIRST, T_STEP, T_LAST}, AnyIndex}...) where {A_FIRST, A_STEP, A_LAST, T_FIRST, T_STEP, T_LAST, T, N, ti} - idxs1, ts, idxs2 = split_indices(idxs, ti) + idxs1, ts, idxs2 = idxs[1:ti - 1], idxs[ti], idxs[ti + 1:end] return arr.data[idxs1..., ts.t, idxs2...] end function Base.getindex(arr::TimestepArray{VariableTimestep{A_TIMES}, T, N, ti}, idxs::Union{VariableTimestep{T_TIMES}, AnyIndex}...) where {A_TIMES, T_TIMES, T, N, ti} - idxs1, ts, idxs2 = split_indices(idxs, ti) + idxs1, ts, idxs2 = idxs[1:ti - 1], idxs[ti], idxs[ti + 1:end] return arr.data[idxs1..., ts.t, idxs2...] end function Base.getindex(arr::TimestepArray{FixedTimestep{FIRST, STEP, LAST}, T_data, N, ti}, idxs::Union{TimestepValue{T_time}, AnyIndex}...) where {T_data, N, ti, FIRST, STEP, LAST, T_time} _single_index_check(arr.data, idxs) - idxs1, ts, idxs2 = split_indices(idxs, ti) + idxs1, ts, idxs2 = idxs[1:ti - 1], idxs[ti], idxs[ti + 1:end] t = _get_time_value_position([FIRST:STEP:LAST...], ts) arr.data isa SubArray ? view_offset = arr.data.indices[ti][1] - 1 : view_offset = 0 t = t - view_offset @@ -410,7 +410,7 @@ end function Base.getindex(arr::TimestepArray{VariableTimestep{TIMES}, T_data, N, ti}, idxs::Union{TimestepValue{T_time}, AnyIndex}...) where {T_data, N, ti, TIMES, T_time} _single_index_check(arr.data, idxs) - idxs1, ts, idxs2 = split_indices(idxs, ti) + idxs1, ts, idxs2 = idxs[1:ti - 1], idxs[ti], idxs[ti + 1:end] t = _get_time_value_position(TIMES, ts) arr.data isa SubArray ? view_offset = arr.data.indices[ti][1] - 1 : view_offset = 0 t = t - view_offset @@ -419,7 +419,7 @@ end function Base.getindex(arr::TimestepArray{FixedTimestep{FIRST, STEP, LAST}, T, N, ti}, idxs::Union{TimestepIndex, AnyIndex}...) where {T, N, ti, FIRST, STEP, LAST} _single_index_check(arr.data, idxs) - idxs1, ts, idxs2 = split_indices(idxs, ti) + idxs1, ts, idxs2 = idxs[1:ti - 1], idxs[ti], idxs[ti + 1:end] t = ts.index _index_bounds_check(arr.data, ti, t) return arr.data[idxs1..., t, idxs2...] @@ -427,25 +427,25 @@ end function Base.getindex(arr::TimestepArray{VariableTimestep{TIMES}, T, N, ti}, idxs::Union{TimestepIndex, AnyIndex}...) where {T, N, ti, TIMES} _single_index_check(arr.data, idxs) - idxs1, ts, idxs2 = split_indices(idxs, ti) + idxs1, ts, idxs2 = idxs[1:ti - 1], idxs[ti], idxs[ti + 1:end] t = ts.index _index_bounds_check(arr.data, ti, t) return arr.data[idxs1..., t, idxs2...] end function Base.setindex!(arr::TimestepArray{FixedTimestep{A_FIRST, A_STEP, A_LAST}, T, N, ti}, val, idxs::Union{FixedTimestep{T_FIRST, T_STEP, T_LAST}, AnyIndex}...) where {A_FIRST, A_STEP, A_LAST, T_FIRST, T_STEP, T_LAST, T, N, ti} - idxs1, ts, idxs2 = split_indices(idxs, ti) + idxs1, ts, idxs2 = idxs[1:ti - 1], idxs[ti], idxs[ti + 1:end] setindex!(arr.data, val, idxs1..., ts.t, idxs2...) end function Base.setindex!(arr::TimestepArray{VariableTimestep{A_TIMES}, T, N, ti}, val, idxs::Union{VariableTimestep{T_TIMES}, AnyIndex}...) where {A_TIMES, T_TIMES, T, N, ti} - idxs1, ts, idxs2 = split_indices(idxs, ti) + idxs1, ts, idxs2 = idxs[1:ti - 1], idxs[ti], idxs[ti + 1:end] setindex!(arr.data, val, idxs1..., ts.t, idxs2...) end function Base.setindex!(arr::TimestepArray{FixedTimestep{FIRST, STEP, LAST}, T_data, N, ti}, val, idxs::Union{TimestepValue{T_time}, AnyIndex}...) where {T_data, N, ti, FIRST, STEP, LAST, T_time} _single_index_check(arr.data, idxs) - idxs1, ts, idxs2 = split_indices(idxs, ti) + idxs1, ts, idxs2 = idxs[1:ti - 1], idxs[ti], idxs[ti + 1:end] t = _get_time_value_position([FIRST:STEP:LAST...], ts) arr.data isa SubArray ? view_offset = arr.data.indices[ti][1] - 1 : view_offset = 0 t = t - view_offset @@ -454,7 +454,7 @@ end function Base.setindex!(arr::TimestepArray{VariableTimestep{TIMES}, T_data, N, ti}, val, idxs::Union{TimestepValue{T_time}, AnyIndex}...) where {T_data, N, ti, TIMES, T_time} _single_index_check(arr.data, idxs) - idxs1, ts, idxs2 = split_indices(idxs, ti) + idxs1, ts, idxs2 = idxs[1:ti - 1], idxs[ti], idxs[ti + 1:end] t = _get_time_value_position(TIMES, ts) arr.data isa SubArray ? view_offset = arr.data.indices[ti][1] - 1 : view_offset = 0 t = t - view_offset @@ -462,24 +462,24 @@ function Base.setindex!(arr::TimestepArray{VariableTimestep{TIMES}, T_data, N, t end function Base.setindex!(arr::TimestepArray{FixedTimestep{FIRST, STEP, LAST}, T, N, ti}, val, idxs::Union{TimestepIndex, AnyIndex}...) where {FIRST, STEP, LAST, T, N, ti} - idxs1, ts, idxs2 = split_indices(idxs, ti) + idxs1, ts, idxs2 = idxs[1:ti - 1], idxs[ti], idxs[ti + 1:end] setindex!(arr.data, val, idxs1..., ts.index, idxs2...) end function Base.setindex!(arr::TimestepArray{VariableTimestep{TIMES}, T, N, ti}, val, idxs::Union{TimestepIndex, AnyIndex}...) where {TIMES, T, N, ti} - idxs1, ts, idxs2 = split_indices(idxs, ti) + idxs1, ts, idxs2 = idxs[1:ti - 1], idxs[ti], idxs[ti + 1:end] setindex!(arr.data, val, idxs1..., ts.index, idxs2...) end # Indexing with arrays of TimestepIndexes or TimestepValues function Base.getindex(arr::TimestepArray{TS, T, N, ti}, idxs::Union{Array{TimestepIndex,1}, AnyIndex}...) where {TS, T, N, ti} - idxs1, ts_array, idxs2 = split_indices(idxs, ti) + idxs1, ts_array, idxs2 = idxs[1:ti - 1], idxs[ti], idxs[ti + 1:end] ts_idxs = _get_ts_indices(ts_array) return arr.data[idxs1..., ts_idxs, idxs2...] end function Base.getindex(arr::TimestepArray{FixedTimestep{FIRST, STEP, LAST}, T_data, N, ti}, idxs::Union{Array{TimestepValue{T_time},1}, AnyIndex}...) where {T_data, N, ti, FIRST, STEP, LAST, T_time} - idxs1, ts_array, idxs2 = split_indices(idxs, ti) + idxs1, ts_array, idxs2 = idxs[1:ti - 1], idxs[ti], idxs[ti + 1:end] ts_idxs = _get_ts_indices(ts_array, [FIRST:STEP:LAST...]) arr.data isa SubArray ? view_offset = arr.data.indices[ti][1] - 1 : view_offset = 0 ts_idxs = ts_idxs .- view_offset @@ -487,7 +487,7 @@ function Base.getindex(arr::TimestepArray{FixedTimestep{FIRST, STEP, LAST}, T_da end function Base.getindex(arr::TimestepArray{VariableTimestep{TIMES}, T_data, N, ti}, idxs::Union{Array{TimestepValue{T_time},1}, AnyIndex}...) where {T_data, N, ti, TIMES, T_time} - idxs1, ts_array, idxs2 = split_indices(idxs, ti) + idxs1, ts_array, idxs2 = idxs[1:ti - 1], idxs[ti], idxs[ti + 1:end] ts_idxs = _get_ts_indices(ts_array, TIMES) arr.data isa SubArray ? view_offset = arr.data.indices[ti][1] - 1 : view_offset = 0 ts_idxs = ts_idxs .- view_offset @@ -495,13 +495,13 @@ function Base.getindex(arr::TimestepArray{VariableTimestep{TIMES}, T_data, N, ti end function Base.setindex!(arr::TimestepArray{TS, T, N, ti}, vals, idxs::Union{Array{TimestepIndex,1}, AnyIndex}...) where {TS, T, N, ti} - idxs1, ts_array, idxs2 = split_indices(idxs, ti) + idxs1, ts_array, idxs2 = idxs[1:ti - 1], idxs[ti], idxs[ti + 1:end] ts_idxs = _get_ts_indices(ts_array) setindex!(arr.data, vals, idxs1..., ts_idxs, idxs2...) end function Base.setindex!(arr::TimestepArray{FixedTimestep{FIRST, STEP, LAST}, T_data, N, ti}, vals, idxs::Union{Array{TimestepValue{T_time},1}, AnyIndex}...) where {T_data, N, ti, FIRST, STEP, LAST, T_time} - idxs1, ts_array, idxs2 = split_indices(idxs, ti) + idxs1, ts_array, idxs2 = idxs[1:ti - 1], idxs[ti], idxs[ti + 1:end] ts_idxs = _get_ts_indices(ts_array, [FIRST:STEP:LAST...]) arr.data isa SubArray ? view_offset = arr.data.indices[ti][1] - 1 : view_offset = 0 ts_idxs = ts_idxs .- view_offset @@ -509,7 +509,7 @@ function Base.setindex!(arr::TimestepArray{FixedTimestep{FIRST, STEP, LAST}, T_d end function Base.setindex!(arr::TimestepArray{VariableTimestep{TIMES}, T_data, N, ti}, vals, idxs::Union{Array{TimestepValue{T_time},1}, AnyIndex}...) where {T_data, N, ti, TIMES, T_time} - idxs1, ts_array, idxs2 = split_indices(idxs, ti) + idxs1, ts_array, idxs2 = idxs[1:ti - 1], idxs[ti], idxs[ti + 1:end] ts_idxs = _get_ts_indices(ts_array, TIMES) arr.data isa SubArray ? view_offset = arr.data.indices[ti][1] - 1 : view_offset = 0 ts_idxs = ts_idxs .- view_offset diff --git a/src/mcs/montecarlo.jl b/src/mcs/montecarlo.jl index 809df1a51..ddbb61eb9 100644 --- a/src/mcs/montecarlo.jl +++ b/src/mcs/montecarlo.jl @@ -381,7 +381,7 @@ function _perturb_param!(param::ArrayModelParameter{T}, md::ModelDef, i::Int, broadcast_flag ? pvalue[indices...] .= rvalue : pvalue[indices...] = rvalue else - indices1, ts, indices2 = split_indices(indices, ti) + indices1, ts, indices2 = indices[1:ti - 1], indices[ti], indices[ti + 1:end] non_ts_indices = [indices1..., indices2...] broadcast_flag = isempty(non_ts_indices) ? false : sum(map(x -> length(x) > 1, non_ts_indices)) > 0