Skip to content

Conversation

@miguelmaso
Copy link
Collaborator

Suggestion of Gemini, it should have less allocations and better performance.

@github-actions
Copy link

github-actions bot commented Dec 18, 2025

Benchmark Results (Julia v1)

Time benchmarks
main ef1819c... main / ef1819c...
Constitutive models/Visco-elastic Ψ 0.0503 ± 0.025 ms 0.0495 ± 0.026 ms 1.02 ± 0.74
Constitutive models/Visco-elastic ∂Ψu 0.0672 ± 0.0044 ms 0.0658 ± 0.0064 ms 1.02 ± 0.12
Constitutive models/Visco-elastic ∂Ψuu 0.117 ± 0.016 ms 0.115 ± 0.016 ms 1.02 ± 0.2
Simulations/StaticMechanicalDirichlet 0.154 ± 0.011 s 0.152 ± 0.0086 s 1.01 ± 0.091
Simulations/StaticMechanicalNeumann 0.135 ± 0.016 s 0.138 ± 0.016 s 0.984 ± 0.16
Simulations/ViscoElastic 21.7 s 21.2 s 1.02
Tensor algebra/Cofactor 0.07 ± 0.001 μs 0.07 ± 0.01 μs 1 ± 0.14
Tensor algebra/Det(A)Inv(A') 0.13 ± 0.009 μs 0.13 ± 0.001 μs 1 ± 0.07
Tensor algebra/δδ_λ_2d 30 ± 0 ns 30 ± 0 ns 1 ± 0
Tensor algebra/δδ_μ_2d 30 ± 0 ns 30 ± 0 ns 1 ± 0
time_to_load 2.54 ± 0.015 s 2.52 ± 0.024 s 1.01 ± 0.011
Memory benchmarks
main ef1819c... main / ef1819c...
Constitutive models/Visco-elastic Ψ 1.25 k allocs: 0.0789 MB 1.24 k allocs: 0.0777 MB 1.02
Constitutive models/Visco-elastic ∂Ψu 1.37 k allocs: 0.0911 MB 1.36 k allocs: 0.0898 MB 1.01
Constitutive models/Visco-elastic ∂Ψuu 2.33 k allocs: 0.158 MB 2.32 k allocs: 0.155 MB 1.02
Simulations/StaticMechanicalDirichlet 1.61 M allocs: 0.115 GB 1.61 M allocs: 0.115 GB 1
Simulations/StaticMechanicalNeumann 1.48 M allocs: 0.0933 GB 1.48 M allocs: 0.0933 GB 1
Simulations/ViscoElastic 0.344 G allocs: 22.3 GB 0.343 G allocs: 22 GB 1.01
Tensor algebra/Cofactor 1 allocs: 0.0781 kB 1 allocs: 0.0781 kB 1
Tensor algebra/Det(A)Inv(A') 4 allocs: 0.25 kB 4 allocs: 0.25 kB 1
Tensor algebra/δδ_λ_2d 0 allocs: 0 B 0 allocs: 0 B
Tensor algebra/δδ_μ_2d 0 allocs: 0 B 0 allocs: 0 B
time_to_load 0.149 k allocs: 11.1 kB 0.149 k allocs: 11.1 kB 1

@miguelmaso
Copy link
Collaborator Author

Conclusion
After doing benchmarks and measuring time, my conclusion is there is no significative difference. mapreduce is much more readable and makes code easier to maintain.

using BenchmarkTools
using StaticArrays
using LinearAlgebra

I3 = SMatrix{3,3}(1.0, 0, 0, 0, 1, 0, 0, 0, 1)
F = SMatrix{3,3}(rand(9))*1e-2 + I3
P() = F*F' + det(F) * I3 + inv(F*F')

N = tuple(1:10...)

function loop()
  val = SMatrix{3,3}(zeros(9))
  for i in N
    val += P()
  end
  return val
end

println("P()")
print("mapreduce :"); @btime mapreduce(i -> P(), +, N); # Calling mapreduce over a tuple improves performance significatively
print("for loop  :"); @btime loop();

function loop(x,y)
  val = 0
  for (i,j) in zip(x,y)
    val += i==j
  end
  return val
end

x = rand(10_000); y = similar(x)
println("x==y")
print("mapreduce :"); @btime mapreduce(==,+,x,y);
print("for loop  :"); @btime loop(x,y);

Possible output using Julia 1.12.3

P()
mapreduce :  1.640 μs (90 allocations: 6.42 KiB)
for loop  :  3.288 μs (101 allocations: 6.80 KiB)
x==y
mapreduce :  2.633 μs (4 allocations: 9.92 KiB)
for loop  :  3.025 μs (0 allocations: 0 bytes)

Changes

  • Keep mapreduce
  • Removed the custom transpose function, which had a lot of allocations.
  • Removed old functions rlated to state variables not following the Julia style guide.

@miguelmaso miguelmaso requested a review from jmartfrut December 19, 2025 08:26
@miguelmaso
Copy link
Collaborator Author

FYI @jmartfrut , the custom function transpose has been removed (you reported it had a tons of allocations).

Those changes come from the implementation of anisotropy+viscoelasticity, which is WIP. More amazing changes will come!

@miguelmaso miguelmaso merged commit 14a8597 into main Dec 19, 2025
4 checks passed
@miguelmaso miguelmaso deleted the optimize-map branch December 19, 2025 08:29
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