Skip to content

Commit

Permalink
Cache the viewport to make invalidation faster (#6918)
Browse files Browse the repository at this point in the history
In `Renderer::TriggerRedraw`, the act of fetching the viewport from the
`pData` over and over is wasted time. We already have a cached variable
of the viewport that is updated on every scroll check (on
`TriggerScroll` and on `PaintFrame`.) Scrolling wouldn't be working
correctly if the clients weren't already notifying us that the viewport
has changed for scroll purposes, so we can just keep using that cached
value for the invalidation restriction to speed things up over fetching
it again.

## Validation Steps Performed
- Run `time cat big.txt`. Checked average time before/after, WPR traces
  before/after.

## PR Checklist
* [x] Closes perf itch
* [x] I work here
* [x] Manual test
* [x] Documentation irrelevant.
* [x] Schema irrelevant.
* [x] Am core contributor.
  • Loading branch information
miniksa committed Jul 16, 2020
1 parent 3255177 commit 81b7e54
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 10 deletions.
145 changes: 143 additions & 2 deletions src/host/ut_host/VtIoTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,16 +254,156 @@ void VtIoTests::DtorTestStackAllocMany()
}
}

class MockRenderData : public IRenderData, IUiaData
{
public:
Microsoft::Console::Types::Viewport GetViewport() noexcept override
{
return Microsoft::Console::Types::Viewport{};
}

COORD GetTextBufferEndPosition() const noexcept override
{
return COORD{};
}

const TextBuffer& GetTextBuffer() noexcept override
{
FAIL_FAST_HR(E_NOTIMPL);
}

const FontInfo& GetFontInfo() noexcept override
{
FAIL_FAST_HR(E_NOTIMPL);
}

std::vector<Microsoft::Console::Types::Viewport> GetSelectionRects() noexcept override
{
return std::vector<Microsoft::Console::Types::Viewport>{};
}

void LockConsole() noexcept override
{
}

void UnlockConsole() noexcept override
{
}

const TextAttribute GetDefaultBrushColors() noexcept override
{
return TextAttribute{};
}

std::pair<COLORREF, COLORREF> GetAttributeColors(const TextAttribute& /*attr*/) const noexcept override
{
return std::make_pair(COLORREF{}, COLORREF{});
}

COORD GetCursorPosition() const noexcept override
{
return COORD{};
}

bool IsCursorVisible() const noexcept override
{
return false;
}

bool IsCursorOn() const noexcept override
{
return false;
}

ULONG GetCursorHeight() const noexcept override
{
return 42ul;
}

CursorType GetCursorStyle() const noexcept override
{
return CursorType::FullBox;
}

ULONG GetCursorPixelWidth() const noexcept override
{
return 12ul;
}

COLORREF GetCursorColor() const noexcept override
{
return COLORREF{};
}

bool IsCursorDoubleWidth() const override
{
return false;
}

bool IsScreenReversed() const noexcept override
{
return false;
}

const std::vector<RenderOverlay> GetOverlays() const noexcept override
{
return std::vector<RenderOverlay>{};
}

const bool IsGridLineDrawingAllowed() noexcept override
{
return false;
}

const std::wstring GetConsoleTitle() const noexcept override
{
return std::wstring{};
}

const bool IsSelectionActive() const override
{
return false;
}

const bool IsBlockSelection() const noexcept override
{
return false;
}

void ClearSelection() override
{
}

void SelectNewRegion(const COORD /*coordStart*/, const COORD /*coordEnd*/) override
{
}

const COORD GetSelectionAnchor() const noexcept
{
return COORD{};
}

const COORD GetSelectionEnd() const noexcept
{
return COORD{};
}

void ColorSelection(const COORD /*coordSelectionStart*/, const COORD /*coordSelectionEnd*/, const TextAttribute /*attr*/)
{
}
};

