Skip to content

Commit

Permalink
Modify (Large/Source)Text.ParseLineStart to specify an initial capaci…
Browse files Browse the repository at this point in the history
…ty for line count (#73701)

* Modify (Large/Source)Text.ParseLineStart to specify an initial capacity for line count

Noticed this in a Find All References profile, the LargeText.ParseLineStarts was showing as 6.2% of allocations and SourceText.ParseLineStarts was 0.4%.

With this change, these reduced to 4.7% and 0.2% respectively.
  • Loading branch information
ToddGrun authored May 30, 2024
1 parent 45ab8b5 commit b714c00
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/Compilers/Core/Portable/Text/LargeText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,9 @@ private SegmentedList<int> ParseLineStarts()
var position = 0;
var index = 0;
var lastCr = -1;
var list = new SegmentedList<int>();
// Initial line capacity estimated at 64 chars / line. This value was obtained by
// looking at ratios in large files in the roslyn repo.
var list = new SegmentedList<int>(Length / 64);

// The following loop goes through every character in the text. It is highly
// performance critical, and thus inlines knowledge about common line breaks
Expand Down
4 changes: 3 additions & 1 deletion src/Compilers/Core/Portable/Text/SourceText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,9 @@ private SegmentedList<int> ParseLineStarts()
return [0];
}

var lineStarts = new SegmentedList<int>()
// Initial line capacity estimated at 64 chars / line. This value was obtained by
// looking at ratios in large files in the roslyn repo.
var lineStarts = new SegmentedList<int>(Length / 64)
{
0 // there is always the first line
};
Expand Down

0 comments on commit b714c00

Please sign in to comment.