Skip to content

Implement mutable addition #743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

blegat
Copy link
Contributor

@blegat blegat commented May 16, 2025

Symbolic expressions are mutable objects and exploiting this kind of mutability is essential when doing array operations like sum of a vector of matrix multiplications.
This PR is a proof of concept starting with the sum of an array of elements. We do the same with array multiplication etc... but let's start simple
The following simple benchmark:

using MutableArithmetics, Symbolics
@syms x::Real y::Real
v = repeat([x, y], 10^6)
s = @time sum(v, init = 0)
s = @time reduce(add!!, v, init = 0)

gives:

  2.669354 seconds (59.99 M allocations: 2.060 GiB, 41.07% gc time)
1000000x + 1000000y

  0.189088 seconds (2.00 M allocations: 30.503 MiB)
1000000x + 1000000y

It's still allocating a lot, I believe because a.dict[b] is type unstable since it a Dict{Any,Any} but we still save a lot of allocations.

Copy link
Contributor

github-actions bot commented May 16, 2025

Benchmark Results

master f6cfdc9... master / f6cfdc9...
overhead/acrule/a+2 1.01 ± 0.32 μs 1.08 ± 0.32 μs 0.941 ± 0.41
overhead/acrule/a+2+b 1.16 ± 0.31 μs 1.22 ± 0.31 μs 0.951 ± 0.35
overhead/acrule/a+b 0.27 ± 0.022 μs 0.259 ± 0.02 μs 1.04 ± 0.12
overhead/acrule/noop:Int 2.47 ± 0.01 ns 1.56 ± 0.01 ns 1.58 ± 0.012
overhead/acrule/noop:Sym 0.0373 ± 0.0058 μs 27.5 ± 6.4 ns 1.35 ± 0.38
overhead/rule/noop:Int 0.0361 ± 0.024 μs 0.0359 ± 0.024 μs 1.01 ± 0.94
overhead/rule/noop:Sym 0.0335 ± 0.024 μs 0.0332 ± 0.024 μs 1.01 ± 1
overhead/rule/noop:Term 0.0336 ± 0.024 μs 0.0333 ± 0.024 μs 1.01 ± 1
overhead/ruleset/noop:Int 0.124 ± 0.0046 μs 0.12 ± 0.0044 μs 1.03 ± 0.054
overhead/ruleset/noop:Sym 0.116 ± 0.0031 μs 0.123 ± 0.007 μs 0.94 ± 0.059
overhead/ruleset/noop:Term 4.57 ± 0.22 μs 4.7 ± 0.13 μs 0.974 ± 0.053
overhead/simplify/noop:Int 0.224 ± 0.015 μs 0.226 ± 0.014 μs 0.99 ± 0.089
overhead/simplify/noop:Sym 0.222 ± 0.018 μs 0.212 ± 0.0061 μs 1.05 ± 0.091
overhead/simplify/noop:Term 0.0486 ± 0.0024 ms 0.0498 ± 0.0018 ms 0.976 ± 0.06
overhead/simplify/randterm (+, *):serial 0.119 ± 0.0036 s 0.125 ± 0.0037 s 0.951 ± 0.04
overhead/simplify/randterm (+, *):thread 0.0697 ± 0.0065 s 0.0715 ± 0.0053 s 0.975 ± 0.12
overhead/simplify/randterm (/, *):serial 0.229 ± 0.022 ms 0.236 ± 0.02 ms 0.972 ± 0.12
overhead/simplify/randterm (/, *):thread 0.261 ± 0.02 ms 0.271 ± 0.019 ms 0.962 ± 0.099
overhead/substitute/a 0.101 ± 0.011 ms 0.108 ± 0.011 ms 0.932 ± 0.14
overhead/substitute/a,b 0.0885 ± 0.01 ms 0.0928 ± 0.01 ms 0.953 ± 0.15
overhead/substitute/a,b,c 20 ± 2.5 μs 21.7 ± 2.6 μs 0.922 ± 0.16
polyform/easy_iszero 0.0407 ± 0.0014 ms 0.0423 ± 0.0012 ms 0.961 ± 0.043
polyform/isone 3.1 ± 0.01 ns 2.79 ± 0.009 ns 1.11 ± 0.0051
polyform/iszero 1.37 ± 0.03 ms 1.42 ± 0.025 ms 0.96 ± 0.027
polyform/simplify_fractions 1.85 ± 0.036 ms 1.9 ± 0.029 ms 0.975 ± 0.024
time_to_load 1.15 ± 0.018 s 1.17 ± 0.0067 s 0.979 ± 0.017

Benchmark Plots

A plot of the benchmark results have been uploaded as an artifact to the workflow run for this PR.
Go to "Actions"->"Benchmark a pull request"->[the most recent run]->"Artifacts" (at the bottom).

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.

1 participant