Skip to content

Commit

Permalink
[DROOLS-7528] Avoid casting to unnecessary interfaces (Fix the second…
Browse files Browse the repository at this point in the history
…ary super cache problem in Drools) (#5451)

* [DROOLS-7528] Avoid casting to unnecessary interfaces (Fix the secondary super cache problem in Drools)

* Reintroduce fix in LeftTuple

* The TupleSink is not always a BetaNode

* Removed leftover
  • Loading branch information
lucamolteni committed Aug 31, 2023
1 parent 0382b91 commit abbfa6f
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1643,7 +1643,7 @@ private static int updateRiaAndTerminalMemory( LeftTupleSource lt,
}

} else if (NodeTypeEnums.isTerminalNode(sink)) {
pmem = (PathMemory) reteEvaluator.getNodeMemory((MemoryFactory) sink);
pmem = reteEvaluator.getNodeMemory((AbstractTerminalNode) sink);
}

if (pmem != null && smem.getPos() < pmem.getSegmentMemories().length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.drools.core.common.ReteEvaluator;
import org.drools.core.common.TupleSets;
import org.drools.core.common.TupleSetsImpl;
import org.drools.core.reteoo.AbstractTerminalNode;
import org.drools.core.reteoo.AccumulateNode;
import org.drools.core.reteoo.AccumulateNode.AccumulateMemory;
import org.drools.core.reteoo.AsyncReceiveNode;
Expand Down Expand Up @@ -327,7 +328,7 @@ public void innerEval(PathMemory pmem,
boolean terminalNode = true;
switch (node.getType()) {
case NodeTypeEnums.RuleTerminalNode:
pRtNode.doNode(( TerminalNode ) node, activationsManager, srcTuples, executor);
pRtNode.doNode((AbstractTerminalNode) node, activationsManager, srcTuples, executor);
break;
case NodeTypeEnums.QueryTerminalNode:
pQtNode.doNode((QueryTerminalNode) node, activationsManager, srcTuples, stack);
Expand Down
14 changes: 13 additions & 1 deletion drools-core/src/main/java/org/drools/core/reteoo/LeftTuple.java
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,20 @@ public int getIndex() {
}

@Override
// It's better to always cast to a concrete or abstract class to avoid
// secondary super cache problem. See https://issues.redhat.com/browse/DROOLS-7521
public LeftTupleSink getTupleSink() {
return (LeftTupleSink)getSink();
Object sink = getSink();
if (sink instanceof AccumulateNode) {
return (AccumulateNode) sink;
} else if (sink instanceof RuleTerminalNode) {
return (RuleTerminalNode) sink;
} else if (sink instanceof RightInputAdapterNode) {
return (RightInputAdapterNode) sink;
} else if (sink instanceof ExistsNode) {
return (ExistsNode) sink;
}
return (LeftTupleSink)sink;
}

/* Had to add the set method because sink adapters must override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public RuleAgendaItem getOrCreateRuleAgendaItem(ActivationsManager activationsMa
}

private TerminalNode ensureAgendaItemCreated(ActivationsManager activationsManager) {
TerminalNode rtn = (TerminalNode) getPathEndNode();
AbstractTerminalNode rtn = (AbstractTerminalNode) getPathEndNode();
if (agendaItem == null) {
int salience = rtn.getRule().getSalience().isDynamic() ? 0 : rtn.getRule().getSalience().getValue();
agendaItem = activationsManager.createRuleAgendaItem(salience, this, rtn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,15 @@ public RightTupleImpl(InternalFactHandle handle,
handle.addLastRightTuple( this );
}

// It's better to always cast to a concrete or abstract class to avoid
// secondary super cache problem. See https://issues.redhat.com/browse/DROOLS-7521
public RightTupleSink getTupleSink() {
return (RightTupleSink) getSink();
Object sink = getSink();
if(sink instanceof BetaNode) {
return (BetaNode)sink;
} else {
return (RightTupleSink) sink;
}
}

public void reAdd() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public void setActivationNode(final ActivationNode activationNode) {
}

public TerminalNode getTerminalNode() {
return (TerminalNode) getTupleSink();
return (AbstractTerminalNode) getTupleSink();
}

public List<FactHandle> getFactHandles() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,19 @@ public BaseNode getMatchingNode(BaseNode candidate) {
public LeftTupleSink[] getSinks() {
return sinkArray;
}


// See LeftTuple.getTupleSink() or https://issues.redhat.com/browse/DROOLS-7521
public LeftTupleSinkNode getFirstLeftTupleSink() {
return ( LeftTupleSinkNode ) sink;
if (sink instanceof AccumulateNode) {
return (AccumulateNode) sink;
} else if (sink instanceof RuleTerminalNode) {
return (RuleTerminalNode) sink;
} else if (sink instanceof RightInputAdapterNode) {
return (RightInputAdapterNode) sink;
} else if (sink instanceof ExistsNode) {
return (ExistsNode) sink;
}
return (LeftTupleSinkNode) sink;
}

public LeftTupleSinkNode getLastLeftTupleSink() {
Expand Down

0 comments on commit abbfa6f

Please sign in to comment.