mathnet-numerics icon indicating copy to clipboard operation
mathnet-numerics copied to clipboard

Avoidable allocations

Open febkor opened this issue 5 months ago • 0 comments

While C# is pretty fast at allocations, they can have a GC penalty. Math.NET is generally pretty good at avoiding allocations, but there are a few scenarios that might have slipped through. These are a few I encountered today:

Avoidable allocations

Here I mainly refer to the Double implementations, though the issues may be more general.

  • DenseVector.InfinityNorm(): avoidable delegate allocations of Math.Max
  • DenseVector.L2Norm(): avoidable delegate allocation
  • Vector.Do*(): scalar operand functions which call out to Map allocate a delegate per call
  • Matrix.DoAdd(), DoSubtract(), DoModulus(), ...: a few scalar operand functions which allocate a delegate per call for use with CommonParallel

Allow reusing allocations

Factorization.DenseLU() doesn't provide a way to input a result matrix, and always does var factors = (DenseMatrix) matrix.Clone() where matrix is the input matrix and factors is the result of the LU factorization.

febkor avatar Jul 14 '25 16:07 febkor