Skip to content

Commit

Permalink
[BZ-981410] avoid eccessive hashtable resize when adding many tuples …
Browse files Browse the repository at this point in the history
…to memory in accumulate node
  • Loading branch information
mariofusco committed Aug 29, 2013
1 parent a11ddd5 commit 090a348
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Expand Up @@ -19,6 +19,7 @@
import org.drools.core.rule.ContextEntry;
import org.drools.core.spi.AlphaNodeFieldConstraint;
import org.drools.core.spi.PropagationContext;
import org.drools.core.util.AbstractHashTable;
import org.drools.core.util.FastIterator;

/**
Expand Down Expand Up @@ -120,11 +121,15 @@ public void doLeftInserts(AccumulateNode accNode,
ContextEntry[] contextEntry = bm.getContext();
BetaConstraints constraints = accNode.getRawConstraints();

boolean leftTupleMemoryEnabled = accNode.isLeftTupleMemoryEnabled();
if (leftTupleMemoryEnabled && srcLeftTuples.insertSize() > 32 && ltm instanceof AbstractHashTable) {
((AbstractHashTable) ltm).ensureCapacity(srcLeftTuples.insertSize());
}

for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();

boolean useLeftMemory = RuleNetworkEvaluator.useLeftMemory(accNode, leftTuple);
boolean useLeftMemory = leftTupleMemoryEnabled || RuleNetworkEvaluator.useLeftMemory(accNode, leftTuple);

if (useLeftMemory) {
ltm.add(leftTuple);
Expand Down Expand Up @@ -204,6 +209,10 @@ public void doRightInserts(AccumulateNode accNode,
ContextEntry[] contextEntry = bm.getContext();
BetaConstraints constraints = accNode.getRawConstraints();

if (srcRightTuples.insertSize() > 32 && rtm instanceof AbstractHashTable) {
((AbstractHashTable) rtm).ensureCapacity(srcRightTuples.insertSize());
}

for (RightTuple rightTuple = srcRightTuples.getInsertFirst(); rightTuple != null; ) {
RightTuple next = rightTuple.getStagedNext();

Expand Down
Expand Up @@ -108,6 +108,17 @@ public void setComparator(final ObjectComparator comparator) {
this.comparator = comparator;
}

public void ensureCapacity(int itemsToBeAdded) {
int newCapacity = this.size + itemsToBeAdded;
if (newCapacity > this.threshold) {
int newSize = this.table.length * 2;
while (newSize < newCapacity) {
newSize *= 2;
}
resize(newSize);
}
}

protected void resize(final int newCapacity) {
final Entry[] oldTable = this.table;
final int oldCapacity = oldTable.length;
Expand Down

0 comments on commit 090a348

Please sign in to comment.