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

Remove double array allocation in SemanticTokens #74271

Merged
Merged
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ namespace Microsoft.CodeAnalysis.LanguageServer.Handler.SemanticTokens
{
internal static class SemanticTokensHelpers
{
private static readonly ObjectPool<List<int>> s_tokenListPool = new ObjectPool<List<int>>(() => new List<int>(capacity: 1000));

internal static async Task<int[]> HandleRequestHelperAsync(
IGlobalOptionService globalOptions,
SemanticTokensRefreshQueue semanticTokensRefreshQueue,
Expand Down Expand Up @@ -243,23 +245,31 @@ private static int[] ComputeTokens(

var tokenTypeMap = SemanticTokensSchema.GetSchema(supportsVisualStudioExtensions).TokenTypeMap;

using var _ = ArrayBuilder<int>.GetInstance(5 * classifiedSpans.Count, out var data);
for (var currentClassifiedSpanIndex = 0; currentClassifiedSpanIndex < classifiedSpans.Count; currentClassifiedSpanIndex++)
var data = s_tokenListPool.Allocate();
Copy link
Member

Choose a reason for hiding this comment

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

if you use .GetPooledObject you can use a using.

try
{
currentClassifiedSpanIndex = ComputeNextToken(
lines, ref lastLineNumber, ref lastStartCharacter, classifiedSpans,
currentClassifiedSpanIndex, tokenTypeMap, tokenTypesToIndex,
out var deltaLine, out var startCharacterDelta, out var tokenLength,
out var tokenType, out var tokenModifiers);

data.Add(deltaLine);
data.Add(startCharacterDelta);
data.Add(tokenLength);
data.Add(tokenType);
data.Add(tokenModifiers);
}
for (var currentClassifiedSpanIndex = 0; currentClassifiedSpanIndex < classifiedSpans.Count; currentClassifiedSpanIndex++)
{
currentClassifiedSpanIndex = ComputeNextToken(
lines, ref lastLineNumber, ref lastStartCharacter, classifiedSpans,
currentClassifiedSpanIndex, tokenTypeMap, tokenTypesToIndex,
out var deltaLine, out var startCharacterDelta, out var tokenLength,
out var tokenType, out var tokenModifiers);

data.Add(deltaLine);
data.Add(startCharacterDelta);
data.Add(tokenLength);
data.Add(tokenType);
data.Add(tokenModifiers);
}

return data.ToArray();
return data.ToArray();
}
finally
{
data.Clear();
Copy link
Member

Choose a reason for hiding this comment

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

i am fine not clearing an list of ints.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, went ahead and switched to GetPooledObject. Clearing will now occur upon retrieval from the pool.

s_tokenListPool.Free(data);
}
}

private static int ComputeNextToken(
Expand Down
Loading