Skip to content

Reducing memory allocation using lazy evaluation #29

@Shuenhoy

Description

@Shuenhoy

Thank you for this project! .NET world really need good numerics and linear algebra libraries nowadays.

The README indicates the impact of memory allocation on performance is already noticed, and an inplace operation is suggested.
In principle, such inplace operations do avoid memory allocations. However, it would requrie the coder to manually arrange the orders for a complex chained expression, and make the code harder to read.

The popular libraries in C++ world (e.g. Eigen3 and Blaze) use a lazy evaluation technique to handle such difficulties. For example, var X = A.Add(B) produces a MatAdd<Mat, Mat> instead of a Mat, var X = A.Add(B.MultipleBy(C)) will produce a MatAdd<Mat, MatMul<Mat, Mat>>, where MatAdd and MatMul only track the operations to be performed (but not actually perform them) and are something like a ref struct that does not need heap allocation.

And finally, X.Eval() will allocate a new matrix and X.EvalTo(M) will use the pre-allocated M. Only at this point the lazy evaluation is performed, and all the intermediate temp allocation can be avoided.

(This may be simplified using the upcoming Extention Everthing, e.g var R = X.Eval() may be just replaced by Mat R = X, but I am not sure)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions