Skip to content

push! is type unstable #328

Open
Open

Description

@rveltz

Hi,

I use SA in BifurcationKit but I recently noticed that the push! is type unstable. If I remove StructArray in here in ContResult, the TI disappear. Am I wrongly using your package?

using Revise
using BifurcationKit

function Fsl!(out, X, p)
    (;μ, c ) = p
    x, y = X
    ua = x^2 + y^2
    out[1] = μ * x - y - x * ua * c
    out[2] = x + μ * y - y * ua * c
    out
end

function JFsl!(J, X, p)
    (;μ, c) = p
    x, y = X
    ua = x^2 + y^2

    J[1, 1] = μ - c * (ua + 2x^2)
    J[1, 2] = -1 - 2c * x * y
    J[2, 1] = 1 - 2c * x * y
    J[2, 2] = μ - c * (ua + 2y^2)

    J
end
JFsl(x,p) = JFsl!(zeros(2,2),x,p)
prob = BifurcationProblem(Fsl!, zeros(2), (μ=0-.1, c=1.), (@optic _.μ); J! = JFsl!, J = JFsl)

opts = ContinuationPar(p_max = 2., detect_bifurcation = 0, detect_fold=false)
br = @time continuation(prob, PALC(), opts)

@profview (for _=1:10000; continuation(prob, PALC(), ContinuationPar(p_max = 2., detect_bifurcation = 0, detect_fold=false)); end 

gives

Image Image

Activity

  1. aplavin commented on Apr 26, 2025

    @aplavin
    Member

    Having an MWE would make understanding/fixing this issue more feasible...
    In general, StructArrays tries to be very careful with type stability, and a typical push! call is definitely stable:

    julia> A = StructArray(x=[1,2,3], y=[:a, :b, :c])
    3-element StructArray(::Vector{Int64}, ::Vector{Symbol}) with eltype @NamedTuple{x::Int64, y::Symbol}:
     (x = 1, y = :a)
     (x = 2, y = :b)
     (x = 3, y = :c)
    
    julia> @code_warntype push!(A, (x=4, y=:d))
    MethodInstance for push!(::StructVector{@NamedTuple{x::Int64, y::Symbol}, @NamedTuple{x::Vector{Int64}, y::Vector{Symbol}}, Int64}, ::@NamedTuple{x::Int64, y::Symbol})
      from push!(s::StructVector{T}, vals) where T @ StructArrays ~/.julia/packages/StructArrays/n5wxA/src/structarray.jl:384
    Static Parameters
      T = @NamedTuple{x::Int64, y::Symbol}
    Arguments
      #self#::Core.Const(push!)
      s::StructVector{@NamedTuple{x::Int64, y::Symbol}, @NamedTuple{x::Vector{Int64}, y::Vector{Symbol}}, Int64}
      vals::@NamedTuple{x::Int64, y::Symbol}
    Locals
      valsT::@NamedTuple{x::Int64, y::Symbol}
    Body::StructVector{@NamedTuple{x::Int64, y::Symbol}, @NamedTuple{x::Vector{Int64}, y::Vector{Symbol}}, Int64}
    1 ─     (valsT = StructArrays.maybe_convert_elt($(Expr(:static_parameter, 1)), vals))
    │       StructArrays.foreachfield(StructArrays.push!, s, valsT)
    └──     return s
  2. rveltz commented on Apr 27, 2025

    @rveltz
    Author

    I edited the code to make it functional, sorry for the mistake

  3. aplavin commented on Apr 27, 2025

    @aplavin
    Member

    I mean, something more minimal would be of a great help. Presumably, just two steps needed to reproduce the instability:

    1. Create a structarray.
    2. Call push! with the structarray and an object.

    Someone may find time to debug the current example as-is, but having a more minimal one would lead to a faster fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions