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

Reduce some allocations from remote classification serialization #75911

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,14 @@ internal readonly struct SerializableClassifiedSpansAndHighlightSpan(
public readonly TextSpan HighlightSpan = highlightSpan;

public static SerializableClassifiedSpansAndHighlightSpan Dehydrate(ClassifiedSpansAndHighlightSpan classifiedSpansAndHighlightSpan)
=> new(SerializableClassifiedSpans.Dehydrate(classifiedSpansAndHighlightSpan.ClassifiedSpans), classifiedSpansAndHighlightSpan.HighlightSpan);
{
using var _ = Classifier.GetPooledList(out var temp);

foreach (var span in classifiedSpansAndHighlightSpan.ClassifiedSpans)
temp.Add(span);

return new(SerializableClassifiedSpans.Dehydrate(temp), classifiedSpansAndHighlightSpan.HighlightSpan);
}

public ClassifiedSpansAndHighlightSpan Rehydrate()
=> new(this.ClassifiedSpans.Rehydrate(), this.HighlightSpan);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ internal sealed class SerializableClassifiedSpans(ImmutableArray<string> classif
[DataMember(Order = 1)]
public readonly ImmutableArray<int> ClassificationTriples = classificationTriples;

internal static SerializableClassifiedSpans Dehydrate(ImmutableArray<ClassifiedSpan> classifiedSpans)
internal static SerializableClassifiedSpans Dehydrate(SegmentedList<ClassifiedSpan> classifiedSpans)
{
using var _1 = PooledDictionary<string, int>.GetInstance(out var classificationTypeToId);
using var _2 = ArrayBuilder<string>.GetInstance(out var classificationTypes);
var classificationTriples = new FixedSizeArrayBuilder<int>(classifiedSpans.Length * 3);
var classificationTriples = new FixedSizeArrayBuilder<int>(classifiedSpans.Count * 3);

foreach (var classifiedSpan in classifiedSpans)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,18 @@ private static string GetPersistenceName(ClassificationType type)
var classifiedSpans = await TryGetOrReadCachedSemanticClassificationsAsync(
documentKey, type, checksum, cancellationToken).ConfigureAwait(false);
var textSpanIntervalTree = new TextSpanMutableIntervalTree(textSpans);
return classifiedSpans.IsDefault
? null
: SerializableClassifiedSpans.Dehydrate(classifiedSpans.WhereAsArray(c => textSpanIntervalTree.HasIntervalThatIntersectsWith(c.TextSpan)));

if (classifiedSpans.IsDefault)
return null;

using var _ = Classifier.GetPooledList(out var temp);
foreach (var span in classifiedSpans)
{
if (textSpanIntervalTree.HasIntervalThatIntersectsWith(span.TextSpan))
temp.Add(span);
}

return SerializableClassifiedSpans.Dehydrate(temp);
}

private static async ValueTask CacheClassificationsAsync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ await classificationService.AddClassificationsAsync(
_workQueue.AddWork((document, type, options));
}

return SerializableClassifiedSpans.Dehydrate([.. temp]);
return SerializableClassifiedSpans.Dehydrate(temp);
}, cancellationToken);
}
}
Loading