void VtIoTests::RendererDtorAndThread()
{
Log::Comment(NoThrowString().Format(
L"Test deleting a Renderer a bunch of times"));

for (int i = 0; i < 16; ++i)
{
auto data = std::make_unique<MockRenderData>();
auto thread = std::make_unique<Microsoft::Console::Render::RenderThread>();
auto* pThread = thread.get();
auto pRenderer = std::make_unique<Microsoft::Console::Render::Renderer>(nullptr, nullptr, 0, std::move(thread));
auto pRenderer = std::make_unique<Microsoft::Console::Render::Renderer>(data.get(), nullptr, 0, std::move(thread));
VERIFY_SUCCEEDED(pThread->Initialize(pRenderer.get()));
// Sleep for a hot sec to make sure the thread starts before we enable painting
// If you don't, the thread might wait on the paint enabled event AFTER
Expand All @@ -286,9 +426,10 @@ void VtIoTests::RendererDtorAndThreadAndDx()

for (int i = 0; i < 16; ++i)
{
auto data = std::make_unique<MockRenderData>();
auto thread = std::make_unique<Microsoft::Console::Render::RenderThread>();
auto* pThread = thread.get();
auto pRenderer = std::make_unique<Microsoft::Console::Render::Renderer>(nullptr, nullptr, 0, std::move(thread));
auto pRenderer = std::make_unique<Microsoft::Console::Render::Renderer>(data.get(), nullptr, 0, std::move(thread));
VERIFY_SUCCEEDED(pThread->Initialize(pRenderer.get()));

auto dxEngine = std::make_unique<::Microsoft::Console::Render::DxEngine>();
Expand Down
13 changes: 6 additions & 7 deletions src/renderer/base/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ Renderer::Renderer(IRenderData* pData,
_In_reads_(cEngines) IRenderEngine** const rgpEngines,
const size_t cEngines,
std::unique_ptr<IRenderThread> thread) :
_pData(pData),
_pData(THROW_HR_IF_NULL(E_INVALIDARG, pData)),
_pThread{ std::move(thread) },
_destructing{ false },
_clusterBuffer{}
_clusterBuffer{},
_viewport{ pData->GetViewport() }
{
_srViewportPrevious = { 0 };

for (size_t i = 0; i < cEngines; i++)
{
IRenderEngine* engine = rgpEngines[i];
Expand Down Expand Up @@ -208,7 +207,7 @@ void Renderer::TriggerSystemRedraw(const RECT* const prcDirtyClient)
// - <none>
void Renderer::TriggerRedraw(const Viewport& region)
{
Viewport view = _pData->GetViewport();
Viewport view = _viewport;
SMALL_RECT srUpdateRegion = region.ToExclusive();

if (view.TrimToViewport(&srUpdateRegion))
Expand Down Expand Up @@ -357,7 +356,7 @@ void Renderer::TriggerSelection()
// - True if something changed and we scrolled. False otherwise.
bool Renderer::_CheckViewportAndScroll()
{
SMALL_RECT const srOldViewport = _srViewportPrevious;
SMALL_RECT const srOldViewport = _viewport.ToInclusive();
SMALL_RECT const srNewViewport = _pData->GetViewport().ToInclusive();

COORD coordDelta;
Expand All @@ -369,7 +368,7 @@ bool Renderer::_CheckViewportAndScroll()
LOG_IF_FAILED(engine->UpdateViewport(srNewViewport));
}

_srViewportPrevious = srNewViewport;
_viewport = Viewport::FromInclusive(srNewViewport);

// If we're keeping some buffers between calls, let them know about the viewport size
// so they can prepare the buffers for changes to either preallocate memory at once
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/base/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ namespace Microsoft::Console::Render

[[nodiscard]] HRESULT _PerformScrolling(_In_ IRenderEngine* const pEngine);

SMALL_RECT _srViewportPrevious;
Microsoft::Console::Types::Viewport _viewport;

static constexpr float _shrinkThreshold = 0.8f;
std::vector<Cluster> _clusterBuffer;
Expand Down

0 comments on commit 81b7e54

Please sign in to comment.