Skip to content

Commit

Permalink
Use GetContainingLineNumber() to reduce allocations
Browse files Browse the repository at this point in the history
Calling `GetContainingLine().LineNumber` causes `TextSnapshotLine` allocations. The `GetContainingLineNumber()` method avoids these.
  • Loading branch information
drewnoakes committed Jun 14, 2023
1 parent 521dc4d commit 30681a6
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,8 @@ public static bool TryGetSurfaceBufferSpan(
var visibleEnd = visibleSpansInBuffer.Last().End;

var snapshot = subjectBuffer.CurrentSnapshot;
var startLine = visibleStart.GetContainingLine().LineNumber;
var endLine = visibleEnd.GetContainingLine().LineNumber;
var startLine = visibleStart.GetContainingLineNumber();
var endLine = visibleEnd.GetContainingLineNumber();

startLine = Math.Max(startLine - extraLines, 0);
endLine = Math.Min(endLine + extraLines, snapshot.LineCount - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static void GetLineAndCharacter(this SnapshotPoint point, out int lineNum
=> point.Snapshot.GetLineAndCharacter(point.Position, out lineNumber, out characterIndex);

public static int GetContainingLineNumber(this SnapshotPoint point)
=> point.GetContainingLine().LineNumber;
=> point.GetContainingLineNumber();

public static ITrackingPoint CreateTrackingPoint(this SnapshotPoint point, PointTrackingMode trackingMode)
=> point.Snapshot.CreateTrackingPoint(point, trackingMode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ End Class
Sub() AssertEx.Fail("Tab should not have been passed to the editor."),
Utilities.TestCommandExecutionContext.Create())

Assert.Equal(3, view.Caret.Position.BufferPosition.GetContainingLine().LineNumber)
Assert.Equal(3, view.Caret.Position.BufferPosition.GetContainingLineNumber())

session.Cancel()
End Using
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.LineCommit

Public Shared Function IsMovementBetweenStatements(oldPoint As SnapshotPoint, newPoint As SnapshotPoint, cancellationToken As CancellationToken) As Boolean
' If they are the same line, then definitely no
If oldPoint.GetContainingLine().LineNumber = newPoint.GetContainingLine().LineNumber Then
If oldPoint.GetContainingLineNumber() = newPoint.GetContainingLineNumber() Then
Return False
End If

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ End Class
End Sub,
Sub(expected, actual, view)
AssertEx.AssertEqualToleratingWhitespaceDifferences(expected, actual)
Assert.Equal(4, view.Caret.Position.BufferPosition.GetContainingLine().LineNumber)
Assert.Equal(4, view.Caret.Position.BufferPosition.GetContainingLineNumber())
Assert.Equal(4, view.Caret.Position.VirtualSpaces)
End Sub)
End Sub
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ rem Hello World$$|]
testData.CommandHandler.ExecuteCommand(New ReturnKeyCommandArgs(testData.View, testData.Buffer), Sub() testData.EditorOperations.InsertNewLine(), TestCommandExecutionContext.Create())
testData.UndoHistory.Undo(count:=1)

Assert.Equal(0, testData.View.Caret.Position.BufferPosition.GetContainingLine().LineNumber)
Assert.Equal(0, testData.View.Caret.Position.BufferPosition.GetContainingLineNumber())
End Using
End Sub

Expand Down Expand Up @@ -342,14 +342,14 @@ End Module

Private Shared Sub AssertCommitsStatement(test As XElement, expectCommit As Boolean, Optional usedSemantics As Boolean = True)
Using testData = CommitTestData.Create(test)
Dim lineNumber = testData.View.Caret.Position.BufferPosition.GetContainingLine().LineNumber
Dim lineNumber = testData.View.Caret.Position.BufferPosition.GetContainingLineNumber()
testData.CommandHandler.ExecuteCommand(New ReturnKeyCommandArgs(testData.View, testData.Buffer), Sub() testData.EditorOperations.InsertNewLine(), TestCommandExecutionContext.Create())
testData.AssertHadCommit(expectCommit)
If expectCommit Then
testData.AssertUsedSemantics(usedSemantics)
End If

Assert.Equal(lineNumber + 1, testData.View.Caret.Position.BufferPosition.GetContainingLine().LineNumber)
Assert.Equal(lineNumber + 1, testData.View.Caret.Position.BufferPosition.GetContainingLineNumber())
End Using
End Sub
End Class
Expand Down

0 comments on commit 30681a6

Please sign in to comment.