Get rid of buffering in the partials handling of u8u16 and u16u8 #14871

Closed
opened 2026-01-31 04:21:49 +00:00 by claunia · 6 comments
Owner

Originally created by @german-one on GitHub (Aug 13, 2021).

Description of the new feature/enhancement

The state classes used for the partials handling contain a buffer which is used to provide a preprocessed string that contains only complete code points for the conversion. This means that each and every incoming string is copied to the buffer first. And this buffer exists for the life time of the state instance. @lhecker already implemented a check in order to shrink the buffer if it grows too much. However, a few days later a facepalm moment appeared to me because buffering of the whole string is not necessary at all. We just need to complete the partial code point in the cache and convert it separately.
It's
currently: copying the whole string in a persistent buffer every time the function is called
vs.
proposed: no buffering, but possibly calling MB2WC or WC2MB twice (only if partials have been cached)

Proposed technical implementation details (optional)

Something about like that for u8u16 (would be quite similar for u16u8 using an array of 2 wchar_t)
nobuffer

Originally created by @german-one on GitHub (Aug 13, 2021). <!-- 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 I ACKNOWLEDGE THE FOLLOWING BEFORE PROCEEDING: 1. If I delete this entire template and go my own path, the core team may close my issue without further explanation or engagement. 2. If I list multiple bugs/concerns in this one issue, the core team may close my issue without further explanation or engagement. 3. If I write an issue that has many duplicates, the core team may close my issue without further explanation or engagement (and without necessarily spending time to find the exact duplicate ID number). 4. If I leave the title incomplete when filing the issue, the core team may close my issue without further explanation or engagement. 5. If I file something completely blank in the body, the core team may close my issue without further explanation or engagement. All good? Then proceed! --> # Description of the new feature/enhancement The `state` classes used for the partials handling contain a buffer which is used to provide a preprocessed string that contains only complete code points for the conversion. This means that each and every incoming string is copied to the buffer first. And this buffer exists for the life time of the `state` instance. @lhecker already implemented a check in order to shrink the buffer if it grows too much. However, a few days later a facepalm moment appeared to me because buffering of the whole string is not necessary at all. We just need to complete the partial code point in the cache and convert it separately. It's currently: _copying the whole string in a persistent buffer every time the function is called_ vs. proposed: _no buffering, but possibly calling `MB2WC` or `WC2MB` twice (only if partials have been cached)_ # Proposed technical implementation details (optional) Something about like that for `u8u16` (would be quite similar for `u16u8` using an array of 2 `wchar_t`) <img width="721" alt="nobuffer" src="https://user-images.githubusercontent.com/46659645/129393515-5d75dfd8-9cf2-4c55-b106-8ab812833a19.png">
claunia added the Area-OutputResolution-Fix-CommittedIssue-TaskProduct-Terminal labels 2026-01-31 04:21:49 +00:00
Author
Owner

@lhecker commented on GitHub (Aug 13, 2021):

I've already prototyped a buffer-free converter a while ago: e7d00cab8e/src/inc/til/u8u16convert.h
I simply do the utf8/utf16 -> utf32 -> utf16/utf8 encoding for a single codepoint myself. But I never got around to finish it completely. I could do that sooner rather than later if we want to...

@lhecker commented on GitHub (Aug 13, 2021): I've already prototyped a buffer-free converter a while ago: https://github.com/microsoft/terminal/blob/e7d00cab8e84f6afde00951f8f6c6320308318a0/src/inc/til/u8u16convert.h I simply do the utf8/utf16 -> utf32 -> utf16/utf8 encoding for a single codepoint myself. But I never got around to finish it completely. I could do that sooner rather than later if we want to...
Author
Owner

@german-one commented on GitHub (Aug 13, 2021):

Looks good. Just one side note: Simplified conversion of the single code point has the risk of getting crap for invalid UTF-8. The API functions would shift in a replacement character, which is the reason why I'd prefer using them. You could c\p the checked implementation out of the U8U16Test project though. However, I'm not sure if it's even worth to think too much about it ... 😊

@german-one commented on GitHub (Aug 13, 2021): Looks good. Just one side note: Simplified conversion of the single code point has the risk of getting crap for invalid UTF-8. The API functions would shift in a replacement character, which is the reason why I'd prefer using them. You could c\p the checked implementation out of the U8U16Test project though. However, I'm not sure if it's even worth to think too much about it ... 😊
Author
Owner

@lhecker commented on GitHub (Aug 13, 2021):

@german-one Yeah we can use the win32 functions for that... Since they aren't actual syscalls (but just regular DLL functions) the call overhead is only a few nanoseconds anyways. 🙂

@lhecker commented on GitHub (Aug 13, 2021): @german-one Yeah we can use the win32 functions for that... Since they aren't actual syscalls (but just regular DLL functions) the call overhead is only a few nanoseconds anyways. 🙂
Author
Owner

@german-one commented on GitHub (Aug 16, 2021):

@lhecker I had some free time over the weekend. So I tried to work on my proposal and already have something that passed the unit tests locally. If you don't mind I'll file a draft PR somewhen within this week in order to get some feedback if I'm heading in the right direction. I just want to update the templatization of all overloads beforehand. I very much like your's that only allows string_views for the incoming data.

@german-one commented on GitHub (Aug 16, 2021): @lhecker I had some free time over the weekend. So I tried to work on my proposal and already have something that passed the unit tests locally. If you don't mind I'll file a draft PR somewhen within this week in order to get some feedback if I'm heading in the right direction. I just want to update the templatization of all overloads beforehand. I very much like your's that only allows string_views for the incoming data.
Author
Owner

@lhecker commented on GitHub (Aug 16, 2021):

@german-one Of course! Please feel free to submit a draft PR, if you'd like. 🙂

@lhecker commented on GitHub (Aug 16, 2021): @german-one Of course! Please feel free to submit a draft PR, if you'd like. 🙂
Author
Owner

@ghost commented on GitHub (Aug 31, 2021):

:tada:This issue was addressed in #10966, which has now been successfully released as Windows Terminal Preview v1.11.2421.0.🎉

Handy links:

@ghost commented on GitHub (Aug 31, 2021): :tada:This issue was addressed in #10966, which has now been successfully released as `Windows Terminal Preview v1.11.2421.0`.:tada: Handy links: * [Release Notes](https://github.com/microsoft/terminal/releases/tag/v1.11.2421.0) * [Store Download](https://www.microsoft.com/store/apps/9n8g5rfz9xk3?cid=storebadge&ocid=badge)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#14871