Increase API buffer cutoff to 128KiB #22626

Closed
opened 2026-01-31 08:18:53 +00:00 by claunia · 0 comments
Owner

Originally created by @carlos-zamora on GitHub (Dec 4, 2024).

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

Originally created by @carlos-zamora on GitHub (Dec 4, 2024). > 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)_
claunia added the Needs-TriageIn-PRNeeds-Tag-Fix labels 2026-01-31 08:18:53 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#22626