Skip to content

Commit

Permalink
Stop maintaining the domain size for IntervalIntVarImpl. (#1050)
Browse files Browse the repository at this point in the history
For such variables, the solver used to track the lower-bound, the
upper-bound and the domain size to provide quick access to the
domain size. This decision had however a significant impact in
terms of memory usage. It requires to have an additional StoredInt
per variable and for every bound change, 2 values where saved into
the stack.

In this patch, the domain size is no longer stored but computed on
demand. In terms of performance, getDomainSize() performs now 2
 get() and one subtraction while it used to perform one get(). On
 the other side it saves one StoredInt, and only one value is saved
into the stack when a bound is updated.

After some internal experiments on problems having around 100,000
bounded variables and 50,000 enum variables, this reduces the
memory consumption of my JVM by about 30% with no performance
degradation.
  • Loading branch information
fhermeni authored Jul 6, 2023
1 parent f4136ee commit 7f57e1c
Showing 1 changed file with 3 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ public final class IntervalIntVarImpl extends AbstractVariable implements IntVar
* Upper bound of the current domain
*/
private final IStateInt UB;
/**
* Current size of domain
*/
private final IStateInt SIZE;
/**
* To iterate over removed values
*/
Expand Down Expand Up @@ -92,7 +88,6 @@ public IntervalIntVarImpl(String name, int min, int max, Model model) {
IEnvironment env = model.getEnvironment();
this.LB = env.makeInt(min);
this.UB = env.makeInt(max);
this.SIZE = env.makeInt(max - min + 1);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -220,7 +215,6 @@ public boolean instantiateTo(int value, ICause cause) throws ContradictionExcept
}
this.LB.set(value);
this.UB.set(value);
this.SIZE.set(1);
this.notifyPropagators(e, cause);
return true;
}
Expand Down Expand Up @@ -258,7 +252,6 @@ public boolean updateLowerBound(int value, ICause cause) throws ContradictionExc
if (reactOnRemoval) {
delta.add(old, value - 1, cause);
}
SIZE.add(old - value);
LB.set(value);
if (isInstantiated()) {
e = IntEventType.INSTANTIATE;
Expand Down Expand Up @@ -301,7 +294,6 @@ public boolean updateUpperBound(int value, ICause cause) throws ContradictionExc
if (reactOnRemoval) {
delta.add(value + 1, old, cause);
}
SIZE.add(value - old);
UB.set(value);

if (isInstantiated()) {
Expand Down Expand Up @@ -348,7 +340,6 @@ public boolean updateBounds(int lb, int ub, ICause cause) throws ContradictionEx
d += ub - oub;
UB.set(ub);
}
SIZE.add(d);
if (isInstantiated()) {
e = IntEventType.INSTANTIATE;
}
Expand All @@ -360,7 +351,7 @@ public boolean updateBounds(int lb, int ub, ICause cause) throws ContradictionEx

@Override
public boolean isInstantiated() {
return SIZE.get() == 1;
return LB.get() == UB.get();
}

@Override
Expand Down Expand Up @@ -404,7 +395,7 @@ public int getUB() {

@Override
public int getDomainSize() {
return SIZE.get();
return UB.get() - LB.get() + 1;
}

@Override
Expand Down Expand Up @@ -468,7 +459,7 @@ public IIntervalDelta getDelta() {

@Override
public String toString() {
if (SIZE.get() == 1) {
if (LB.get() == UB.get()) {
return String.format("%s = %d", name, getLB());
}
return String.format("%s = [%d,%d]", name, getLB(), getUB());
Expand Down

0 comments on commit 7f57e1c

Please sign in to comment.