string_literal_benchmark.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include <benchmark/benchmark.h>
  5. #include "toolchain/diagnostics/null_diagnostics.h"
  6. #include "toolchain/lexer/string_literal.h"
  7. namespace Carbon::Testing {
  8. namespace {
  9. static void BM_ValidString(benchmark::State& state, std::string_view introducer,
  10. std::string_view terminator) {
  11. std::string x(introducer);
  12. x.append(100000, 'a');
  13. x.append(terminator);
  14. for (auto _ : state) {
  15. LexedStringLiteral::Lex(x);
  16. }
  17. }
  18. static void BM_ValidString_Simple(benchmark::State& state) {
  19. BM_ValidString(state, "\"", "\"");
  20. }
  21. static void BM_ValidString_Multiline(benchmark::State& state) {
  22. BM_ValidString(state, "'''\n", "\n'''");
  23. }
  24. static void BM_ValidString_MultilineDoubleQuote(benchmark::State& state) {
  25. BM_ValidString(state, "\"\"\"\n", "\n\"\"\"");
  26. }
  27. static void BM_ValidString_Raw(benchmark::State& state) {
  28. BM_ValidString(state, "#\"", "\"#");
  29. }
  30. BENCHMARK(BM_ValidString_Simple);
  31. BENCHMARK(BM_ValidString_Multiline);
  32. BENCHMARK(BM_ValidString_MultilineDoubleQuote);
  33. BENCHMARK(BM_ValidString_Raw);
  34. static void BM_IncompleteWithRepeatedEscapes(benchmark::State& state,
  35. std::string_view introducer,
  36. std::string_view escape) {
  37. std::string x(introducer);
  38. // Aim for about 100k to emphasize escape parsing issues.
  39. while (x.size() < 100000) {
  40. x.append("key: ");
  41. x.append(escape);
  42. x.append("\"");
  43. x.append(escape);
  44. x.append("\"");
  45. x.append(escape);
  46. x.append("n ");
  47. }
  48. for (auto _ : state) {
  49. LexedStringLiteral::Lex(x);
  50. }
  51. }
  52. static void BM_IncompleteWithEscapes_Simple(benchmark::State& state) {
  53. BM_IncompleteWithRepeatedEscapes(state, "\"", "\\");
  54. }
  55. static void BM_IncompleteWithEscapes_Multiline(benchmark::State& state) {
  56. BM_IncompleteWithRepeatedEscapes(state, "'''\n", "\\");
  57. }
  58. static void BM_IncompleteWithEscapes_MultilineDoubleQuote(
  59. benchmark::State& state) {
  60. BM_IncompleteWithRepeatedEscapes(state, "\"\"\"\n", "\\");
  61. }
  62. static void BM_IncompleteWithEscapes_Raw(benchmark::State& state) {
  63. BM_IncompleteWithRepeatedEscapes(state, "#\"", "\\#");
  64. }
  65. BENCHMARK(BM_IncompleteWithEscapes_Simple);
  66. BENCHMARK(BM_IncompleteWithEscapes_Multiline);
  67. BENCHMARK(BM_IncompleteWithEscapes_MultilineDoubleQuote);
  68. BENCHMARK(BM_IncompleteWithEscapes_Raw);
  69. static void BM_SimpleStringValue(benchmark::State& state,
  70. std::string_view introducer,
  71. std::string_view terminator) {
  72. std::string x(introducer);
  73. x.append(100000, 'a');
  74. x.append(terminator);
  75. for (auto _ : state) {
  76. LexedStringLiteral::Lex(x)->ComputeValue(
  77. NullDiagnosticEmitter<const char*>());
  78. }
  79. }
  80. static void BM_SimpleStringValue_Simple(benchmark::State& state) {
  81. BM_SimpleStringValue(state, "\"", "\"");
  82. }
  83. static void BM_SimpleStringValue_Multiline(benchmark::State& state) {
  84. BM_SimpleStringValue(state, "'''\n", "\n'''");
  85. }
  86. static void BM_SimpleStringValue_MultilineDoubleQuote(benchmark::State& state) {
  87. BM_SimpleStringValue(state, "\"\"\"\n", "\n\"\"\"");
  88. }
  89. static void BM_SimpleStringValue_Raw(benchmark::State& state) {
  90. BM_SimpleStringValue(state, "#\"", "\"#");
  91. }
  92. BENCHMARK(BM_SimpleStringValue_Simple);
  93. BENCHMARK(BM_SimpleStringValue_Multiline);
  94. BENCHMARK(BM_SimpleStringValue_MultilineDoubleQuote);
  95. BENCHMARK(BM_SimpleStringValue_Raw);
  96. } // namespace
  97. } // namespace Carbon::Testing