Skip to content
Open
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
19 changes: 19 additions & 0 deletions src/terminal/parser/stateMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2009,6 +2009,25 @@ void StateMachine::ProcessString(const std::wstring_view string)
_runOffset = i;
_runSize = 0;
}

if (_state == VTStates::Ground &&
string.size() - i >= 2 &&
til::at(string, i) == AsciiChars::ESC &&
til::at(string, i + 1) == L'[' &&
!_isEngineForInput &&
_parserMode.test(Mode::Ansi) &&
!_trace.IsEnabled())
Comment on lines +2013 to +2019

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What is the perf improvement of this change alone? The PR description does not establish whether this individual change results in a significant improvement.

My 50ct are that we should rewrite the VT parser from a push to a pull parser, as this is easier to unit test, fuzz, and to optimize. However, this requires a major rewrite.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fair. Measuring it alone, will post the number. If it's noise I'll close it. Agree a pull parser is the real fix.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Measured it alone vs main: sgr +7.6% throughput, -7.0% CPU; cursor +7.7%, -7.5%; seq flat. Table's in the description. Keeping it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

However, the way this is written both leaves perf on the table and is also written in a somewhat odd way. I think this isn't a bad idea per se due to how common CSIs are but I'd like to see this cleaned up.

{
// Only Ground reaches this scan. Interrupt is a no-op here;
// retain both entry actions and the raw prefix for passthrough.
_runSize = 1;
_processingLastCharacter = false;
_EnterEscape();
_runSize = 2;
_processingLastCharacter = i + 2 == string.size();
_EnterCsiEntry();
i += 2;
}
}

processStringLoopVtStart:
Expand Down
5 changes: 5 additions & 0 deletions src/terminal/parser/tracing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ void ParserTracing::AddSequenceTrace(const wchar_t wch)
}
}

bool ParserTracing::IsEnabled() const noexcept
{
return TraceLoggingProviderEnabled(g_hConsoleVirtTermParserEventTraceProvider, WINEVENT_LEVEL_VERBOSE, TIL_KEYWORD_TRACE);
}

void ParserTracing::DispatchSequenceTrace(const bool fSuccess) noexcept
{
if (fSuccess)
Expand Down
1 change: 1 addition & 0 deletions src/terminal/parser/tracing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace Microsoft::Console::VirtualTerminal
void TraceCharInput(const wchar_t wch);

void AddSequenceTrace(const wchar_t wch);
bool IsEnabled() const noexcept;
void DispatchSequenceTrace(const bool fSuccess) noexcept;
void ClearSequenceTrace() noexcept;
void DispatchPrintRunTrace(const std::wstring_view& string) const;
Expand Down
48 changes: 48 additions & 0 deletions src/terminal/parser/ut_parser/StateMachineTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ class Microsoft::Console::VirtualTerminal::StateMachineTest
TEST_METHOD(RunStorageBeforeEscape);
TEST_METHOD(BulkTextPrint);
TEST_METHOD(PassThroughUnhandledSplitAcrossWrites);
TEST_METHOD(CsiPrefixDispatchAcrossWrites);
TEST_METHOD(CsiPrefixPreservesPassthroughAndCallback);

TEST_METHOD(DcsDataStringsReceivedByHandler);
TEST_METHOD(DcsParamIgnoresControlCharacters);
Expand Down Expand Up @@ -281,6 +283,52 @@ void StateMachineTest::PassThroughUnhandledSplitAcrossWrites()
VERIFY_ARE_EQUAL(L"", engine.printed);
}

void StateMachineTest::CsiPrefixDispatchAcrossWrites()
{
const std::wstring_view sequence{ L"\x1b[12;34H" };
for (size_t split = 0; split <= sequence.size(); ++split)
{
auto enginePtr = std::make_unique<TestStateMachineEngine>();
auto& engine = *enginePtr;
StateMachine machine{ std::move(enginePtr) };
machine.ProcessString(sequence.substr(0, split));
machine.ProcessString(sequence.substr(split));

const uint64_t expectedId = VTID("H");
VERIFY_ARE_EQUAL(expectedId, engine.csiId);
const std::vector<size_t> expected{ 12, 34 };
VERIFY_ARE_EQUAL(expected, engine.csiParams);
VERIFY_IS_TRUE(machine.IsProcessingLastCharacter());
}
}

void StateMachineTest::CsiPrefixPreservesPassthroughAndCallback()
{
const std::wstring_view sequence{ L"\x1b[?1234h" };
for (size_t split = 0; split <= sequence.size(); ++split)
{
auto enginePtr = std::make_unique<TestStateMachineEngine>();
auto& engine = *enginePtr;
StateMachine machine{ std::move(enginePtr) };
size_t callbacks = 0;
engine.pfnFlushToTerminal = [&]() {
VERIFY_IS_TRUE(machine.IsProcessingLastCharacter());
return machine.FlushToTerminal();
};
machine.OnCsiComplete([&]() {
++callbacks;
machine.ProcessString(L"callback");
});
machine.ProcessString(sequence.substr(0, split));
machine.ProcessString(sequence.substr(split));
machine.ProcessString(L"tail");

VERIFY_ARE_EQUAL(sequence, std::wstring_view{ engine.passedThrough });
VERIFY_ARE_EQUAL(L"callbacktail", engine.printed);
VERIFY_ARE_EQUAL(size_t{ 1 }, callbacks);
}
}

void StateMachineTest::DcsDataStringsReceivedByHandler()
{
BEGIN_TEST_METHOD_PROPERTIES()
Expand Down