Merged PR 6286783: Release unneeded memory more eagerly from conhost

This is equivalent to commit 8779249b1, but reflected from the OS repository.

Retrieved from https://microsoft.visualstudio.com os.2020 OS official/rs_wdx_dxp_windev a4d67e9b05039f365a1a0c58e9c63474c58073a1

Related work items: MSFT-34777060
This commit is contained in:
Dustin Howett 2021-09-21 20:49:55 +00:00
parent 184919fb24
commit 431d51de4c
2 changed files with 24 additions and 2 deletions

View file

@ -55,6 +55,14 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
RETURN_HR_IF(E_ABORT, !base::CheckAdd(in.length(), _partialsLen).AssignIfValid(&capacity));
_buffer.clear();
// If we were previously called with a huge buffer we have an equally large _buffer.
// We shouldn't just keep this huge buffer around, if no one needs it anymore.
if (_buffer.capacity() > 16 * 1024 && (_buffer.capacity() >> 1) > capacity)
{
_buffer.shrink_to_fit();
}
_buffer.reserve(capacity);
// copy UTF-8 code units that were remaining from the previous call (if any)

View file

@ -104,7 +104,14 @@ try
{
RETURN_HR_IF(E_FAIL, State.ReadOffset > Descriptor.InputSize);
ULONG const cbReadSize = Descriptor.InputSize - State.ReadOffset;
const ULONG cbReadSize = Descriptor.InputSize - State.ReadOffset;
// If we were previously called with a huge buffer we have an equally large _inputBuffer.
// We shouldn't just keep this huge buffer around, if no one needs it anymore.
if (_inputBuffer.capacity() > 16 * 1024 && (_inputBuffer.capacity() >> 1) > cbReadSize)
{
_inputBuffer.shrink_to_fit();
}
_inputBuffer.resize(cbReadSize);
@ -145,10 +152,17 @@ try
ULONG cbWriteSize = Descriptor.OutputSize - State.WriteOffset;
RETURN_IF_FAILED(ULongMult(cbWriteSize, cbFactor, &cbWriteSize));
// If we were previously called with a huge buffer we have an equally large _outputBuffer.
// We shouldn't just keep this huge buffer around, if no one needs it anymore.
if (_outputBuffer.capacity() > 16 * 1024 && (_outputBuffer.capacity() >> 1) > cbWriteSize)
{
_outputBuffer.shrink_to_fit();
}
_outputBuffer.resize(cbWriteSize);
// 0 it out.
std::fill(_outputBuffer.begin(), _outputBuffer.end(), (BYTE)0);
std::fill_n(_outputBuffer.data(), _outputBuffer.size(), BYTE(0));
State.OutputBuffer = _outputBuffer.data();
State.OutputBufferSize = cbWriteSize;