STL icon indicating copy to clipboard operation
STL copied to clipboard

Implement P3107R5 optimized `<print>`

Open blackninja9939 opened this issue 1 year ago • 3 comments

Fixes #4509

Is my first PR so I expect plenty of fun times and issues 😅

In my benchmarking the non-buffered version is about 400-700ns faster, which is not as much as I would have hoped, but I do not think there is a huge amount of room in the design of format and Window's write API for great gains.

The format API ties the iterator type to everything, so we always must go through _Fmt_iterator_buffer writing char's into the buffer and flushing on demand, so using _fputc_nolock is hard since we never actually do anything character by character for the wrapped iterator and the type is important. Instead I've generalised the optimization vector and string were doing of a customization point for the wrapped iterator when we flush and used that in these custom iterators to write to the stream or to the console with the parent holding a lock. This avoids need to ever allocate the final std::string or re-take the lock.

If we write to the console we must additionally transcode to wchar_t so we can call WriteConsoleW, I've reduced allocation there as well since it can often fit in a buffer but it is still work being done.

Its still a speedup, and I'm sure there is extra room for gains, but I tried various approaches and this was the "simplest" and reduced the most allocation calls.

blackninja9939 avatar Jul 08 '24 16:07 blackninja9939

@blackninja9939 please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@microsoft-github-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@microsoft-github-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@microsoft-github-policy-service agree company="Microsoft"

Contributor License Agreement

@microsoft-github-policy-service agree

blackninja9939 avatar Jul 08 '24 16:07 blackninja9939

If you edit your PR description to use the GitHub magic phrase "Fixes #4509" instead of "Implement", that will auto-link the issue to the PR, such that merging the PR will close the issue. Read about GitHub's close/fix/resolve syntax for more info and synonyms.

StephanTLavavej avatar Jul 08 '24 17:07 StephanTLavavej

libc++ tests fail due to old version expectations.

You need to fix up expected results, like here:

https://github.com/microsoft/STL/blob/ef1d621d51263285aff8e560a214f5477d63d687/tests/libcxx/expected_results.txt#L119-L121

AlexGuteniev avatar Jul 09 '24 05:07 AlexGuteniev

I've gotten clarification from LWG on what the defective wording intends to say. I'll push changes for my review comments in the next couple of days (and prepare an LWG issue to correct the wording).

CaseyCarter avatar Aug 28 '24 21:08 CaseyCarter

Thanks, this is incredible! :heart_eyes_cat: I pushed individual commits for the remaining issues I noticed, all small (the absence of const on a test format being the most significant).

We merge PRs to the GitHub and MSVC-internal repos in a semi-manual process, batched up to save time. Your PR will be part of the next batch, likely next week. I'll post comments here as I prepare it for merging!

StephanTLavavej avatar Aug 30 '24 23:08 StephanTLavavej

I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

StephanTLavavej avatar Sep 03 '24 20:09 StephanTLavavej

Thanks for implementing this C++23 DR, and congratulations on your first microsoft/STL commit! :heart_eyes_cat: :partying_face: :cake:

This change is expected to ship in Preview 1 of whatever comes after VS 2022 17.12 (we don't know what it'll be yet, so we're calling it VS Meow in the Changelog until we do know).

StephanTLavavej avatar Sep 04 '24 04:09 StephanTLavavej