Просмотр исходного кода

Switch std::count CHECK to DCHECK, adding DCHECK (#1107)

Jon Meow 4 лет назад
Родитель
Сommit
a9a726bc3e

+ 7 - 0
common/check.h

@@ -28,6 +28,13 @@ namespace Carbon {
                     << ": " #condition                                    \
                     << Carbon::Internal::ExitingStream::AddSeparator()
 
+// DCHECK calls CHECK in debug mode, and does nothing otherwise.
+#ifndef NDEBUG
+#define DCHECK(condition) CHECK(condition)
+#else
+#define DCHECK(condition) CHECK(true || (condition))
+#endif
+
 // This is similar to CHECK, but is unconditional. Writing FATAL() is clearer
 // than CHECK(false) because it avoids confusion about control flow.
 //

+ 12 - 3
toolchain/lexer/BUILD

@@ -58,6 +58,18 @@ cc_library(
     ],
 )
 
+cc_binary(
+    name = "numeric_literal_benchmark",
+    testonly = 1,
+    srcs = ["numeric_literal_benchmark.cpp"],
+    deps = [
+        ":numeric_literal",
+        "//common:check",
+        "//toolchain/diagnostics:null_diagnostics",
+        "@com_github_google_benchmark//:benchmark_main",
+    ],
+)
+
 cc_test(
     name = "numeric_literal_test",
     size = "small",
@@ -104,10 +116,7 @@ cc_binary(
     srcs = ["string_literal_benchmark.cpp"],
     deps = [
         ":string_literal",
-        "//common:ostream",
-        "//toolchain/diagnostics:diagnostic_emitter",
         "@com_github_google_benchmark//:benchmark_main",
-        "@llvm-project//llvm:Support",
     ],
 )
 

+ 1 - 1
toolchain/lexer/numeric_literal.cpp

@@ -403,7 +403,7 @@ auto LexedNumericLiteral::Parser::CheckDigitSequence(
 // correctly positioned.
 auto LexedNumericLiteral::Parser::CheckDigitSeparatorPlacement(
     llvm::StringRef text, int radix, int num_digit_separators) -> void {
-  CHECK(std::count(text.begin(), text.end(), '_') == num_digit_separators)
+  DCHECK(std::count(text.begin(), text.end(), '_') == num_digit_separators)
       << "given wrong number of digit separators: " << num_digit_separators;
 
   if (radix == 2) {

+ 50 - 0
toolchain/lexer/numeric_literal_benchmark.cpp

@@ -0,0 +1,50 @@
+// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
+// Exceptions. See /LICENSE for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+#include <benchmark/benchmark.h>
+
+#include "common/check.h"
+#include "toolchain/diagnostics/null_diagnostics.h"
+#include "toolchain/lexer/numeric_literal.h"
+
+namespace Carbon::Testing {
+namespace {
+
+static void BM_Lex_Float(benchmark::State& state) {
+  for (auto _ : state) {
+    CHECK(LexedNumericLiteral::Lex("0.000001"));
+  }
+}
+
+static void BM_Lex_Integer(benchmark::State& state) {
+  for (auto _ : state) {
+    CHECK(LexedNumericLiteral::Lex("1_234_567_890"));
+  }
+}
+
+static void BM_ComputeValue_Float(benchmark::State& state) {
+  auto val = LexedNumericLiteral::Lex("0.000001");
+  CHECK(val);
+  auto emitter = NullDiagnosticEmitter<const char*>();
+  for (auto _ : state) {
+    val->ComputeValue(emitter);
+  }
+}
+
+static void BM_ComputeValue_Integer(benchmark::State& state) {
+  auto val = LexedNumericLiteral::Lex("1_234_567_890");
+  auto emitter = NullDiagnosticEmitter<const char*>();
+  CHECK(val);
+  for (auto _ : state) {
+    val->ComputeValue(emitter);
+  }
+}
+
+BENCHMARK(BM_Lex_Float);
+BENCHMARK(BM_Lex_Integer);
+BENCHMARK(BM_ComputeValue_Float);
+BENCHMARK(BM_ComputeValue_Integer);
+
+}  // namespace
+}  // namespace Carbon::Testing