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

Perf: save text buffer size for frequent access #3351

Closed
Closed
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
18 changes: 15 additions & 3 deletions 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 @@ -622,7 +625,7 @@ COORD TextBuffer::GetLastNonSpaceCharacter(const Microsoft::Console::Types::View
// Return Value:
// - Coordinate position in screen coordinates of the character just before the cursor.
// - NOTE: Will return 0,0 if already in the top left corner
COORD TextBuffer::_GetPreviousFromCursor() const
COORD TextBuffer::_GetPreviousFromCursor() const noexcept
{
COORD coordPosition = GetCursor().GetPosition();

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

const Viewport TextBuffer::GetSize() const noexcept
{
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
8 changes: 6 additions & 2 deletions src/buffer/out/textBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class TextBuffer final

const SHORT GetFirstRowIndex() const noexcept;

const Microsoft::Console::Types::Viewport GetSize() const;
const Microsoft::Console::Types::Viewport GetSize() const noexcept;

void ScrollRows(const SHORT firstRow, const SHORT size, const SHORT delta);

Expand Down Expand Up @@ -156,18 +156,22 @@ 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;

COORD _GetPreviousFromCursor() const;
COORD _GetPreviousFromCursor() const noexcept;

void _SetWrapOnCurrentRow();
void _AdjustWrapOnCurrentRow(const bool fSet);
Expand Down
2 changes: 1 addition & 1 deletion src/types/ScreenInfoUiaProviderBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ IFACEMETHODIMP ScreenInfoUiaProviderBase::get_SupportedTextSelection(_Out_ Suppo

#pragma endregion

const COORD ScreenInfoUiaProviderBase::_getScreenBufferCoords() const
const COORD ScreenInfoUiaProviderBase::_getScreenBufferCoords() const noexcept
{
return _getTextBuffer().GetSize().Dimensions();
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/ScreenInfoUiaProviderBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ namespace Microsoft::Console::Types
// mechanism for multi-threaded code.
std::map<EVENTID, bool> _signalFiringMapping;

const COORD _getScreenBufferCoords() const;
const COORD _getScreenBufferCoords() const noexcept;
const TextBuffer& _getTextBuffer() const noexcept;
const Viewport _getViewport() const noexcept;
void _LockConsole() noexcept;
Expand Down
56 changes: 33 additions & 23 deletions src/types/UiaTextRangeBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ IdType UiaTextRangeBase::id = 1;

UiaTextRangeBase::MoveState::MoveState(IUiaData* pData,
const UiaTextRangeBase& range,
const MovementDirection direction) :
const MovementDirection direction) noexcept :
StartScreenInfoRow{ UiaTextRangeBase::_endpointToScreenInfoRow(pData, range.GetStart()) },
StartColumn{ UiaTextRangeBase::_endpointToColumn(pData, range.GetStart()) },
EndScreenInfoRow{ UiaTextRangeBase::_endpointToScreenInfoRow(pData, range.GetEnd()) },
Expand Down Expand Up @@ -298,7 +298,7 @@ IFACEMETHODIMP UiaTextRangeBase::CompareEndpoints(_In_ TextPatternRangeEndpoint
return S_OK;
}

IFACEMETHODIMP UiaTextRangeBase::ExpandToEnclosingUnit(_In_ TextUnit unit)
IFACEMETHODIMP UiaTextRangeBase::ExpandToEnclosingUnit(_In_ TextUnit unit) noexcept
{
_pData->LockConsole();
auto Unlock = wil::scope_exit([&]() noexcept {
Expand Down Expand Up @@ -548,7 +548,7 @@ IFACEMETHODIMP UiaTextRangeBase::GetText(_In_ int maxLength, _Out_ BSTR* pRetVal

IFACEMETHODIMP UiaTextRangeBase::Move(_In_ TextUnit unit,
_In_ int count,
_Out_ int* pRetVal)
_Out_ int* pRetVal) noexcept
{
_pData->LockConsole();
auto Unlock = wil::scope_exit([&]() noexcept {
Expand Down Expand Up @@ -579,7 +579,7 @@ IFACEMETHODIMP UiaTextRangeBase::Move(_In_ TextUnit unit,
_outputRowConversions();
#endif

auto moveFunc = &_moveByDocument;
std::pair<unsigned, unsigned> (*moveFunc)(gsl::not_null<IUiaData*>, int, MoveState, gsl::not_null<int*>) = &_moveByDocument;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I have no idea it would end up like this. If anyone has better way to do this, please enlighten me.

if (unit == TextUnit::TextUnit_Character)
{
moveFunc = &_moveByCharacter;
Expand All @@ -589,6 +589,11 @@ IFACEMETHODIMP UiaTextRangeBase::Move(_In_ TextUnit unit,
moveFunc = &_moveByLine;
}

if (moveFunc == nullptr)
{
return S_OK;
}

const MovementDirection moveDirection = (count > 0) ? MovementDirection::Forward : MovementDirection::Backward;
std::pair<Endpoint, Endpoint> newEndpoints;

Expand Down Expand Up @@ -620,7 +625,7 @@ IFACEMETHODIMP UiaTextRangeBase::Move(_In_ TextUnit unit,
IFACEMETHODIMP UiaTextRangeBase::MoveEndpointByUnit(_In_ TextPatternRangeEndpoint endpoint,
_In_ TextUnit unit,
_In_ int count,
_Out_ int* pRetVal)
_Out_ int* pRetVal) noexcept
{
_pData->LockConsole();
auto Unlock = wil::scope_exit([&]() noexcept {
Expand Down Expand Up @@ -655,7 +660,7 @@ IFACEMETHODIMP UiaTextRangeBase::MoveEndpointByUnit(_In_ TextPatternRangeEndpoin

const MovementDirection moveDirection = (count > 0) ? MovementDirection::Forward : MovementDirection::Backward;

auto moveFunc = &_moveEndpointByUnitDocument;
std::tuple<unsigned, unsigned, bool> (*moveFunc)(gsl::not_null<IUiaData*>, int, TextPatternRangeEndpoint, MoveState, gsl::not_null<int*>) = &_moveEndpointByUnitDocument;
if (unit == TextUnit::TextUnit_Character)
{
moveFunc = &_moveEndpointByUnitCharacter;
Expand All @@ -665,6 +670,11 @@ IFACEMETHODIMP UiaTextRangeBase::MoveEndpointByUnit(_In_ TextPatternRangeEndpoin
moveFunc = &_moveEndpointByUnitLine;
}

if (moveFunc == nullptr)
{
return S_OK;
}

std::tuple<Endpoint, Endpoint, bool> moveResults;
try
{
Expand All @@ -687,7 +697,7 @@ IFACEMETHODIMP UiaTextRangeBase::MoveEndpointByUnit(_In_ TextPatternRangeEndpoin

IFACEMETHODIMP UiaTextRangeBase::MoveEndpointByRange(_In_ TextPatternRangeEndpoint endpoint,
_In_ ITextRangeProvider* pTargetRange,
_In_ TextPatternRangeEndpoint targetEndpoint)
_In_ TextPatternRangeEndpoint targetEndpoint) noexcept
{
_pData->LockConsole();
auto Unlock = wil::scope_exit([&]() noexcept {
Expand Down Expand Up @@ -924,7 +934,7 @@ IFACEMETHODIMP UiaTextRangeBase::GetChildren(_Outptr_result_maybenull_ SAFEARRAY

#pragma endregion

const COORD UiaTextRangeBase::_getScreenBufferCoords(gsl::not_null<IUiaData*> pData)
const COORD UiaTextRangeBase::_getScreenBufferCoords(gsl::not_null<IUiaData*> pData) noexcept
{
return pData->GetTextBuffer().GetSize().Dimensions();
}
Expand Down Expand Up @@ -957,7 +967,7 @@ const unsigned int UiaTextRangeBase::_getTotalRows(gsl::not_null<IUiaData*> pDat
// - <none>
// Return Value:
// - The row width
const unsigned int UiaTextRangeBase::_getRowWidth(gsl::not_null<IUiaData*> pData)
const unsigned int UiaTextRangeBase::_getRowWidth(gsl::not_null<IUiaData*> pData) noexcept
{
// make sure that we can't leak a 0
return std::max(static_cast<unsigned int>(_getScreenBufferCoords(pData).X), 1u);
Expand All @@ -969,7 +979,7 @@ const unsigned int UiaTextRangeBase::_getRowWidth(gsl::not_null<IUiaData*> pData
// - endpoint - the endpoint to translate
// Return Value:
// - the column value
const Column UiaTextRangeBase::_endpointToColumn(gsl::not_null<IUiaData*> pData, const Endpoint endpoint)
const Column UiaTextRangeBase::_endpointToColumn(gsl::not_null<IUiaData*> pData, const Endpoint endpoint) noexcept
{
return endpoint % _getRowWidth(pData);
}
Expand All @@ -981,7 +991,7 @@ const Column UiaTextRangeBase::_endpointToColumn(gsl::not_null<IUiaData*> pData,
// Return Value:
// - the text buffer row value
const TextBufferRow UiaTextRangeBase::_endpointToTextBufferRow(gsl::not_null<IUiaData*> pData,
const Endpoint endpoint)
const Endpoint endpoint) noexcept
{
return endpoint / _getRowWidth(pData);
}
Expand All @@ -993,7 +1003,7 @@ const TextBufferRow UiaTextRangeBase::_endpointToTextBufferRow(gsl::not_null<IUi
// - <none>
// Return Value:
// - The number of rows in the range.
const unsigned int UiaTextRangeBase::_rowCountInRange(gsl::not_null<IUiaData*> pData) const
const unsigned int UiaTextRangeBase::_rowCountInRange(gsl::not_null<IUiaData*> pData) const noexcept
{
if (_degenerate)
{
Expand Down Expand Up @@ -1127,7 +1137,7 @@ const TextBufferRow UiaTextRangeBase::_screenInfoRowToTextBufferRow(gsl::not_nul
// - row - the TextBufferRow to convert
// Return Value:
// - the equivalent Endpoint, starting at the beginning of the TextBufferRow.
const Endpoint UiaTextRangeBase::_textBufferRowToEndpoint(gsl::not_null<IUiaData*> pData, const TextBufferRow row)
const Endpoint UiaTextRangeBase::_textBufferRowToEndpoint(gsl::not_null<IUiaData*> pData, const TextBufferRow row) noexcept
{
return _getRowWidth(pData) * row;
}
Expand All @@ -1139,7 +1149,7 @@ const Endpoint UiaTextRangeBase::_textBufferRowToEndpoint(gsl::not_null<IUiaData
// Return Value:
// - the equivalent Endpoint.
const Endpoint UiaTextRangeBase::_screenInfoRowToEndpoint(gsl::not_null<IUiaData*> pData,
const ScreenInfoRow row)
const ScreenInfoRow row) noexcept
{
return _textBufferRowToEndpoint(pData, _screenInfoRowToTextBufferRow(pData, row));
}
Expand All @@ -1151,7 +1161,7 @@ const Endpoint UiaTextRangeBase::_screenInfoRowToEndpoint(gsl::not_null<IUiaData
// Return Value:
// - the equivalent ScreenInfoRow.
const ScreenInfoRow UiaTextRangeBase::_endpointToScreenInfoRow(gsl::not_null<IUiaData*> pData,
const Endpoint endpoint)
const Endpoint endpoint) noexcept
{
return _textBufferRowToScreenInfoRow(pData, _endpointToTextBufferRow(pData, endpoint));
}
Expand Down Expand Up @@ -1255,7 +1265,7 @@ const Column UiaTextRangeBase::_getFirstColumnIndex() noexcept
// - <none>
// Return Value:
// - the index of the last column (0-indexed) of the screen info rows
const Column UiaTextRangeBase::_getLastColumnIndex(gsl::not_null<IUiaData*> pData)
const Column UiaTextRangeBase::_getLastColumnIndex(gsl::not_null<IUiaData*> pData) noexcept
{
return _getRowWidth(pData) - 1;
}
Expand All @@ -1275,7 +1285,7 @@ const int UiaTextRangeBase::_compareScreenCoords(gsl::not_null<IUiaData*> pData,
const ScreenInfoRow rowA,
const Column colA,
const ScreenInfoRow rowB,
const Column colB)
const Column colB) noexcept
{
FAIL_FAST_IF(!(rowA >= _getFirstScreenInfoRowIndex()));
FAIL_FAST_IF(!(rowA <= _getLastScreenInfoRowIndex(pData)));
Expand Down Expand Up @@ -1441,7 +1451,7 @@ std::pair<Endpoint, Endpoint> UiaTextRangeBase::_moveByCharacterBackward(gsl::no
std::pair<Endpoint, Endpoint> UiaTextRangeBase::_moveByLine(gsl::not_null<IUiaData*> pData,
const int moveCount,
const MoveState moveState,
_Out_ gsl::not_null<int*> const pAmountMoved)
_Out_ gsl::not_null<int*> const pAmountMoved) noexcept
{
*pAmountMoved = 0;
Endpoint start = _screenInfoRowToEndpoint(pData, moveState.StartScreenInfoRow) + moveState.StartColumn;
Expand Down Expand Up @@ -1488,7 +1498,7 @@ std::pair<Endpoint, Endpoint> UiaTextRangeBase::_moveByLine(gsl::not_null<IUiaDa
std::pair<Endpoint, Endpoint> UiaTextRangeBase::_moveByDocument(gsl::not_null<IUiaData*> pData,
const int /*moveCount*/,
const MoveState moveState,
_Out_ gsl::not_null<int*> const pAmountMoved)
_Out_ gsl::not_null<int*> const pAmountMoved) noexcept
{
// We can't move by anything larger than a line, so move by document will apply and will
// just report that it can't do that.
Expand Down Expand Up @@ -1722,7 +1732,7 @@ std::tuple<Endpoint, Endpoint, bool> UiaTextRangeBase::_moveEndpointByUnitLine(g
const int moveCount,
const TextPatternRangeEndpoint endpoint,
const MoveState moveState,
_Out_ gsl::not_null<int*> const pAmountMoved)
_Out_ gsl::not_null<int*> const pAmountMoved) noexcept
{
*pAmountMoved = 0;
int count = moveCount;
Expand Down Expand Up @@ -1881,7 +1891,7 @@ std::tuple<Endpoint, Endpoint, bool> UiaTextRangeBase::_moveEndpointByUnitDocume
const int moveCount,
const TextPatternRangeEndpoint endpoint,
const MoveState moveState,
_Out_ gsl::not_null<int*> const pAmountMoved)
_Out_ gsl::not_null<int*> const pAmountMoved) noexcept
{
*pAmountMoved = 0;

Expand Down Expand Up @@ -1944,13 +1954,13 @@ std::tuple<Endpoint, Endpoint, bool> UiaTextRangeBase::_moveEndpointByUnitDocume
return std::make_tuple(start, end, degenerate);
}

COORD UiaTextRangeBase::_endpointToCoord(gsl::not_null<IUiaData*> pData, const Endpoint endpoint)
COORD UiaTextRangeBase::_endpointToCoord(gsl::not_null<IUiaData*> pData, const Endpoint endpoint) noexcept
{
return { gsl::narrow<short>(_endpointToColumn(pData, endpoint)), gsl::narrow<short>(_endpointToScreenInfoRow(pData, endpoint)) };
}

Endpoint UiaTextRangeBase::_coordToEndpoint(gsl::not_null<IUiaData*> pData,
const COORD coord)
const COORD coord) noexcept
{
return _screenInfoRowToEndpoint(pData, coord.Y) + coord.X;
}
Expand Down
Loading