Repository: microsoft/terminal (language: cpp)

Issue:
Increase API buffer cutoff to 128KiB
> I figured out why this happens: When we read from the console pipe, we currently need to provide a buffer where the message is copied into, similar to `ReadFile`. In order to slightly amortize the cost of allocating a buffer we reuse the previous allocation unless the previous one was >16KiB and the next one is less than half the size. This avoids issues where a huge 100MB buffer never gets freed.
> This however does not work well for this usage of the API, because the `GetConsoleProcessList` call needs 128KiB and the `PeekConsoleInput` call only needs 20 (?) bytes. This causes the buffer to be repeatedly freed and reallocated.
> 
> IMO there's 3 things we should do:
> * cygwin should avoid making "large" data requests if there's no reason for that. That `DWORD buf[65536];` buffer is 128KiB large and in practice maybe 16 byte of that will ever be used. It could improve the code by only increasing the request size if the return value is equal to the buffer size.
> * We should increase the cutoff size from 16KiB to 128KiB, because that's the size of the `cat` chunk size anyway.
> * We should switch to a cheap allocator for processing console messages in the future, because malloc is a poor fit for it. But this doesn't fix the issue for large allocations, since we still need to be conservative about our resident set size. This means that even with a custom allocator, cygwin's usage pattern may still be suboptimal. 

 _Originally posted by @lhecker in [#18264](https://github.com/microsoft/terminal/issues/18264#issuecomment-2515448548)_


Discussion / hints:

Fix patch:
diff --git a/src/server/ApiMessage.cpp b/src/server/ApiMessage.cpp
index ff7b5ff768f..2603ba0a62b 100644
--- a/src/server/ApiMessage.cpp
+++ b/src/server/ApiMessage.cpp
@@ -108,7 +108,7 @@ try
 
         // 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)
+        if (_inputBuffer.capacity() > 128 * 1024 && (_inputBuffer.capacity() >> 1) > cbReadSize)
         {
             _inputBuffer.shrink_to_fit();
         }
@@ -151,7 +151,7 @@ try
 
         // 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)
+        if (_outputBuffer.capacity() > 128 * 1024 && (_outputBuffer.capacity() >> 1) > cbWriteSize)
         {
             _outputBuffer.shrink_to_fit();
         }


Test patch: