Skip to content
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

refactor sub_assign DensePolynomial #906

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions poly/src/polynomial/univariate/dense.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,8 +509,7 @@ impl<'a, F: Field> Sub<&'a SparsePolynomial<F>> for &DensePolynomial<F> {
#[inline]
fn sub(self, other: &'a SparsePolynomial<F>) -> DensePolynomial<F> {
if self.is_zero() {
let result = other.clone();
(-result).into()
(-(other.clone())).into()
} else if other.is_zero() {
self.clone()
} else {
Expand Down Expand Up @@ -538,24 +537,21 @@ impl<'a, F: Field> SubAssign<&'a DensePolynomial<F>> for DensePolynomial<F> {
#[inline]
fn sub_assign(&mut self, other: &'a DensePolynomial<F>) {
if self.is_zero() {
self.coeffs.resize(other.coeffs.len(), F::zero());
} else if other.is_zero() {
return;
} else if self.degree() >= other.degree() {
} else {
// Add the necessary number of zero coefficients.
self.coeffs.resize(other.coeffs.len(), F::zero());
self.coeffs = other.coeffs.iter().map(|&x| -x).collect();
Copy link
Member

@Pratyush Pratyush Dec 24, 2024

Choose a reason for hiding this comment

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

The hope with the old way was that if self.coeffs has capacity large enough (e.g., it was the result of a previous operation that resulted in a zero), then we can avoid re-allocation via the collect.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yes you are right didn't notice that. Then there is no point to refactor this, this is already optimized, let us close :)

} else if !other.is_zero() {
if self.degree() < other.degree() {
self.coeffs.resize(other.coeffs.len(), F::zero());
}
self.coeffs
.iter_mut()
.zip(&other.coeffs)
.for_each(|(a, b)| *a -= b);

// If the leading coefficient ends up being zero, pop it off.
// This can happen if they were the same degree, or if other's
// coefficients were constructed with leading zeros.
self.truncate_leading_zeros();
}
self.coeffs
.iter_mut()
.zip(&other.coeffs)
.for_each(|(a, b)| {
*a -= b;
});
// If the leading coefficient ends up being zero, pop it off.
// This can happen if they were the same degree, or if other's
// coefficients were constructed with leading zeros.
self.truncate_leading_zeros();
}
}

Expand Down
Loading