Skip to content

Should -DNDEBUG be added to all builds? #21

@norbertwenzel

Description

@norbertwenzel

I was testing a small function which contained some assert()s. (Just copied these with the original code.) I was surprised to find the assertion related code in the assembly output. I was always assuming this is removed using -DNDEBUG.

I simply added a failing assertion to the default code:

#include <cassert>

static void StringCreation(benchmark::State& state) {
  // Code inside this loop is measured repeatedly
  for (auto _ : state) {
    std::string created_string("hello");
    assert(created_string.empty());
    // Make sure the variable is not optimized away by compiler
    benchmark::DoNotOptimize(created_string);
  }
}
// Register the function as a benchmark
BENCHMARK(StringCreation);

The run fails (using the default settings, ie. -O3) with the following log:

Error or timeout
{
  "context": {
    "date": "2021-12-07 17:04:21",
    "host_name": "6f36063a71a3",
    "executable": "./bench",
    "num_cpus": 1,
    "mhz_per_cpu": 2400,
    "cpu_scaling_enabled": false,
    "caches": [
      {
        "type": "Data",
        "level": 1,
        "size": 32768,
        "num_sharing": 1
      },
      {
        "type": "Instruction",
        "level": 1,
        "size": 32768,
        "num_sharing": 1
      },
      {
        "type": "Unified",
        "level": 2,
        "size": 262144,
        "num_sharing": 1
      },
      {
        "type": "Unified",
        "level": 3,
        "size": 31457280,
        "num_sharing": 1
      }
    ],
    "load_avg": [0.51,0.54,0.5],
    "library_build_type": "release"
  },
  "benchmarks": [
bench: bench-file.cpp:8: void StringCreation(benchmark::State &): Assertion `created_string.empty()' failed.
Aborted
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.007 MB perf.data (12 samples) ]

So clearly, the assertion is run.

Is that intentional or an oversight? Should -DNDEBUG be added to all builds unconditionally, should this be an additional option to turn on/off or is this the expected behavior?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions