string_literal_benchmark.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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::Lex {
  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. StringLiteral::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. StringLiteral::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, int size,
  70. std::string_view introducer, bool add_escape,
  71. std::string_view terminator) {
  72. llvm::BumpPtrAllocator allocator;
  73. std::string x(introducer);
  74. x.append(size, 'a');
  75. if (add_escape) {
  76. // Adds a basic escape that forces ComputeValue to generate a new string.
  77. x.append("\\\\");
  78. }
  79. x.append(terminator);
  80. for (auto _ : state) {
  81. StringLiteral::Lex(x)->ComputeValue(allocator,
  82. NullDiagnosticEmitter<const char*>());
  83. }
  84. }
  85. static void BM_ComputeValue_NoGenerate_Short(benchmark::State& state) {
  86. BM_SimpleStringValue(state, 10, "\"", /*add_escape=*/false, "\"");
  87. }
  88. static void BM_ComputeValue_NoGenerate_Long(benchmark::State& state) {
  89. BM_SimpleStringValue(state, 10000, "\"", /*add_escape=*/false, "\"");
  90. }
  91. static void BM_ComputeValue_WillGenerate_Short(benchmark::State& state) {
  92. BM_SimpleStringValue(state, 10, "\"", /*add_escape=*/true, "\"");
  93. }
  94. static void BM_ComputeValue_WillGenerate_Long(benchmark::State& state) {
  95. BM_SimpleStringValue(state, 10000, "\"", /*add_escape=*/true, "\"");
  96. }
  97. static void BM_ComputeValue_WillGenerate_Multiline(benchmark::State& state) {
  98. BM_SimpleStringValue(state, 10000, "'''\n", /*add_escape=*/true, "\n'''");
  99. }
  100. static void BM_ComputeValue_WillGenerate_MultilineDoubleQuote(
  101. benchmark::State& state) {
  102. BM_SimpleStringValue(state, 10000, "\"\"\"\n", /*add_escape=*/true,
  103. "\n\"\"\"");
  104. }
  105. static void BM_ComputeValue_WillGenerate_Raw(benchmark::State& state) {
  106. BM_SimpleStringValue(state, 10000, "#\"", /*add_escape=*/true, "\"#");
  107. }
  108. BENCHMARK(BM_ComputeValue_NoGenerate_Short);
  109. BENCHMARK(BM_ComputeValue_NoGenerate_Long);
  110. BENCHMARK(BM_ComputeValue_WillGenerate_Short);
  111. BENCHMARK(BM_ComputeValue_WillGenerate_Long);
  112. BENCHMARK(BM_ComputeValue_WillGenerate_Multiline);
  113. BENCHMARK(BM_ComputeValue_WillGenerate_MultilineDoubleQuote);
  114. BENCHMARK(BM_ComputeValue_WillGenerate_Raw);
  115. } // namespace
  116. } // namespace Carbon::Lex