From 3de05e07086ae761fb742f8bb0ebd95f38559650 Mon Sep 17 00:00:00 2001 From: Mario Fusco Date: Fri, 25 Aug 2023 12:32:13 +0200 Subject: [PATCH] minor performances improvements (#5487) * minor performances improvements * wip * wip * wip * wip --- .../core/common/PhreakPropagationContext.java | 28 ++++++++--------- .../core/common/PropagationContext.java | 9 +++--- .../core/phreak/PhreakRuleTerminalNode.java | 30 +++++++++++-------- .../drools/core/phreak/PhreakTimerNode.java | 14 ++++----- .../org/drools/core/phreak/RuleExecutor.java | 5 +++- .../org/drools/core/reteoo/LeftTuple.java | 4 --- .../drools/core/reteoo/QueryElementNode.java | 18 +++++------ .../reteoo/RuleTerminalNodeLeftTuple.java | 11 +++++-- 8 files changed, 64 insertions(+), 55 deletions(-) diff --git a/drools-core/src/main/java/org/drools/core/common/PhreakPropagationContext.java b/drools-core/src/main/java/org/drools/core/common/PhreakPropagationContext.java index d0942233daa..d5985409159 100644 --- a/drools-core/src/main/java/org/drools/core/common/PhreakPropagationContext.java +++ b/drools-core/src/main/java/org/drools/core/common/PhreakPropagationContext.java @@ -16,25 +16,29 @@ package org.drools.core.common; -import java.io.IOException; -import java.io.ObjectInput; -import java.io.ObjectOutput; -import java.util.Collections; -import java.util.List; - import org.drools.base.base.ClassObjectType; +import org.drools.base.base.ObjectType; import org.drools.base.definitions.InternalKnowledgePackage; import org.drools.base.definitions.rule.impl.RuleImpl; -import org.drools.core.marshalling.MarshallerReaderContext; import org.drools.base.reteoo.PropertySpecificUtil; -import org.drools.core.reteoo.TerminalNode; import org.drools.base.rule.EntryPointId; import org.drools.base.rule.TypeDeclaration; -import org.drools.base.base.ObjectType; +import org.drools.core.marshalling.MarshallerReaderContext; +import org.drools.core.reteoo.TerminalNode; import org.drools.core.util.bitmask.BitMask; import org.kie.api.runtime.rule.FactHandle; -import static org.drools.base.reteoo.PropertySpecificUtil.*; +import java.io.IOException; +import java.io.ObjectInput; +import java.io.ObjectOutput; +import java.util.Collections; +import java.util.List; + +import static org.drools.base.reteoo.PropertySpecificUtil.allSetBitMask; +import static org.drools.base.reteoo.PropertySpecificUtil.getEmptyPropertyReactiveMask; +import static org.drools.base.reteoo.PropertySpecificUtil.isAllSetPropertyReactiveMask; +import static org.drools.base.reteoo.PropertySpecificUtil.isPropertySetOnMask; +import static org.drools.base.reteoo.PropertySpecificUtil.setPropertyOnMask; public class PhreakPropagationContext implements @@ -208,10 +212,6 @@ public BitMask getModificationMask() { return modificationMask; } - public void setModificationMask( BitMask modificationMask ) { - this.modificationMask = modificationMask; - } - public PropagationContext adaptModificationMaskForObjectType(ObjectType type, ReteEvaluator reteEvaluator) { if (isAllSetPropertyReactiveMask(originalMask) || originalMask.isSet(PropertySpecificUtil.TRAITABLE_BIT) || !(type instanceof ClassObjectType)) { return this; diff --git a/drools-core/src/main/java/org/drools/core/common/PropagationContext.java b/drools-core/src/main/java/org/drools/core/common/PropagationContext.java index db725dc2b66..9b41fbdf2a3 100644 --- a/drools-core/src/main/java/org/drools/core/common/PropagationContext.java +++ b/drools-core/src/main/java/org/drools/core/common/PropagationContext.java @@ -16,16 +16,16 @@ package org.drools.core.common; -import java.io.Externalizable; - +import org.drools.base.base.ObjectType; import org.drools.base.definitions.rule.impl.RuleImpl; +import org.drools.base.rule.EntryPointId; import org.drools.core.marshalling.MarshallerReaderContext; import org.drools.core.reteoo.TerminalNode; -import org.drools.base.rule.EntryPointId; -import org.drools.base.base.ObjectType; import org.drools.core.util.bitmask.BitMask; import org.kie.api.runtime.rule.FactHandle; +import java.io.Externalizable; + public interface PropagationContext extends Externalizable { enum Type { @@ -50,7 +50,6 @@ enum Type { BitMask getModificationMask(); PropagationContext adaptModificationMaskForObjectType(ObjectType type, ReteEvaluator reteEvaluator); - void setModificationMask(BitMask mask); MarshallerReaderContext getReaderContext(); diff --git a/drools-core/src/main/java/org/drools/core/phreak/PhreakRuleTerminalNode.java b/drools-core/src/main/java/org/drools/core/phreak/PhreakRuleTerminalNode.java index 50645ffd6a1..1ea2957922f 100644 --- a/drools-core/src/main/java/org/drools/core/phreak/PhreakRuleTerminalNode.java +++ b/drools-core/src/main/java/org/drools/core/phreak/PhreakRuleTerminalNode.java @@ -98,10 +98,14 @@ public static void doLeftTupleInsert(TerminalNode rtnNode, RuleExecutor executor return; } - PropagationContext pctx = leftTuple.findMostRecentPropagationContext(); - - if ( rtnNode.getRule().isNoLoop() && sameRules(rtnNode, pctx.getTerminalNodeOrigin()) ) { - return; + PropagationContext pctx; + if ( rtnNode.getRule().isNoLoop() ) { + pctx = leftTuple.findMostRecentPropagationContext(); + if ( sameRules(rtnNode, pctx.getTerminalNodeOrigin()) ) { + return; + } + } else { + pctx = leftTuple.getPropagationContext(); } int salienceInt = getSalienceValue(rtnNode, ruleAgendaItem, (InternalMatch) leftTuple, reteEvaluator); @@ -111,8 +115,8 @@ public static void doLeftTupleInsert(TerminalNode rtnNode, RuleExecutor executor activationsManager.getAgendaEventSupport().fireActivationCreated(rtnLeftTuple, activationsManager.getReteEvaluator()); - if ( rtnNode.getRule().isLockOnActive() && - leftTuple.getPropagationContext().getType() != PropagationContext.Type.RULE_ADDITION ) { + if ( rtnNode.getRule().isLockOnActive() && pctx.getType() != PropagationContext.Type.RULE_ADDITION ) { + pctx = leftTuple.findMostRecentPropagationContext(); InternalAgendaGroup agendaGroup = executor.getRuleAgendaItem().getAgendaGroup(); if (blockedByLockOnActive(rtnNode.getRule(), pctx, agendaGroup)) { activationsManager.getAgendaEventSupport().fireActivationCancelled(rtnLeftTuple, reteEvaluator, MatchCancelledCause.FILTER ); @@ -179,15 +183,18 @@ public static void doLeftTupleUpdate(TerminalNode rtnNode, RuleExecutor executor return; } - PropagationContext pctx = leftTuple.findMostRecentPropagationContext(); + PropagationContext pctx = leftTuple.getPropagationContext(); boolean blocked = false; - if( executor.isDeclarativeAgendaEnabled() ) { + if ( executor.isDeclarativeAgendaEnabled() ) { if ( rtnLeftTuple.hasBlockers() ) { blocked = true; // declarativeAgenda still blocking LeftTuple, so don't add back ot list } } else { - blocked = rtnNode.getRule().isNoLoop() && rtnNode.equals(pctx.getTerminalNodeOrigin()); + if (rtnNode.getRule().isNoLoop()) { + pctx = leftTuple.findMostRecentPropagationContext(); + blocked = rtnNode.equals(pctx.getTerminalNodeOrigin()); + } } int salienceInt = getSalienceValue(rtnNode, executor.getRuleAgendaItem(), (InternalMatch) leftTuple, reteEvaluator); @@ -199,9 +206,8 @@ public static void doLeftTupleUpdate(TerminalNode rtnNode, RuleExecutor executor if ( !blocked ) { boolean addToExector = true; - if ( rtnNode.getRule().isLockOnActive() && - pctx.getType() != PropagationContext.Type.RULE_ADDITION ) { - + if ( rtnNode.getRule().isLockOnActive() && pctx.getType() != PropagationContext.Type.RULE_ADDITION ) { + pctx = leftTuple.findMostRecentPropagationContext(); InternalAgendaGroup agendaGroup = executor.getRuleAgendaItem().getAgendaGroup(); if (blockedByLockOnActive(rtnNode.getRule(), pctx, agendaGroup)) { addToExector = false; diff --git a/drools-core/src/main/java/org/drools/core/phreak/PhreakTimerNode.java b/drools-core/src/main/java/org/drools/core/phreak/PhreakTimerNode.java index 16e55f2c615..5aea4427f62 100644 --- a/drools-core/src/main/java/org/drools/core/phreak/PhreakTimerNode.java +++ b/drools-core/src/main/java/org/drools/core/phreak/PhreakTimerNode.java @@ -15,12 +15,12 @@ package org.drools.core.phreak; -import java.util.Date; -import java.util.List; - +import org.drools.base.common.NetworkNode; +import org.drools.base.time.JobHandle; +import org.drools.base.time.Trigger; +import org.drools.base.time.impl.Timer; import org.drools.core.common.ActivationsManager; import org.drools.core.common.InternalFactHandle; -import org.drools.base.common.NetworkNode; import org.drools.core.common.PropagationContext; import org.drools.core.common.ReteEvaluator; import org.drools.core.common.TupleSets; @@ -36,11 +36,8 @@ import org.drools.core.reteoo.Tuple; import org.drools.core.time.Job; import org.drools.core.time.JobContext; -import org.drools.base.time.JobHandle; import org.drools.core.time.TimerService; -import org.drools.base.time.Trigger; import org.drools.core.time.impl.DefaultJobHandle; -import org.drools.base.time.impl.Timer; import org.drools.core.util.LinkedList; import org.drools.core.util.index.TupleList; import org.kie.api.definition.rule.Rule; @@ -49,6 +46,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.Date; +import java.util.List; + import static org.drools.core.phreak.BuildtimeSegmentUtilities.nextNodePosMask; import static org.drools.core.phreak.RuleNetworkEvaluator.normalizeStagedTuples; diff --git a/drools-core/src/main/java/org/drools/core/phreak/RuleExecutor.java b/drools-core/src/main/java/org/drools/core/phreak/RuleExecutor.java index 2da9ceff82a..df3b9f06c73 100644 --- a/drools-core/src/main/java/org/drools/core/phreak/RuleExecutor.java +++ b/drools-core/src/main/java/org/drools/core/phreak/RuleExecutor.java @@ -270,7 +270,10 @@ private boolean haltRuleFiring(int fireCount, activationsManager.evaluateEagerList(); RuleAgendaItem nextRule = activationsManager.peekNextRule(); - return nextRule != null && (!ruleAgendaItem.getAgendaGroup().equals( nextRule.getAgendaGroup() ) || !isHigherSalience(nextRule)); + if (nextRule == ruleAgendaItem || nextRule == null) { + return false; + } + return !ruleAgendaItem.getAgendaGroup().equals( nextRule.getAgendaGroup() ) || !isHigherSalience(nextRule); } private boolean isHigherSalience(RuleAgendaItem nextRule) { diff --git a/drools-core/src/main/java/org/drools/core/reteoo/LeftTuple.java b/drools-core/src/main/java/org/drools/core/reteoo/LeftTuple.java index 480a5b53aa2..9801285fef3 100644 --- a/drools-core/src/main/java/org/drools/core/reteoo/LeftTuple.java +++ b/drools-core/src/main/java/org/drools/core/reteoo/LeftTuple.java @@ -665,8 +665,4 @@ public Collection getAccumulatedObjects() { } return result; } - - public InternalFactHandle getFactHandle() { - return (InternalFactHandle) super.getFactHandle(); - } } diff --git a/drools-core/src/main/java/org/drools/core/reteoo/QueryElementNode.java b/drools-core/src/main/java/org/drools/core/reteoo/QueryElementNode.java index d2224ba7da4..99bcc21fd3a 100644 --- a/drools-core/src/main/java/org/drools/core/reteoo/QueryElementNode.java +++ b/drools-core/src/main/java/org/drools/core/reteoo/QueryElementNode.java @@ -16,33 +16,33 @@ package org.drools.core.reteoo; -import java.util.List; - +import org.drools.base.definitions.rule.impl.QueryImpl; +import org.drools.base.definitions.rule.impl.RuleImpl; import org.drools.base.reteoo.NodeTypeEnums; +import org.drools.base.rule.Declaration; +import org.drools.base.rule.QueryArgument; +import org.drools.base.rule.QueryElement; import org.drools.core.RuleBaseConfiguration; import org.drools.core.base.DroolsQueryImpl; import org.drools.core.base.InternalViewChangedEventListener; import org.drools.core.common.InternalFactHandle; import org.drools.core.common.Memory; import org.drools.core.common.MemoryFactory; +import org.drools.core.common.PropagationContext; import org.drools.core.common.QueryElementFactHandle; import org.drools.core.common.ReteEvaluator; import org.drools.core.common.TupleSets; import org.drools.core.common.TupleSetsImpl; import org.drools.core.common.UpdateContext; -import org.drools.base.definitions.rule.impl.RuleImpl; import org.drools.core.phreak.BuildtimeSegmentUtilities; import org.drools.core.phreak.StackEntry; import org.drools.core.reteoo.builder.BuildContext; -import org.drools.base.rule.Declaration; -import org.drools.base.rule.QueryArgument; -import org.drools.base.rule.QueryElement; -import org.drools.base.definitions.rule.impl.QueryImpl; import org.drools.core.rule.consequence.InternalMatch; -import org.drools.core.common.PropagationContext; import org.drools.core.util.AbstractBaseLinkedListNode; import org.kie.api.runtime.rule.FactHandle; +import java.util.List; + public class QueryElementNode extends LeftTupleSource implements LeftTupleSinkNode, MemoryFactory { private LeftTupleSinkNode previousTupleSinkNode; @@ -268,7 +268,7 @@ public void rowAdded(RuleImpl rule, LeftTuple resultLeftTuple, ReteEvaluator ret objects[variable] = decl.getValue(reteEvaluator, resultLeftTuple); } - QueryElementFactHandle resultHandle = createQueryResultHandle(leftTuple.findMostRecentPropagationContext(), + QueryElementFactHandle resultHandle = createQueryResultHandle(leftTuple.getPropagationContext(), reteEvaluator, objects); diff --git a/drools-core/src/main/java/org/drools/core/reteoo/RuleTerminalNodeLeftTuple.java b/drools-core/src/main/java/org/drools/core/reteoo/RuleTerminalNodeLeftTuple.java index 1fcf74e1824..cd3c5e2ef88 100644 --- a/drools-core/src/main/java/org/drools/core/reteoo/RuleTerminalNodeLeftTuple.java +++ b/drools-core/src/main/java/org/drools/core/reteoo/RuleTerminalNodeLeftTuple.java @@ -60,6 +60,7 @@ public class RuleTerminalNodeLeftTuple extends LeftTuple implements InternalMatc private Runnable callback; private RuleImpl rule; + private Consequence consequence; public RuleTerminalNodeLeftTuple() { // constructor needed for serialisation @@ -138,7 +139,12 @@ public void update(final int salience, @Override protected void setSink(Sink sink) { super.setSink(sink); - this.rule = getTerminalNode().getRule(); + TerminalNode terminalNode = (TerminalNode) sink; + this.rule = terminalNode.getRule(); + if (this.rule != null && terminalNode instanceof RuleTerminalNode) { + String consequenceName = ((RuleTerminalNode)terminalNode).getConsequenceName(); + this.consequence = consequenceName.equals(RuleImpl.DEFAULT_CONSEQUENCE_NAME) ? rule.getConsequence() : rule.getNamedConsequence(consequenceName); + } } /** @@ -151,8 +157,7 @@ public RuleImpl getRule() { } public Consequence getConsequence() { - String consequenceName = ((RuleTerminalNode) getTerminalNode()).getConsequenceName(); - return consequenceName.equals(RuleImpl.DEFAULT_CONSEQUENCE_NAME) ? getRule().getConsequence() : getRule().getNamedConsequence(consequenceName); + return consequence; } /**