Skip to content

Commit

Permalink
Throttle scroll position update (#3531)
Browse files Browse the repository at this point in the history
<!-- Enter a brief description/summary of your PR here. What does it fix/what does it change/how was it tested (even manually, if necessary)? -->
## Summary of the Pull Request

Another tiny performance fix.

<!-- Provide a more detailed description of the PR, other things fixed or any additional comments/features here -->
## Detailed Description of the Pull Request / Additional comments

Correct me if I'm wrong, It doesn't really make sense to update scroll status faster than frame rate limit.

<!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well -->
## Validation Steps Performed

<hr>

* Throttle scroll position update

* Review
  • Loading branch information
skyline75489 authored and zadjii-msft committed Nov 13, 2019
1 parent 306e751 commit 1177815
Showing 2 changed files with 21 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
@@ -20,6 +20,10 @@ using namespace winrt::Windows::UI::Core;
using namespace winrt::Windows::System;
using namespace winrt::Microsoft::Terminal::Settings;

// Limit the rate of scroll update operation
// See also: Microsoft::Console::Render::RenderThread::s_FrameLimitMilliseconds
constexpr long long ScrollRateLimitMilliseconds = 8;

namespace winrt::Microsoft::Terminal::TerminalControl::implementation
{
// Helper static function to ensure that all ambiguous-width glyphs are reported as narrow.
@@ -54,6 +58,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
_settings{ settings },
_closing{ false },
_lastScrollOffset{ std::nullopt },
_lastScrollTime{ std::nullopt },
_autoScrollVelocity{ 0 },
_autoScrollingPointerPoint{ std::nullopt },
_autoScrollTimer{},
@@ -1412,6 +1417,21 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
return;
}

// Throttle the update operation.
const auto timeNow = std::chrono::high_resolution_clock::now();

if (_lastScrollTime.has_value())
{
const long long deltaTimeInMilliSec = std::chrono::duration_cast<std::chrono::milliseconds>(timeNow - _lastScrollTime.value()).count();
if (deltaTimeInMilliSec < ScrollRateLimitMilliseconds)
{
_lastScrollTime = std::nullopt;
return;
}
}

_lastScrollTime = timeNow;

// Update our scrollbar
_scrollBar.Dispatcher().RunAsync(CoreDispatcherPriority::Low, [=]() {
// Even if we weren't closed/closing few lines above, we might be
1 change: 1 addition & 0 deletions src/cascadia/TerminalControl/TermControl.h
Original file line number Diff line number Diff line change
@@ -113,6 +113,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
FontInfo _actualFont;

std::optional<int> _lastScrollOffset;
std::optional<std::chrono::high_resolution_clock::time_point> _lastScrollTime;

// Auto scroll occurs when user, while selecting, drags cursor outside viewport. View is then scrolled to 'follow' the cursor.
double _autoScrollVelocity;

0 comments on commit 1177815

Please sign in to comment.