Skip to content

Commit

Permalink
Perf: save text buffer size to prevent frequent vector access
Browse files Browse the repository at this point in the history
  • Loading branch information
skyline75489 committed Oct 28, 2019
1 parent 634687b commit dd10a15
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/buffer/out/textBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ TextBuffer::TextBuffer(const COORD screenBufferSize,
_cursor{ cursorSize, *this },
_storage{},
_unicodeStorage{},
_size{ Viewport::Empty() },
_renderTarget{ renderTarget }
{
// initialize ROWs
for (size_t i = 0; i < static_cast<size_t>(screenBufferSize.Y); ++i)
{
_storage.emplace_back(static_cast<SHORT>(i), screenBufferSize.X, _currentAttributes, this);
}

_UpdateSize();
}

// Routine Description:
Expand Down Expand Up @@ -651,9 +654,10 @@ const SHORT TextBuffer::GetFirstRowIndex() const noexcept
{
return _firstRow;
}

const Viewport TextBuffer::GetSize() const
{
return Viewport::FromDimensions({ 0, 0 }, { gsl::narrow<SHORT>(_storage.at(0).size()), gsl::narrow<SHORT>(_storage.size()) });
return _size;
}

void TextBuffer::_SetFirstRowIndex(const SHORT FirstRowIndex) noexcept
Expand Down Expand Up @@ -844,6 +848,9 @@ void TextBuffer::Reset()
// Also take advantage of the row ID refresh loop to resize the rows in the X dimension
// and cleanup the UnicodeStorage characters that might fall outside the resized buffer.
_RefreshRowIDs(newSize.X);

// Update the buffer size.
_UpdateSize();
}
CATCH_RETURN();

Expand Down Expand Up @@ -896,6 +903,11 @@ void TextBuffer::_RefreshRowIDs(std::optional<SHORT> newRowWidth)
_unicodeStorage.Remap(rowMap, newRowWidth);
}

void TextBuffer::_UpdateSize()
{
_size = Viewport::FromDimensions({ 0, 0 }, { gsl::narrow<SHORT>(_storage.at(0).size()), gsl::narrow<SHORT>(_storage.size()) });
}

void TextBuffer::_NotifyPaint(const Viewport& viewport) const
{
_renderTarget.TriggerRedraw(viewport);
Expand Down
4 changes: 4 additions & 0 deletions src/buffer/out/textBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,17 @@ class TextBuffer final

SHORT _firstRow; // indexes top row (not necessarily 0)

Microsoft::Console::Types::Viewport _size;

TextAttribute _currentAttributes;

// storage location for glyphs that can't fit into the buffer normally
UnicodeStorage _unicodeStorage;

void _RefreshRowIDs(std::optional<SHORT> newRowWidth);

void _UpdateSize();

Microsoft::Console::Render::IRenderTarget& _renderTarget;

void _SetFirstRowIndex(const SHORT FirstRowIndex) noexcept;
Expand Down

0 comments on commit dd10a15

Please sign in to comment.