Hi Nico,If you have a moment, Nektarios and I were debating which of these is better?Version 1:#if DCHECK_IS_ON()bool IsValid() {...}#endifvoid MyClass::OnDataChanged() {#if DCHECK_IS_ON()DCHECK(IsValid());#endif}Version 2:Same as #1 but remove #if DCHECK_IS_ON().My concern with this one is whether our linkers on all platforms will be smart enough to not include IsValid().Version 3:Same as #1 but remove only the last #if DCHECK_IS_ON(). Keep the first one around IsValid().I know this one isn't right, because i've tried it and it failed to compile on some platforms, which really surprised me? Why should I need #if DCHECK_IS_ON() around a DCHECK() ?Thanks for any advice,Aaron
+cxx3 seems right to me. You want the first check so that you don't get an "unused function" warning. You shouldn't need to surround the DCHECK though. Do you have a link to a bot where that didn't work?
On Wed, Jun 30, 2021, 11:17 AM Aaron Leventhal <aleve...@google.com> wrote:Hi Nico,If you have a moment, Nektarios and I were debating which of these is better?Version 1:#if DCHECK_IS_ON()bool IsValid() {...}#endifvoid MyClass::OnDataChanged() {#if DCHECK_IS_ON()DCHECK(IsValid());#endif}Version 2:Same as #1 but remove #if DCHECK_IS_ON().My concern with this one is whether our linkers on all platforms will be smart enough to not include IsValid().Version 3:Same as #1 but remove only the last #if DCHECK_IS_ON(). Keep the first one around IsValid().I know this one isn't right, because i've tried it and it failed to compile on some platforms, which really surprised me? Why should I need #if DCHECK_IS_ON() around a DCHECK() ?Thanks for any advice,Aaron
--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAMGbLiG5fVnmnhv0FZA8h8mZcdRT_jvGKdrDr7-1w2N5k-QM2g%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CAHtyhaRqfZg_VqiHsV%2BvSgN7MaAeXXZpe88sV14DjV6TZouR0Q%40mail.gmail.com.
Fwiw, once we had a regression in Oilpan that was caused by not wrappingDCHECK_EQ(some_value, FreeListSize())in #if DCHECK_IS_ON().Even though FreeListSize() was fully inlinable and side-effect-free, the compiler decided to not drop it. What made this bug insidious is that on non-official release builds the compiler managed to optimize the call away. The problem was only reproducible on official release builds (wrong AFDO feedback?).
Maybe we should use some metaprogramming to avoid calling the function in this case, since "unused function" isn't a warning like "unused variable".
On Wed, Jun 30, 2021 at 12:09 PM Anton Bikineev <biki...@google.com> wrote:Fwiw, once we had a regression in Oilpan that was caused by not wrapping
Maybe we should use some metaprogramming to avoid calling the function in this case, since "unused function" isn't a warning like "unused variable".What we did in cppgc (new Oilpan) is we put the DCHECK expression in unevaluated context (as a decltype operand):Maybe something like this could also work for base/logging.h.
On Wed, Jun 30, 2021 at 12:21 PM Anton Bikineev <biki...@google.com> wrote:Maybe we should use some metaprogramming to avoid calling the function in this case, since "unused function" isn't a warning like "unused variable".What we did in cppgc (new Oilpan) is we put the DCHECK expression in unevaluated context (as a decltype operand):Maybe something like this could also work for base/logging.h.That looks like a great idea, something we couldn't do when this code was first written, but could now.
I guess that cppgc DCHECKs are never written with lambdas inside them because of this decision.
So, it sounds like number 1 should be the preferred way to write this because the compiler might not optimize the function away in all cases?Nektarios.
The example given where it was not caught included code written outside the DCHECK. So, AFAICT option 2 should be fine. If anyone can show that the linker is not removing unused functions then that'd be good to fix, but sounds unrelated to DCHECKs.
The example given where it was not caught included code written outside the DCHECK. So, AFAICT option 2 should be fine. If anyone can show that the linker is not removing unused functions then that'd be good to fix, but sounds unrelated to DCHECKs.Agreed. Just in case, I tested the binary size with a call to 1MB function inside a DCHECK and there was no difference: https://chromium-review.googlesource.com/c/chromium/src/+/2998706So version 2 should be fine. However, if you have some code around a DCHECK, likevoid MyClass::OnDataChanged(Data data) {const int checksum = ComputeCheckSum(data);DCHECK_EQ(cached_checksum_, checksum);}it's safer to wrap it in #if DCHECK_IS_ON(), as the compiler is not guaranteed to dead-code-eliminate it.