diff --git a/drools-core/src/main/java/org/drools/core/phreak/PhreakAccumulateNode.java b/drools-core/src/main/java/org/drools/core/phreak/PhreakAccumulateNode.java index 82f452b1672..a85b117d5f7 100644 --- a/drools-core/src/main/java/org/drools/core/phreak/PhreakAccumulateNode.java +++ b/drools-core/src/main/java/org/drools/core/phreak/PhreakAccumulateNode.java @@ -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; /** @@ -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); @@ -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(); diff --git a/drools-core/src/main/java/org/drools/core/util/AbstractHashTable.java b/drools-core/src/main/java/org/drools/core/util/AbstractHashTable.java index 8f51e5dc5d8..9e78a4adf67 100644 --- a/drools-core/src/main/java/org/drools/core/util/AbstractHashTable.java +++ b/drools-core/src/main/java/org/drools/core/util/AbstractHashTable.java @@ -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;