Your New Mental Model of constexpr - Jason Turner - CppCon 2021

preview_player
Показать описание
---
In my experience as a trainer, YouTuber, and speaker, I find that many people have a mistaken impression of what constexpr is and for what it is suited. I see people over-complicate constexpr by conflating it with meta programming and template-related topics. Students will often fail to recognize where and when constexpr could be used. Experts will get concerned about the "contract" you are making with the user of your library by marking functions as constexpr.

In this talk, I will present a mental model for how you should consider constexpr. I will explain what constexpr is (less mechanically and more metaphorically), give practical applications for constexpr, and help you figure out where constexpr fits into your application or library.

In the most straightforward sense, constexpr is moving work from runtime to compile-time. This mental model that I will present will ask, "what work would you rather do at compile-time?" We will find this answer by looking at constexpr as a continuum. On one end of the continuum is the use case of moving a runtime fixed-size vector into a compile-time fixed-size constexpr array, and on the far end is executing your entire program at compile-time and simply outputting the results at runtime.

---
Jason Turner

I'm available for contracting and onsite training.

---

*--*
Рекомендации по теме
Комментарии
Автор

Great talk. I always enjoy Jason's presentations, but I'm still waiting for constexpr Doom :-p

OperationDarkside
Автор

Thank you Jason and cppcon for this! Everytime I watch one of Jason's talk, I start with skepticism, because the headline doesn't appeal much and the beginning sounds like old news, but then I keep watching because it's cppcon, it means more than that and I know better about Jason, so I wait for the moment in his talk where I'll flip and go like "ok now we're talking", then it comes, time to share! At that point I start sending links to colleagues and friends, and we start discussing about the real deal that this talk is about, and we start deconstructing the bits and pieces of what has just been presented to us, just so we can come to the same conclusions by our own, and appreciate even more all the outcomes of it, and that's is the really cool part. Thank you Jason and everyone involved for allowing us to grow and contribute through what you share to the community, much appreciated!

pippzzz
Автор

Wow, thank you very much for this presentation! I sense this is very very important for the performance limited embedded systems and their optimizations!

joonasmakinen
Автор

constexpr example I did:
for a given time interval, with a given clock speed, find the best fitting clock divider/top count value for the timer of an Atmel chip (Arduino) and into just 6 instructions to fill the 3 register bytes used.

fromgermany
Автор

In the end using constexpr (if at all applicable) or not boils down to a basic standard software engineering questions : do you trade memory for speed. I mention this because the compile time generated data tables may be bigger then the runtime code needed to generate those tables and this might be an issue on smaller systems.

peregrin
Автор

I always enjoy your constexpr talks Jason. It makes me reconsider what work could reasonably be done at compile time in my work. I've been pushing to constexpr more over the last year or so. Also I got the Back to the Future joke. :)

adammitchell
Автор

This talk was very hard to follow, but I glad that I did it.

kamilziemian
Автор

Yesss, new Jason talk! I was looking forward to this the whole year

nothke
Автор

29:00 also you don't always want to have everything possible at compile time. When working on bigger projects compiles might take hours and heavy use of constexpr and templates do add to that and make debugging more annoying. Also this can effectively DOS your build server. And i these cases where there is no real time performance requirement or any other hard run time performance requirements I think it can make sense to also do things at runtime, even though they would be possible at compile time.

Also as jason mentioned: compile time computation often produces a bigger binary, because it has to store the computation results somehow (can also make the binary smaller, so this is not a rule). But if you have a storage medium limitation, you sometimes have to generate things at runtime. I think the game KKrieger is an amazing and extreme example of this.

So it is always a question of what you value most. And C++ always is about giving the user the option/power. So having the compiler decide what should be constexpr would go against one principle of C++ that make many people love the language so much.

Erarnitox
Автор

My favorite part of every cppcon: constexpr

Voltra_
Автор

In my experience, an audience that doesn't have a question doesn't usually mean they're an expert in the topic at hand, but rather that they haven't understood anywhere near enough to even consider considering questions.

ixiwildflowerixi
Автор

I'm writing a toy database engine and I'm trying to do as much as I can at compile time. I've been at it off an on for more than two years, almost have a working proof of concept now, and I have very little runtime code. You can do a lot at compile time if you design for it from scratch, and the most difficult problem I've had was serializing and de-serializing (because de-serialization from storage happens at runtime by definition, yet it must produce compile-time-constant results so that further processing of the results can happen at compile time again). Had to redesign this part three times to avoid insane compiler recursion depth and code bloat.

VioletGiraffe
Автор

This talk has inspired me to code the presentation slide! I have written an article with the title "Code Your Presentation Slides as an Executable Program" on Medium.

pllee
Автор

I think constexpr is about dealing with static information. Static information is information that is known at compile time. If initial information, data is static we can process it as much as we want in compile time. So like presentation here, many resource is known at compile time already, we can process it as much as we want(of course there might be limitation in current C++, but it is not about fundamental limitation). On the other hand, dynamic information, which is information that is created in runtime(like user input data), can not be handled in constexpr context. Trade off might be memory space and compile time.

jhk
Автор

I remember writing a font scaler in GW-BASIC on a Tandy 1000 back in the early 1990s that scaled up the CGA font in roughly the same way that Jason's approximation of hq2x ends up doing. I believe the actual hq2x algorithm, however, is more sophisticated, since it deals with color. I was only working with 1bpp CGA font bitmaps.

And the result looked pretty much identical to the smoothed font in Jason's preso. It was a fun blast from the past for me. 😀

And in case anyone wonders, I'm referring to the _original_ Tandy 1000, with no suffix letters. I had an add-in card to take it from 128K to 640K. It was like a "Super PCjr" in many ways.

intvnut
Автор

I both love and hate constexpr. As a game modder, I don't want any of a game evaluated at compile time and hidden from me, but as a game modder, I want as much of my own code not adding performance overhead at runtime :)

MrGencyExit
Автор

Jason, great presenter as always. Thanks also for your YouTube channel!

aloluk
Автор

WOOH! YEAH BABY! THAT'S WHO I'VE BEEN WAITING FOR, CONSTEXPR IS WHAT IT'S ALL ABOUT.

dexus
Автор

Love it!
Jason running PPT on C64.
At least according to the fonts.
But content is key! 👍🏻👍🏻👍🏻

PS.: i swear, I wrote this after seeing his first „slide“

fromgermany
Автор

Jason Turner is some kind of C++ superman. And constexpert.

kamilziemian