TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

The Sad State of Debug Performance in C++

146 点作者 SuperV1234超过 2 年前

17 条评论

_gabe_超过 2 年前
I ran into these problems with std::unordered_map. While developing my game, I would always run it in debug mode because I need to debug it. Loading a game world was taking around 12 seconds, and it was beginning to grate on me because of the increased iteration times. So I decided to profile it and see what was taking so long. I compiled in release mode to begin debugging, and it magically took the world load time down to 2 seconds.<p>I wondered what code I had written that caused such a massive performance hit in debug mode. So I went to profile my code in a debug build to find out. Lo and behold, something like 90% of the CPU time was wasted doing lookups from std::unordered_map because of debug iterators. I tried everything I could to just turn debug iterators off, and eventually gave up and switched to robin_hood::unordered_map which runs great in debug and release. Now I have an unwarranted aversion to the std lib, even though it really is great so long as you&#x27;re running it in release mode.
评论 #32985726 未加载
评论 #32986160 未加载
评论 #32987043 未加载
评论 #32987164 未加载
评论 #32987093 未加载
评论 #32986112 未加载
Deganta超过 2 年前
The problem is that C++ is full of so called &quot;zero-cost-abstractions&quot; that are only zero cost if you ignore:<p>1. Compile time<p>2. Non-optimized builds<p>Stuffing everything into the Standard Library using lots of template magic does has its upside, but debug performance is one of the big downsides.<p>Another one are really clunky interfaces, like std::variant. This should really have been a language feature, not a library features.
评论 #32983762 未加载
评论 #32988159 未加载
PaulDavisThe1st超过 2 年前
I&#x27;ve been using C++ since 1993. I still don&#x27;t understand how I&#x27;ve written all this code and never once found myself using, or wishing I could use std::move.<p>Presumably my code has been less than optimally efficient or something, because it seems that most people talking about &quot;modern&quot; C++ view it as absolutely central to the language.
评论 #32987185 未加载
评论 #32987283 未加载
评论 #32986839 未加载
评论 #33023502 未加载
评论 #32991378 未加载
评论 #32988126 未加载
glandium超过 2 年前
&gt; Even if -Og was ubiquitous, it is still suboptimal to -O0: it can still inline code a bit too aggressively for an effective debugging session.<p>IMO, the problem is not so much the inlining, but the sad state of the optimization passes losing track of many things and producing useless debug info.
评论 #32982636 未加载
评论 #32982208 未加载
BenFrantzDale超过 2 年前
It’s always nice to understand more about why game developers do the weird things they do. I always buried and run in “RelWithDebInfo”, then if I really need it, I’ll throw `#pragma clang optimize off` around the code that the debugger can’t see into.
评论 #32983164 未加载
humanrebar超过 2 年前
Have any multibillion dollar game (etc.) companies invested any resources into debug build modes in the open source compilers they develop with? Seems like it should be trivial to tie team productivity rates to quality of the debug experience.
评论 #32983323 未加载
评论 #32983183 未加载
评论 #32983078 未加载
评论 #32984163 未加载
评论 #32983366 未加载
alternatetwo超过 2 年前
MSVC has &#x2F;Ob1 [1] which only inlines specifically marked functions, or functions defined in a header. Shouldn&#x27;t this inline all those trivial calls yet keep <i>most</i> debug info if you disable all the other optimizations? I haven&#x27;t experimented with it yet *or what I rather mean is use Debug builds, but enable &#x2F;Ob1 in them.<p>[1]: <a href="https:&#x2F;&#x2F;learn.microsoft.com&#x2F;en-us&#x2F;cpp&#x2F;build&#x2F;reference&#x2F;ob-inline-function-expansion?view=msvc-170" rel="nofollow">https:&#x2F;&#x2F;learn.microsoft.com&#x2F;en-us&#x2F;cpp&#x2F;build&#x2F;reference&#x2F;ob-inl...</a>
staticassertion超过 2 年前
With our rust project we actually compile with `01` because the cost of that optimization pass is less than the cost to our tests running 100x slower. There is definitely a need for a &#x27;more optimal&#x27; debug mode, and it is one of those things that feels zero cost until it isn&#x27;t. Similarly, I try to at least consider compile times when writing code these days.
andrewmcwatters超过 2 年前
You know, at least in C++ you get some really poorly performing standard containers. Every time I reach for C, it drives me nuts that the best you get is GLib.<p>And BOTH pale in comparison to newer language standard libraries like Go&#x27;s.
phaedrus超过 2 年前
This dovetails with something I was thinking about recently in the context of Reproducible Builds (or the lack thereof). For most of its history Computer Science (at least as practiced by industry) has gone all-in on &quot;functionally equivalent&quot; transformations, and done almost nothing in the space of &quot;output invariant&quot; guarantees.<p>In my day job we sometimes we need to reproduce the build of a firmware ROM or executable, sometimes decades after the engineer who last built it left. Getting a match is easier (or even possible) for older tech <i>only</i> because of the relative unsophistication of the compilers used - even then it&#x27;s only reproducible &quot;by accident,&quot; and not because the compiler vendor made any guarantees about X language construct reliably produces Y machine code and data layout.<p>But we need that! For getting accurate baselines. For security verification. And there&#x27;s no reason <i>in principle</i> we should have to forego updating compilers, IDEs, and OS environments in the indirect hope of not disturbing anything. Those are two separate things: if we had a through-line of higher level language construct -&gt; semantically defined transformation (irregardless of optimization settings) -&gt; machine code, vendors could continue to update their IDEs and compilers while just making sure they still respect the invariants.<p>C++&#x27;s so-called zero-cost abstractions are poor substitute for this: header (library) writers and C++ gurus write code <i>as if</i> they worked like this &quot;guaranteed output transform&quot; I describe, but no compiler actually has to respect it (nevermind that the fine details of what the transformation actually is isn&#x27;t nailed down) and it differs between Debug and Release build which is particularly bad for game development as TFA makes clear.
323超过 2 年前
I worked on a CAD&#x2F;CAM product, debug mode was literally unusable, it was 100-1000 times slower than optimized mode.
halayli超过 2 年前
The std::move -&gt; call issue has been fixed in clang-15.
评论 #32984364 未加载
olliej超过 2 年前
The urge to have basic language features not be part of the language, but instead be part of the standard library is responsible for a lot of the less than stellar parts of of C++. Std::move&#x2F;forward are particularly obvious cases, but you get similar accessing members of tuples, etc.<p>Wanting to keep things in the standard library rather than the language itself means that you have to compile large amounts of template hell for a wide array of basic things which hurts build time even in debug modes, and then as this article says you end up with no-op operations that become calls. You also can’t easily debug any of this because you end up with absurd layers of template nonsense for what is again basic functionality.<p>You can compile with -O1, but that then inlines things that aren’t part of the standard library that makes debugging of the actually relevant code annoying (this is what the debug llvm and clang debug builds do), as it inlines your own code and also means that you lose variables all over the place.
bluGill超过 2 年前
I&#x27;m with Kernigham and Pike: the debugger is for getting a stack trace out of core dumps. Printf is for debugging.
评论 #32987119 未加载
评论 #32999794 未加载
评论 #32985597 未加载
chubot超过 2 年前
It&#x27;s also annoying for tools -- like debuggers, and uftrace (user space function tracing), which will start showing all these tiny functions that you don&#x27;t really care about<p>I prefer to avoid all the operator overloading and smart pointers, etc.
StellarScience超过 2 年前
MSVC lacks -Og because it instead treats inlining, optimization, and inclusion of debug information as separate, orthogonal choices.<p>These days I rarely build a whole MSVC project as &quot;Debug&quot; without optimization. Instead I enable both optimization and debug information, so I always get call stacks and line-by-line stepping ability. When I end up debugging some file where the optimizations inhibit debuggability, I&#x27;ll recompile just that compilation unit or library without optimization or inlining.<p>That said, I agree with the gist of the article that both &quot;zero cost abstraction&quot; and debuggability need to be constantly improved.
评论 #32985261 未加载
not2b超过 2 年前
The author needs to learn about the -Og switch, which can be used together with debugging. It can be thought of as &quot;do those optimizations that don&#x27;t interfere with debugging&quot;. I verified with Godbolt that this suffices to eliminate the call overhead.
评论 #32987386 未加载