Code Health: Optimize C++ Code for performance #8838

Closed
opened 2026-01-31 01:39:13 +00:00 by claunia · 4 comments
Owner

Originally created by @WSLUser on GitHub (Jun 5, 2020).

Description of the new feature/enhancement

As discussed in https://github.com/microsoft/terminal/issues/6350, we do not appear to be optimizing our C++ code to run faster I/O (while still maintaining safety). We should optimize code using iostream and other related header libraries but preferably with fast_io, which is fastest currently in existence, which aims to become part of the C++20 standard. https://www.thegeekstuff.com/2015/01/c-cpp-code-optimization/ is a general guide on how to optimize our code. https://github.com/expnkx/fast_io/wiki provides specific examples for different types of operations that can be optimized using fast_io.

Proposed technical implementation details (optional)

Start by merging adding #5152 and https://github.com/microsoft/terminal/pull/5080 but those will provide only small benefit. Otherwise use the fast_io wiki to help optimize code as PRs are created if relevant to the PR. Depends on: https://github.com/microsoft/terminal/issues/6350 being implemented.

Originally created by @WSLUser on GitHub (Jun 5, 2020). <!-- 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 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 As discussed in https://github.com/microsoft/terminal/issues/6350, we do not appear to be optimizing our C++ code to run faster I/O (while still maintaining safety). We should optimize code using iostream and other related header libraries but preferably with fast_io, which is fastest currently in existence, which aims to become part of the C++20 standard. https://www.thegeekstuff.com/2015/01/c-cpp-code-optimization/ is a general guide on how to optimize our code. https://github.com/expnkx/fast_io/wiki provides specific examples for different types of operations that can be optimized using fast_io. <!-- A clear and concise description of what the problem is that the new feature would solve. Describe why and how a user would use this new functionality (if applicable). --> # Proposed technical implementation details (optional) Start by merging adding #5152 and https://github.com/microsoft/terminal/pull/5080 but those will provide only small benefit. Otherwise use the fast_io wiki to help optimize code as PRs are created if relevant to the PR. Depends on: https://github.com/microsoft/terminal/issues/6350 being implemented. <!-- A clear and concise description of what you want to happen. -->
claunia added the Issue-QuestionNeeds-Tag-FixResolution-Answered labels 2026-01-31 01:39:13 +00:00
Author
Owner

@beviu commented on GitHub (Jun 5, 2020):

I'm pretty sure that most points on that article (1, 2, 4, 5, 6, 7 and 10) are wrong because the compiler can already do them automatically.
There is a website to check what assembly the compiler generates: https://godbolt.org/
For example, if we try with the first example on that article and turn optimization on, then we get the same result for the "slower" code than the "faster" code: https://godbolt.org/z/T5fM5y (EDIT: wrong link) (I've modified it a little bit to remove useless code).
Note that I'm not a pro at c++.

@beviu commented on GitHub (Jun 5, 2020): I'm pretty sure that most points on that article (1, 2, 4, 5, 6, 7 and 10) are wrong because the compiler can already do them automatically. There is a website to check what assembly the compiler generates: https://godbolt.org/ For example, if we try with the first example on that article and turn optimization on, then we get the same result for the "slower" code than the "faster" code: https://godbolt.org/z/T5fM5y (EDIT: wrong link) (I've modified it a little bit to remove useless code). Note that I'm not a pro at c++.
Author
Owner

@WSLUser commented on GitHub (Jun 5, 2020):

It's also from 2015 so I wouldn't be surprised if some things changed. It's good the compiler can do some stuff for us but we eventually want to be cross-platform. It might be better to be explicit so we don't have to rely on the compiler. Regardless, using fast_io will still be better than any compiler optimizations (at least until it's included like fmt is into the standard).

@WSLUser commented on GitHub (Jun 5, 2020): It's also from 2015 so I wouldn't be surprised if some things changed. It's good the compiler can do some stuff for us but we eventually want to be cross-platform. It might be better to be explicit so we don't have to rely on the compiler. Regardless, using fast_io will still be better than any compiler optimizations (at least until it's included like fmt is into the standard).
Author
Owner

@DHowett commented on GitHub (Jun 5, 2020):

Thanks for filing this! I'd prefer to keep track of performance issues at a finer scale than this, however. Performance informs everything we do, and I'm not ready to declare a flag day where the five of us sit down and pore over perf traces and rewrite large swaths of code based on those numbers.

When we find hot spots, we'll file them. When we don't find hot spots, we look for better and better corpuses of data so that we can find hot spots. Only from there can we file workitems to drive those hot spots back down. 😄

Closing for now, but I'd like to retain this as a discussion issue.

@DHowett commented on GitHub (Jun 5, 2020): Thanks for filing this! I'd prefer to keep track of performance issues at a finer scale than this, however. Performance informs everything we do, and I'm not ready to declare a flag day where the five of us sit down and pore over perf traces and rewrite large swaths of code based on those numbers. When we find hot spots, we'll file them. When we _don't_ find hot spots, we look for better and better corpuses of data so that we _can_ find hot spots. Only from there can we file workitems to drive those hot spots back down. :smile: Closing for now, but I'd like to retain this as a discussion issue.
Author
Owner

@WSLUser commented on GitHub (Jun 5, 2020):

I was thinking of this as a master issue from which, we could file smaller issues as you've said that are tracked here. When I have some time, I'll be looking for a spot where I can introduce some optimized code using fast_io and bring the dep in with that as an example where we could use it. Of course somebody else can beat me to it and that won't bother me at all. Best place I can think of currently is where we actually have used fmt, which should provide some gain though not as much as somewhere in the code that hasn't been optimized at all.

@WSLUser commented on GitHub (Jun 5, 2020): I was thinking of this as a master issue from which, we could file smaller issues as you've said that are tracked here. When I have some time, I'll be looking for a spot where I can introduce some optimized code using fast_io and bring the dep in with that as an example where we could use it. Of course somebody else can beat me to it and that won't bother me at all. Best place I can think of currently is where we actually have used fmt, which should provide some gain though not as much as somewhere in the code that hasn't been optimized at all.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#8838