Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of lsif generation. #51457

Merged
3 commits merged into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/Features/Lsif/Generator/Writing/BatchingLsifJsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ public void Write(Element element)
}
}

public void Write(List<Element> elements)
{
lock (_elementsGate)
{
_elements.AddRange(elements);
}
}

public void FlushToUnderlyingAndEmpty()
{
lock (_writingGate)
Expand All @@ -69,10 +77,7 @@ public void FlushToUnderlyingAndEmpty()
_elements = new List<Element>();
}

foreach (var element in localElements)
{
_underlyingWriter.Write(element);
}
_underlyingWriter.Write(localElements);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Features/Lsif/Generator/Writing/ILsifJsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using Microsoft.CodeAnalysis.LanguageServerIndexFormat.Generator.Graph;

namespace Microsoft.CodeAnalysis.LanguageServerIndexFormat.Generator.Writing
{
internal interface ILsifJsonWriter
{
void Write(Element element);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❔ Do we still need this one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup. several callers call directly into that one. I could have them wrap into a single-element list, but it seemed wasteful :)

void Write(List<Element> elements);
CyrusNajmabadi marked this conversation as resolved.
Show resolved Hide resolved
}
}
10 changes: 10 additions & 0 deletions src/Features/Lsif/Generator/Writing/JsonModeLsifJsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.CodeAnalysis.LanguageServerIndexFormat.Generator.Graph;
using Newtonsoft.Json;
Expand Down Expand Up @@ -44,6 +45,15 @@ public void Write(Element element)
}
}

public void Write(List<Element> elements)
{
lock (_writeGate)
{
foreach (var element in elements)
_jsonSerializer.Serialize(_jsonTextWriter, element);
}
}

public void Dispose()
{
_jsonTextWriter.WriteWhitespace(Environment.NewLine);
Expand Down
14 changes: 14 additions & 0 deletions src/Features/Lsif/Generator/Writing/LineModeLsifJsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.IO;
using Microsoft.CodeAnalysis.LanguageServerIndexFormat.Generator.Graph;
using Newtonsoft.Json;
Expand Down Expand Up @@ -40,5 +41,18 @@ public void Write(Element element)
_outputWriter.WriteLine(line);
}
}

public void Write(List<Element> elements)
{
var lines = new List<string>();
foreach (var element in elements)
lines.Add(JsonConvert.SerializeObject(element, _settings));

lock (_writeGate)
{
foreach (var line in lines)
_outputWriter.WriteLine(line);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ Namespace Microsoft.CodeAnalysis.LanguageServerIndexFormat.Generator.UnitTests.U
Private ReadOnly _elementsById As Dictionary(Of Id(Of Element), Element) = New Dictionary(Of Id(Of Element), Element)
Private ReadOnly _edgesByOutVertex As Dictionary(Of Vertex, List(Of Edge)) = New Dictionary(Of Vertex, List(Of Edge))

Private Sub ILsifJsonWriter_Write(elements As List(Of Element)) Implements ILsifJsonWriter.Write
For Each element In elements
ILsifJsonWriter_Write(element)
Next
End Sub

CyrusNajmabadi marked this conversation as resolved.
Show resolved Hide resolved
Private Sub ILsifJsonWriter_Write(element As Element) Implements ILsifJsonWriter.Write
SyncLock _gate
' We intentionally use Add so it'll throw if we have a duplicate ID.
Expand Down