提交 df3e25fb 编写于 作者: H heejaechang

reduce allocation on tree serialization

     recent perf regression shows that we are allocating too much memory on one particular spot in the tree serialization.

     this change is to remove that bottleneck. basically, rather than creating temporary array just to pass it in to the serializer, it will iterate through elements itself and serialize each element. (changeset 1408094)
上级 1d0b2f2f
......@@ -39,14 +39,29 @@ private void InitializeChildren()
internal WithManyChildrenBase(ObjectReader reader)
: base(reader)
{
this.children = ArrayElement<CSharpSyntaxNode>.MakeElementArray((CSharpSyntaxNode[])reader.ReadValue());
var length = reader.ReadInt32();
this.children = new ArrayElement<CSharpSyntaxNode>[length];
for (var i = 0; i < length; i++)
{
this.children[i].Value = (CSharpSyntaxNode)reader.ReadValue();
}
this.InitializeChildren();
}
internal override void WriteTo(ObjectWriter writer)
{
base.WriteTo(writer);
writer.WriteValue(ArrayElement<CSharpSyntaxNode>.MakeArray(this.children));
// PERF: Write the array out manually.Profiling shows that this is cheaper than converting to
// an array in order to use writer.WriteValue.
writer.WriteInt32(this.children.Length);
for (var i = 0; i < this.children.Length; i++)
{
writer.WriteValue(this.children[i].Value);
}
}
protected override int GetSlotCount()
......
......@@ -331,13 +331,27 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Syntax.InternalSyntax
Protected Sub New(reader As ObjectReader)
MyBase.New(reader)
Me._children = ArrayElement(Of VisualBasicSyntaxNode).MakeElementArray(DirectCast(reader.ReadValue(), VisualBasicSyntaxNode()))
Dim length = reader.ReadInt32()
Me._children = New ArrayElement(Of VisualBasicSyntaxNode)(length - 1) {}
For i = 0 To length - 1
Me._children(i).Value = DirectCast(reader.ReadValue(), VisualBasicSyntaxNode)
Next
InitChildren()
End Sub
Friend Overrides Sub WriteTo(writer As ObjectWriter)
MyBase.WriteTo(writer)
writer.WriteValue(ArrayElement(Of VisualBasicSyntaxNode).MakeArray(Me._children))
' PERF Write the array out manually.Profiling shows that this Is cheaper than converting to
' an array in order to use writer.WriteValue.
writer.WriteInt32(Me._children.Length)
For i = 0 To Me._children.Length - 1
writer.WriteValue(Me._children(i).Value)
Next
End Sub
Friend Overrides Sub CopyTo(nodes As ArrayElement(Of VisualBasicSyntaxNode)(), offset As Integer)
......
......@@ -998,7 +998,14 @@ public void TestRecoverableSyntaxTreeCSharp()
var pid = ProjectId.CreateNewId();
var did = DocumentId.CreateNewId(pid);
var text = "public class C {}";
var text = @"public class C {
public void Method1() {}
public void Method2() {}
public void Method3() {}
public void Method4() {}
public void Method5() {}
public void Method6() {}
}";
var sol = CreateNotKeptAliveSolution()
.AddProject(pid, "foo", "foo.dll", LanguageNames.CSharp)
......@@ -1014,7 +1021,20 @@ public void TestRecoverableSyntaxTreeVisualBasic()
var pid = ProjectId.CreateNewId();
var did = DocumentId.CreateNewId(pid);
var text = @"Public Class C\r\nEnd Class";
var text = @"Public Class C
Sub Method1()
End Sub
Sub Method2()
End Sub
Sub Method3()
End Sub
Sub Method4()
End Sub
Sub Method5()
End Sub
Sub Method6()
End Sub
End Class";
var sol = CreateNotKeptAliveSolution()
.AddProject(pid, "foo", "foo.dll", LanguageNames.VisualBasic)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册