Skip to content

Commit e600bc3

Browse files
committed
do not create new expressions on no-op modifications
1 parent 98593da commit e600bc3

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

ortools/linear_solver/wrappers/model_builder_helper.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,10 +1083,12 @@ void AffineExpr::Visit(ExprVisitor& lin, double c) {
10831083
}
10841084

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

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

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

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

ortools/sat/python/linear_expr.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,10 +611,12 @@ std::string IntAffine::DebugString() const {
611611
}
612612

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

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

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

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

0 commit comments

Comments
 (0)