Skip to content

perf: eliminate redundant matrix allocation in DenseProductMatrix via vector negation#466

Open
Omar-Abdo1 wants to merge 2 commits intoGeomScale:developfrom
Omar-Abdo1:perf/eigen-memory-optimization
Open

perf: eliminate redundant matrix allocation in DenseProductMatrix via vector negation#466
Omar-Abdo1 wants to merge 2 commits intoGeomScale:developfrom
Omar-Abdo1:perf/eigen-memory-optimization

Conversation

@Omar-Abdo1
Copy link
Copy Markdown

This PR addresses the TODOs in EigenvaluesProblems.h regarding unnecessary matrix allocations.

The Problem:
Previously, the generalized eigenvalue solver allocated a new N×N matrix (_B = -1 * B) just to negate the sign. For high-dimensional polytopes (up to thousands of dimensions as targeted by VolEsti ), this O(N2) space and time allocation is a significant bottleneck.

The Solution:
I modified the DenseProductMatrix class to handle negation "lazily". By introducing a negative flag, the sign inversion is now performed on the intermediate vector v (where v=Ax) inside the perform_op and MultMv methods.

Key Benefits:

Zero Allocation: Replaces N×N matrix allocation with an in-place vector operation.

Complexity Reduction: Moves the negation from O(N2) matrix-level to O(N) vector-level.

Stability: Verified that the build passes 100% and logic remains consistent with the generalized eigenvalue problem Av=−λBv.

@Omar-Abdo1 Omar-Abdo1 changed the title erf: eliminate redundant matrix allocation in DenseProductMatrix via vector negation perf: eliminate redundant matrix allocation in DenseProductMatrix via vector negation Feb 26, 2026
// and next of y to y_out
Eigen::Map<VT> const x(const_cast<double*>(x_in), _rows);
VT const v = *A * x;
VT v = *A * x;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively,

VT v = negative ? *A * x * -1 : *A * x;

Also negative could be a template parameter.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review! Updated to use the ternary operator as suggested. Let me know if there are any further changes needed.

@vissarion vissarion requested a review from TolisChal March 12, 2026 12:55
Copy link
Copy Markdown
Member

@vissarion vissarion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I am OK with this change.

@vissarion
Copy link
Copy Markdown
Member

@TolisChal what do you think?

@Omar-Abdo1
Copy link
Copy Markdown
Author

Thank you for the approval @vissarion!

@TolisChal — happy to make any further adjustments
if you have suggestions. Just let me know.

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