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 SourceText.LineInfo indexer. #74267

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
4 changes: 2 additions & 2 deletions src/Compilers/Core/Portable/Text/SourceText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -966,12 +966,12 @@ public override TextLine this[int index]
int start = _lineStarts[index];
if (index == _lineStarts.Count - 1)
{
return TextLine.FromSpan(_text, TextSpan.FromBounds(start, _text.Length));
return TextLine.FromSpanUnsafe(_text, TextSpan.FromBounds(start, _text.Length));
}
else
{
int end = _lineStarts[index + 1];
return TextLine.FromSpan(_text, TextSpan.FromBounds(start, end));
return TextLine.FromSpanUnsafe(_text, TextSpan.FromBounds(start, end));
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/Compilers/Core/Portable/Text/TextLine.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.Diagnostics;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Text
Expand Down Expand Up @@ -81,6 +82,17 @@ public static TextLine FromSpan(SourceText text, TextSpan span)
}
}

// Do not use unless you are certain the span you are passing in is valid!
// This was added to allow SourceText.LineInfo's indexer to directly create TextLines
// without the performance implications of calling FromSpan.
internal static TextLine FromSpanUnsafe(SourceText text, TextSpan span)
{
Debug.Assert(span.Start == 0 || TextUtilities.IsAnyLineBreakCharacter(text[span.Start - 1]));
Debug.Assert(span.End == text.Length || TextUtilities.IsAnyLineBreakCharacter(text[span.End - 1]));

return new TextLine(text, span.Start, span.End);
}

/// <summary>
/// Gets the source text.
/// </summary>
Expand Down