Skip to content

Commit

Permalink
test - vm graph perf (#4)
Browse files Browse the repository at this point in the history
* cleanup dependency graph from old nodes

* public function as internal

* preserve public API

* bugfix tests

* hashset optimization

* remove nodes only on deletion

* remove inactive nodes

---------

Co-authored-by: Luis Rivera <[email protected]>
  • Loading branch information
BogdanZavu and Luis Rivera authored Mar 6, 2025
1 parent 1f72822 commit 22e41d9
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 12 deletions.
15 changes: 12 additions & 3 deletions src/Engine/ProtoAssociative/CodeGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2156,7 +2156,8 @@ public override int Emit(ProtoCore.AST.Node codeBlockNode, ProtoCore.Associative
hasReturnStatement = EmitCodeBlock(codeblock.Body, ref inferedType, ProtoCore.CompilerDefinitions.SubCompilePass.UnboundIdentifier, false);
if (compilePass == ProtoCore.CompilerDefinitions.CompilePass.GlobalScope && !hasReturnStatement)
{
EmitReturnNull();
var guidList = codeblock.Body.Select(x=> (x as BinaryExpressionNode)?.guid).Where(x=>x!=null).ToList();
EmitReturnNull(guidList.Any() ? guidList.Last() : null);
}

compilePass++;
Expand Down Expand Up @@ -5909,7 +5910,7 @@ private void EmitGroupExpressionNode(AssociativeNode node, ref ProtoCore.Type in
}
}

protected override void EmitReturnNull()
protected override void EmitReturnNull(Guid? guid)
{
int startpc = pc;

Expand All @@ -5928,9 +5929,17 @@ protected override void EmitReturnNull()
retNode.updateBlock.startpc = startpc;
retNode.updateBlock.endpc = pc - 1;
retNode.isReturn = true;
if (guid != null)
{
retNode.guid = (Guid)guid;
}

PushGraphNode(retNode);
}
protected override void EmitReturnNull()
{
EmitReturnNull(null);
}

private ProtoCore.Type BuildArgumentTypeFromVarDeclNode(VarDeclNode argNode, GraphNode graphNode = null)
{
Expand Down Expand Up @@ -6130,4 +6139,4 @@ protected override void DfsTraverse(ProtoCore.AST.Node pNode, ref ProtoCore.Type
int blockId = codeBlock.codeBlockId;
}
}
}
}
19 changes: 11 additions & 8 deletions src/Engine/ProtoCore/AssociativeGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ public static void BuildGraphNodeDependencies(List<AssociativeGraph.GraphNode> g
{
return;
}

for (int i = 0; i < graphNodesInScope.Count; ++i)
{
AssociativeGraph.GraphNode currentNode = graphNodesInScope[i];
currentNode.ParentNodes.Clear();
currentNode.ChildrenNodes.Clear();
}
// Get the current graphnode to check against the list
// [a = 10] -> this one
// c = 1
Expand Down Expand Up @@ -967,33 +972,31 @@ internal IEnumerable<Guid> ClearCycles(IEnumerable<GraphNode> graphNodes)
Stack<GraphNode> stack = new Stack<GraphNode>();
stack.Push(this);

var visited = graphNodes.ToDictionary(node => node, node => false);
var visited = new HashSet<int>();

var guids = new List<Guid>();
var guids = new HashSet<Guid>();
while(stack.Any())
{
var node = stack.Pop();
if (!visited[node])
if (visited.Add(node.UID))
{
guids.Add(node.guid);
if (node.isCyclic)
{
node.isCyclic = false;
node.isActive = true;

}
visited[node] = true;
}

foreach(var cNode in node.ChildrenNodes)
{
if (!visited[cNode])
if (!visited.Contains(cNode.UID))
{
stack.Push(cNode);
}
}
}
return guids;
return guids.ToList();
}

public void PushDependent(GraphNode dependent)
Expand Down
1 change: 1 addition & 0 deletions src/Engine/ProtoCore/CodeGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2363,6 +2363,7 @@ protected bool InsideFunction()

// used to manully emit "return = null" instruction if a function or language block does not have a return statement
// there is update code involved in associativen code gen, so it is not implemented here
protected abstract void EmitReturnNull(Guid? guid);
protected abstract void EmitReturnNull();

protected abstract void DfsTraverse(Node node, ref ProtoCore.Type inferedType, bool isBooleanOp = false, ProtoCore.AssociativeGraph.GraphNode graphNode = null,
Expand Down
7 changes: 6 additions & 1 deletion src/Engine/ProtoImperative/CodeGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2543,12 +2543,17 @@ private void EmitSetExpressionUID(int exprId)
//AppendInstruction(instr);
}

protected override void EmitReturnNull()
protected override void EmitReturnNull(Guid? guid = null)
{
EmitPushNull();
EmitReturnToRegister();
}

protected override void EmitReturnNull()
{
EmitReturnNull(null);
}

protected void EmitGropuExpressionNode(ImperativeNode node, ref ProtoCore.Type inferedType)
{
GroupExpressionNode group = node as GroupExpressionNode;
Expand Down
15 changes: 15 additions & 0 deletions src/Engine/ProtoScript/Runners/LiveRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1709,6 +1709,13 @@ private void SynchronizeInternal(GraphSyncData syncData)
// Get AST list that need to be executed
var finalDeltaAstList = changeSetComputer.GetDeltaASTList(syncData);

//nodes which will be defined or redefined after compilation and execution
var pendingUIDsForDeletion = changeSetComputer.csData.DeletedBinaryExprASTNodes
.Select(x => (x as BinaryExpressionNode)?.guid)
.Where(x => x != null)
.Select(x => x.Value)
.ToHashSet();

// Prior to execution, apply state modifications to the VM given the delta AST's
bool anyForcedExecutedNodes = changeSetComputer.csData.ForceExecuteASTList.Any();
changeSetApplier.Apply(runnerCore, runtimeCore, changeSetComputer.csData);
Expand All @@ -1721,6 +1728,14 @@ private void SynchronizeInternal(GraphSyncData syncData)
var guids = runtimeCore.ExecutedAstGuids.ToList();
executedAstGuids[syncData.SessionID] = guids;
runtimeCore.RemoveExecutedAstGuids();

// There should be a CodeBlock in CodeBlockList by now
if (runnerCore.CodeBlockList.Any())
{
var nodes = runnerCore.CodeBlockList[(int)Language.Associative].instrStream.dependencyGraph.GetGraphNodesAtScope(Constants.kInvalidPC, Constants.kInvalidPC);
//delete all nodes which were redefined. For those nodes that were defined for the first time, this will be a no-op
nodes.RemoveAll(x=>!x.isActive || pendingUIDsForDeletion.Contains(x.guid));
}
}
}

Expand Down

0 comments on commit 22e41d9

Please sign in to comment.