Skip to content
Permalink
Browse files

Use List instead of IList for faster iteration

  • Loading branch information...
snikolayev committed Apr 4, 2019
1 parent a4abadf commit b7a8727b0d53078a36ab2a61749a1e9c7cf3a0a3
@@ -21,7 +21,7 @@ public AggregateNode(ITupleSource leftSource, IObjectSource rightSource, string
_isSubnetJoin = isSubnetJoin;
}

public override void PropagateAssert(IExecutionContext context, IList<Tuple> tuples)
public override void PropagateAssert(IExecutionContext context, List<Tuple> tuples)
{
var aggregationContext = new AggregationContext(context.Session, context.EventAggregator, NodeInfo);
var joinedSets = JoinedSets(context, tuples);
@@ -35,7 +35,7 @@ public override void PropagateAssert(IExecutionContext context, IList<Tuple> tup
PropagateAggregation(context, aggregation);
}

public override void PropagateUpdate(IExecutionContext context, IList<Tuple> tuples)
public override void PropagateUpdate(IExecutionContext context, List<Tuple> tuples)
{
var aggregationContext = new AggregationContext(context.Session, context.EventAggregator, NodeInfo);
var joinedSets = JoinedSets(context, tuples);
@@ -63,7 +63,7 @@ public override void PropagateUpdate(IExecutionContext context, IList<Tuple> tup
PropagateAggregation(context, aggregation);
}

public override void PropagateRetract(IExecutionContext context, IList<Tuple> tuples)
public override void PropagateRetract(IExecutionContext context, List<Tuple> tuples)
{
var aggregation = new Aggregation();
foreach (var tuple in tuples)
@@ -77,7 +77,7 @@ public override void PropagateRetract(IExecutionContext context, IList<Tuple> tu
PropagateAggregation(context, aggregation);
}

public override void PropagateAssert(IExecutionContext context, IList<Fact> facts)
public override void PropagateAssert(IExecutionContext context, List<Fact> facts)
{
var aggregationContext = new AggregationContext(context.Session, context.EventAggregator, NodeInfo);
var joinedSets = JoinedSets(context, facts);
@@ -104,7 +104,7 @@ public override void PropagateAssert(IExecutionContext context, IList<Fact> fact
PropagateAggregation(context, aggregation);
}

public override void PropagateUpdate(IExecutionContext context, IList<Fact> facts)
public override void PropagateUpdate(IExecutionContext context, List<Fact> facts)
{
var aggregationContext = new AggregationContext(context.Session, context.EventAggregator, NodeInfo);
var joinedSets = JoinedSets(context, facts);
@@ -132,7 +132,7 @@ public override void PropagateUpdate(IExecutionContext context, IList<Fact> fact
PropagateAggregation(context, aggregation);
}

public override void PropagateRetract(IExecutionContext context, IList<Fact> facts)
public override void PropagateRetract(IExecutionContext context, List<Fact> facts)
{
var aggregationContext = new AggregationContext(context.Session, context.EventAggregator, NodeInfo);
var joinedSets = JoinedSets(context, facts);
@@ -192,7 +192,7 @@ private void UpdateInAggregate(AggregationContext context, IFactAggregator aggre
}
}

private void RetractFromAggregate(AggregationContext context, IFactAggregator aggregator, Aggregation aggregation, Tuple tuple, IList<Fact> facts)
private void RetractFromAggregate(AggregationContext context, IFactAggregator aggregator, Aggregation aggregation, Tuple tuple, List<Fact> facts)
{
try
{
@@ -13,7 +13,7 @@ internal class AlphaMemoryNode : IObjectSink, IAlphaMemoryNode

public IEnumerable<IObjectSink> Sinks => _sinks;

public void PropagateAssert(IExecutionContext context, IList<Fact> facts)
public void PropagateAssert(IExecutionContext context, List<Fact> facts)
{
IAlphaMemory memory = context.WorkingMemory.GetNodeMemory(this);
foreach (var sink in _sinks)
@@ -23,7 +23,7 @@ public void PropagateAssert(IExecutionContext context, IList<Fact> facts)
memory.Add(facts);
}

public void PropagateUpdate(IExecutionContext context, IList<Fact> facts)
public void PropagateUpdate(IExecutionContext context, List<Fact> facts)
{
IAlphaMemory memory = context.WorkingMemory.GetNodeMemory(this);
var toUpdate = new List<Fact>();
@@ -48,7 +48,7 @@ public void PropagateUpdate(IExecutionContext context, IList<Fact> facts)
}
}

public void PropagateRetract(IExecutionContext context, IList<Fact> facts)
public void PropagateRetract(IExecutionContext context, List<Fact> facts)
{
IAlphaMemory memory = context.WorkingMemory.GetNodeMemory(this);
var toRetract = new List<Fact>(facts.Count);
@@ -19,7 +19,7 @@ protected AlphaNode()

public abstract bool IsSatisfiedBy(IExecutionContext context, Fact fact);

public void PropagateAssert(IExecutionContext context, IList<Fact> facts)
public void PropagateAssert(IExecutionContext context, List<Fact> facts)
{
var toAssert = new List<Fact>();
foreach (var fact in facts)
@@ -38,7 +38,7 @@ public void PropagateAssert(IExecutionContext context, IList<Fact> facts)
}
}

public void PropagateUpdate(IExecutionContext context, IList<Fact> facts)
public void PropagateUpdate(IExecutionContext context, List<Fact> facts)
{
var toUpdate = new List<Fact>();
var toRetract = new List<Fact>();
@@ -64,7 +64,7 @@ public void PropagateUpdate(IExecutionContext context, IList<Fact> facts)
}
}

public void PropagateRetract(IExecutionContext context, IList<Fact> facts)
public void PropagateRetract(IExecutionContext context, List<Fact> facts)
{
foreach (var childNode in ChildNodes)
{
@@ -73,7 +73,7 @@ public void PropagateRetract(IExecutionContext context, IList<Fact> facts)
MemoryNode?.PropagateRetract(context, facts);
}

protected virtual void UnsatisfiedFactUpdate(IExecutionContext context, IList<Fact> facts)
protected virtual void UnsatisfiedFactUpdate(IExecutionContext context, List<Fact> facts)
{
PropagateRetract(context, facts);
}
@@ -5,9 +5,9 @@ namespace NRules.Rete
internal interface IBetaMemoryNode : ITupleSource, INode
{
IEnumerable<ITupleSink> Sinks { get; }
void PropagateAssert(IExecutionContext context, ITupleFactList tupleFactList);
void PropagateUpdate(IExecutionContext context, ITupleFactList tupleFactList);
void PropagateRetract(IExecutionContext context, ITupleFactList tupleFactList);
void PropagateAssert(IExecutionContext context, TupleFactList tupleFactList);
void PropagateUpdate(IExecutionContext context, TupleFactList tupleFactList);
void PropagateRetract(IExecutionContext context, TupleFactList tupleFactList);
}

internal class BetaMemoryNode : IBetaMemoryNode
@@ -16,7 +16,7 @@ internal class BetaMemoryNode : IBetaMemoryNode

public IEnumerable<ITupleSink> Sinks => _sinks;

public void PropagateAssert(IExecutionContext context, ITupleFactList tupleFactList)
public void PropagateAssert(IExecutionContext context, TupleFactList tupleFactList)
{
if (tupleFactList.Count == 0) return;
IBetaMemory memory = context.WorkingMemory.GetNodeMemory(this);
@@ -32,7 +32,7 @@ public void PropagateAssert(IExecutionContext context, ITupleFactList tupleFactL
PropagateAssertInternal(context, memory, toAssert);
}

public void PropagateUpdate(IExecutionContext context, ITupleFactList tupleFactList)
public void PropagateUpdate(IExecutionContext context, TupleFactList tupleFactList)
{
if (tupleFactList.Count == 0) return;
IBetaMemory memory = context.WorkingMemory.GetNodeMemory(this);
@@ -58,7 +58,7 @@ public void PropagateUpdate(IExecutionContext context, ITupleFactList tupleFactL
PropagateUpdateInternal(context, toUpdate);
}

public void PropagateRetract(IExecutionContext context, ITupleFactList tupleFactList)
public void PropagateRetract(IExecutionContext context, TupleFactList tupleFactList)
{
if (tupleFactList.Count == 0) return;
IBetaMemory memory = context.WorkingMemory.GetNodeMemory(this);
@@ -12,9 +12,9 @@ protected BetaNode()
public NodeDebugInfo NodeInfo { get; }
public IBetaMemoryNode MemoryNode { get; set; }

public abstract void PropagateAssert(IExecutionContext context, IList<Tuple> tuples);
public abstract void PropagateUpdate(IExecutionContext context, IList<Tuple> tuples);
public abstract void PropagateRetract(IExecutionContext context, IList<Tuple> tuples);
public abstract void PropagateAssert(IExecutionContext context, List<Tuple> tuples);
public abstract void PropagateUpdate(IExecutionContext context, List<Tuple> tuples);
public abstract void PropagateRetract(IExecutionContext context, List<Tuple> tuples);

public abstract void Accept<TContext>(TContext context, ReteNodeVisitor<TContext> visitor);
}
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace NRules.Rete
@@ -21,9 +20,9 @@ protected BinaryBetaNode(ITupleSource leftSource, IObjectSource rightSource)
RightSource.Attach(this);
}

public abstract void PropagateAssert(IExecutionContext context, IList<Fact> facts);
public abstract void PropagateUpdate(IExecutionContext context, IList<Fact> facts);
public abstract void PropagateRetract(IExecutionContext context, IList<Fact> facts);
public abstract void PropagateAssert(IExecutionContext context, List<Fact> facts);
public abstract void PropagateUpdate(IExecutionContext context, List<Fact> facts);
public abstract void PropagateRetract(IExecutionContext context, List<Fact> facts);

protected TupleFactSet JoinedSet(IExecutionContext context, Tuple tuple)
{
@@ -38,7 +37,7 @@ protected TupleFactSet JoinedSet(IExecutionContext context, Tuple tuple)
return new TupleFactSet(tuple, facts);
}

protected IEnumerable<TupleFactSet> JoinedSets(IExecutionContext context, IList<Tuple> tuples)
protected IEnumerable<TupleFactSet> JoinedSets(IExecutionContext context, List<Tuple> tuples)
{
if (tuples.Count == 0) return EmptySetList;
int level = tuples[0].Level;
@@ -54,7 +53,7 @@ protected IEnumerable<TupleFactSet> JoinedSets(IExecutionContext context, IList<
return CrossJoin(tuples, facts);
}

protected IEnumerable<TupleFactSet> JoinedSets(IExecutionContext context, IList<Fact> facts)
protected IEnumerable<TupleFactSet> JoinedSets(IExecutionContext context, List<Fact> facts)
{
var tuples = LeftSource.GetTuples(context).ToList();
if (tuples.Count == 0) return EmptySetList;
@@ -86,7 +85,7 @@ private static TupleFactSet JoinByGroupId(Tuple tuple, IDictionary<long, List<Fa
return tupleFactSet;
}

private IEnumerable<TupleFactSet> CrossJoin(IList<Tuple> tuples, IList<Fact> facts)
private IEnumerable<TupleFactSet> CrossJoin(IList<Tuple> tuples, List<Fact> facts)
{
var sets = new List<TupleFactSet>();
foreach (var tuple in tuples)
@@ -96,7 +95,7 @@ private IEnumerable<TupleFactSet> CrossJoin(IList<Tuple> tuples, IList<Fact> fac
return sets;
}

private IDictionary<long, List<Fact>> GroupFacts(IList<Fact> facts, int level)
private IDictionary<long, List<Fact>> GroupFacts(List<Fact> facts, int level)
{
if (facts.Count == 0 || !facts[0].IsWrapperFact) return EmptyGroups;

@@ -105,14 +104,14 @@ private IEnumerable<TupleFactSet> CrossJoin(IList<Tuple> tuples, IList<Fact> fac
var factGroups = new Dictionary<long, List<Fact>>();
foreach (var fact in facts)
{
var wrapperfact = (WrapperFact) fact;
long groupId = wrapperfact.WrappedTuple.GetGroupId(level);
var wrapperFact = (WrapperFact) fact;
long groupId = wrapperFact.WrappedTuple.GetGroupId(level);
if (!factGroups.TryGetValue(groupId, out var factGroup))
{
factGroup = new List<Fact>();
factGroups[groupId] = factGroup;
}
factGroup.Add(wrapperfact);
factGroup.Add(wrapperFact);
}
return factGroups;
}
@@ -18,7 +18,7 @@ public BindingNode(IBindingExpression bindingExpression, Type resultType, ITuple
Source.Attach(this);
}

public override void PropagateAssert(IExecutionContext context, IList<Tuple> tuples)
public override void PropagateAssert(IExecutionContext context, List<Tuple> tuples)
{
var toAssert = new TupleFactList();
foreach (var tuple in tuples)
@@ -28,7 +28,7 @@ public override void PropagateAssert(IExecutionContext context, IList<Tuple> tup
MemoryNode.PropagateAssert(context, toAssert);
}

public override void PropagateUpdate(IExecutionContext context, IList<Tuple> tuples)
public override void PropagateUpdate(IExecutionContext context, List<Tuple> tuples)
{
var toAssert = new TupleFactList();
var toUpdate = new TupleFactList();
@@ -50,7 +50,7 @@ public override void PropagateUpdate(IExecutionContext context, IList<Tuple> tup
MemoryNode.PropagateAssert(context, toAssert);
}

public override void PropagateRetract(IExecutionContext context, IList<Tuple> tuples)
public override void PropagateRetract(IExecutionContext context, List<Tuple> tuples)
{
var toRetract = new TupleFactList();
foreach (var tuple in tuples)
@@ -38,17 +38,17 @@ public void Accept<TContext>(TContext context, ReteNodeVisitor<TContext> visitor
visitor.VisitDummyNode(context, this);
}

public void PropagateAssert(IExecutionContext context, ITupleFactList tupleFactList)
public void PropagateAssert(IExecutionContext context, TupleFactList tupleFactList)
{
//Do nothing
}

public void PropagateUpdate(IExecutionContext context, ITupleFactList tupleFactList)
public void PropagateUpdate(IExecutionContext context, TupleFactList tupleFactList)
{
//Do nothing
}

public void PropagateRetract(IExecutionContext context, ITupleFactList tupleFactList)
public void PropagateRetract(IExecutionContext context, TupleFactList tupleFactList)
{
//Do nothing
}
@@ -8,7 +8,7 @@ public ExistsNode(ITupleSource leftSource, IObjectSource rightSource) : base(lef
{
}

public override void PropagateAssert(IExecutionContext context, IList<Tuple> tuples)
public override void PropagateAssert(IExecutionContext context, List<Tuple> tuples)
{
var joinedSets = JoinedSets(context, tuples);
var toAssert = new TupleFactList();
@@ -27,7 +27,7 @@ public override void PropagateAssert(IExecutionContext context, IList<Tuple> tup
MemoryNode.PropagateAssert(context, toAssert);
}

public override void PropagateUpdate(IExecutionContext context, IList<Tuple> tuples)
public override void PropagateUpdate(IExecutionContext context, List<Tuple> tuples)
{
var toUpdate = new TupleFactList();
foreach (var tuple in tuples)
@@ -40,7 +40,7 @@ public override void PropagateUpdate(IExecutionContext context, IList<Tuple> tup
MemoryNode.PropagateUpdate(context, toUpdate);
}

public override void PropagateRetract(IExecutionContext context, IList<Tuple> tuples)
public override void PropagateRetract(IExecutionContext context, List<Tuple> tuples)
{
var toRetract = new TupleFactList();
foreach (var tuple in tuples)
@@ -53,7 +53,7 @@ public override void PropagateRetract(IExecutionContext context, IList<Tuple> tu
MemoryNode.PropagateRetract(context, toRetract);
}

public override void PropagateAssert(IExecutionContext context, IList<Fact> facts)
public override void PropagateAssert(IExecutionContext context, List<Fact> facts)
{
var joinedSets = JoinedSets(context, facts);
var toAssert = new TupleFactList();
@@ -73,12 +73,12 @@ public override void PropagateAssert(IExecutionContext context, IList<Fact> fact
MemoryNode.PropagateAssert(context, toAssert);
}

public override void PropagateUpdate(IExecutionContext context, IList<Fact> facts)
public override void PropagateUpdate(IExecutionContext context, List<Fact> facts)
{
//Do nothing
}

public override void PropagateRetract(IExecutionContext context, IList<Fact> facts)
public override void PropagateRetract(IExecutionContext context, List<Fact> facts)
{
var joinedSets = JoinedSets(context, facts);
var toRetract = new TupleFactList();
@@ -4,8 +4,8 @@ namespace NRules.Rete
{
internal interface IObjectSink : INode
{
void PropagateAssert(IExecutionContext context, IList<Fact> facts);
void PropagateUpdate(IExecutionContext context, IList<Fact> facts);
void PropagateRetract(IExecutionContext context, IList<Fact> facts);
void PropagateAssert(IExecutionContext context, List<Fact> facts);
void PropagateUpdate(IExecutionContext context, List<Fact> facts);
void PropagateRetract(IExecutionContext context, List<Fact> facts);
}
}
@@ -4,8 +4,8 @@ namespace NRules.Rete
{
internal interface ITupleSink : INode
{
void PropagateAssert(IExecutionContext context, IList<Tuple> tuples);
void PropagateUpdate(IExecutionContext context, IList<Tuple> tuples);
void PropagateRetract(IExecutionContext context, IList<Tuple> tuples);
void PropagateAssert(IExecutionContext context, List<Tuple> tuples);
void PropagateUpdate(IExecutionContext context, List<Tuple> tuples);
void PropagateRetract(IExecutionContext context, List<Tuple> tuples);
}
}
Oops, something went wrong.

0 comments on commit b7a8727

Please sign in to comment.
You can’t perform that action at this time.