From 20500babda37a2d8ad65688183fb0b06de5f87df Mon Sep 17 00:00:00 2001 From: "Dustin L. Howett" Date: Wed, 21 Jun 2023 15:52:01 -0500 Subject: [PATCH] Stop committing the whole buffer when determining if it's empty (#15582) As a shortcut, GetLastNonSpaceCharacter can start with the last committed row. It's guaranteed that there isn't anything of worth below that point, so why bother checking? Without this, Terminal immediately commits the entire 9031-line buffer on startup while trying to--get this!--clear the screen! --------- Co-authored-by: Leonard Hecker --- src/buffer/out/textBuffer.cpp | 17 +++++++++++++++-- src/buffer/out/textBuffer.hpp | 1 + 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/buffer/out/textBuffer.cpp b/src/buffer/out/textBuffer.cpp index d4d62facb8c..7f6eaafdc91 100644 --- a/src/buffer/out/textBuffer.cpp +++ b/src/buffer/out/textBuffer.cpp @@ -166,6 +166,19 @@ ROW& TextBuffer::_getRowByOffsetDirect(size_t offset) return *reinterpret_cast(row); } +// Returns the "user-visible" index of the last committed row, which can be used +// to short-circuit some algorithms that try to scan the entire buffer. +// Returns 0 if no rows are committed in. +til::CoordType TextBuffer::_estimateOffsetOfLastCommittedRow() const noexcept +{ + const auto lastRowOffset = (_commitWatermark - _buffer.get()) / _bufferRowStride; + // This subtracts 2 from the offset to account for the: + // * scratchpad row at offset 0, whereas regular rows start at offset 1. + // * fact that _commitWatermark points _past_ the last committed row, + // but we want to return an index pointing at the last row. + return std::max(0, gsl::narrow_cast(lastRowOffset - 2)); +} + // Retrieves a row from the buffer by its offset from the first row of the text buffer // (what corresponds to the top row of the screen buffer). const ROW& TextBuffer::GetRowByOffset(const til::CoordType index) const @@ -807,7 +820,7 @@ til::point TextBuffer::GetLastNonSpaceCharacter(std::optional