Skip to content

Commit

Permalink
do not create new expressions on no-op modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
lperron committed Jan 19, 2025
1 parent 98593da commit e600bc3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ortools/linear_solver/wrappers/model_builder_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1083,10 +1083,12 @@ void AffineExpr::Visit(ExprVisitor& lin, double c) {
}

std::shared_ptr<LinearExpr> AffineExpr::AddFloat(double cst) {
if (cst == 0.0) return shared_from_this();
return LinearExpr::Affine(expr_, coeff_, offset_ + cst);
}

std::shared_ptr<LinearExpr> AffineExpr::SubFloat(double cst) {
if (cst == 0.0) return shared_from_this();
return LinearExpr::Affine(expr_, coeff_, offset_ - cst);
}

Expand All @@ -1095,6 +1097,8 @@ std::shared_ptr<LinearExpr> AffineExpr::RSubFloat(double cst) {
}

std::shared_ptr<LinearExpr> AffineExpr::MulFloat(double cst) {
if (cst == 0.0) return std::make_shared<FixedValue>(0);
if (cst == 1.0) return shared_from_this();
return LinearExpr::Affine(expr_, coeff_ * cst, offset_ * cst);
}

Expand Down
4 changes: 4 additions & 0 deletions ortools/sat/python/linear_expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -611,10 +611,12 @@ std::string IntAffine::DebugString() const {
}

std::shared_ptr<LinearExpr> IntAffine::AddInt(int64_t cst) {
if (cst == 0) return shared_from_this();
return LinearExpr::AffineInt(expr_, coeff_, offset_ + cst);
}

std::shared_ptr<LinearExpr> IntAffine::SubInt(int64_t cst) {
if (cst == 0) return shared_from_this();
return LinearExpr::AffineInt(expr_, coeff_, offset_ - cst);
}

Expand All @@ -623,6 +625,8 @@ std::shared_ptr<LinearExpr> IntAffine::RSubInt(int64_t cst) {
}

std::shared_ptr<LinearExpr> IntAffine::MulInt(int64_t cst) {
if (cst == 0) return std::make_shared<IntConstant>(0);
if (cst == 1) return shared_from_this();
return LinearExpr::AffineInt(expr_, coeff_ * cst, offset_ * cst);
}

Expand Down

0 comments on commit e600bc3

Please sign in to comment.