string_literal_benchmark.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/lex/string_literal.h"
  7. namespace Carbon::Testing {
  8. namespace {
  9. using Lex::StringLiteral;
  10. static void BM_ValidString(benchmark::State& state, std::string_view introducer,
  11. std::string_view terminator) {
  12. std::string x(introducer);
  13. x.append(100000, 'a');
  14. x.append(terminator);
  15. for (auto _ : state) {
  16. StringLiteral::Lex(x);
  17. }
  18. }
  19. static void BM_ValidString_Simple(benchmark::State& state) {
  20. BM_ValidString(state, "\"", "\"");
  21. }
  22. static void BM_ValidString_Multiline(benchmark::State& state) {
  23. BM_ValidString(state, "'''\n", "\n'''");
  24. }
  25. static void BM_ValidString_MultilineDoubleQuote(benchmark::State& state) {
  26. BM_ValidString(state, "\"\"\"\n", "\n\"\"\"");
  27. }
  28. static void BM_ValidString_Raw(benchmark::State& state) {
  29. BM_ValidString(state, "#\"", "\"#");
  30. }
  31. BENCHMARK(BM_ValidString_Simple);
  32. BENCHMARK(BM_ValidString_Multiline);
  33. BENCHMARK(BM_ValidString_MultilineDoubleQuote);
  34. BENCHMARK(BM_ValidString_Raw);
  35. static void BM_IncompleteWithRepeatedEscapes(benchmark::State& state,
  36. std::string_view introducer,
  37. std::string_view escape) {
  38. std::string x(introducer);
  39. // Aim for about 100k to emphasize escape parsing issues.
  40. while (x.size() < 100000) {
  41. x.append("key: ");
  42. x.append(escape);
  43. x.append("\"");
  44. x.append(escape);
  45. x.append("\"");
  46. x.append(escape);
  47. x.append("n ");
  48. }
  49. for (auto _ : state) {
  50. StringLiteral::Lex(x);
  51. }
  52. }
  53. static void BM_IncompleteWithEscapes_Simple(benchmark::State& state) {
  54. BM_IncompleteWithRepeatedEscapes(state, "\"", "\\");
  55. }
  56. static void BM_IncompleteWithEscapes_Multiline(benchmark::State& state) {
  57. BM_IncompleteWithRepeatedEscapes(state, "'''\n", "\\");
  58. }
  59. static void BM_IncompleteWithEscapes_MultilineDoubleQuote(
  60. benchmark::State& state) {
  61. BM_IncompleteWithRepeatedEscapes(state, "\"\"\"\n", "\\");
  62. }
  63. static void BM_IncompleteWithEscapes_Raw(benchmark::State& state) {
  64. BM_IncompleteWithRepeatedEscapes(state, "#\"", "\\#");
  65. }
  66. BENCHMARK(BM_IncompleteWithEscapes_Simple);
  67. BENCHMARK(BM_IncompleteWithEscapes_Multiline);
  68. BENCHMARK(BM_IncompleteWithEscapes_MultilineDoubleQuote);
  69. BENCHMARK(BM_IncompleteWithEscapes_Raw);
  70. static void BM_SimpleStringValue(benchmark::State& state,
  71. std::string_view introducer,
  72. std::string_view terminator) {
  73. std::string x(introducer);
  74. x.append(100000, 'a');
  75. x.append(terminator);
  76. for (auto _ : state) {
  77. StringLiteral::Lex(x)->ComputeValue(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