Skip to content

Conversation

@jverzani
Copy link
Contributor

Use @isuruf's rewrite of get_symengine_class in #285 to reduce memory allocation with get_symengine_class

This makes dispatch on BasicType nearly as performant as using ifelse statements.

A test case (relevant to #279)

using SymEngine
using SymEngine: libsymengine, get_symengine_class, is_symbol, Basic, BasicType

adiff(b1::Basic, b2::Basic) = adiff(b1, BasicType(b2))
adiff(b1::Basic, b2::BasicType) =
    throw(ArgumentError("Second argument must be of Symbol type"))

function adiff(b1::Basic, b2::BasicType{Val{:Symbol}})
    a = Basic()
    ret = ccall((:basic_diff, libsymengine), Int, (Ref{Basic}, Ref{Basic}, Ref{Basic}), a, b1, b2)
    return a
end


function bdiff(b1::Basic, b2::Basic)
    is_symbol(b2) || throw(ArgumentError("Second argument must be of Symbol type"))
    a = Basic()
    ret = ccall((:basic_diff, libsymengine), Int, (Ref{Basic}, Ref{Basic}, Ref{Basic}), a, b1, b2)
    return a
end

@vars x
u = sin(x)
julia> @time adiff(u,x)
  0.000023 seconds (3 allocations: 48 bytes)

julia> @time bdiff(u,x)
  0.000017 seconds (2 allocations: 32 bytes)

Compared to the older get_symengine_class we have

julia> @time adiff(u, x)
  0.000024 seconds (4 allocations: 72 bytes)

@isuruf isuruf merged commit d0eea29 into symengine:master Mar 19, 2025
9 checks passed
@jverzani jverzani mentioned this pull request Mar 19, 2025
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