string_literal_benchmark.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <string>
  6. #include <string_view>
  7. #include "toolchain/diagnostics/null_diagnostics.h"
  8. #include "toolchain/lex/string_literal.h"
  9. namespace Carbon::Lex {
  10. namespace {
  11. static void BM_ValidString(benchmark::State& state, std::string_view introducer,
  12. std::string_view terminator) {
  13. std::string x(introducer);
  14. x.append(100000, 'a');
  15. x.append(terminator);
  16. for (auto _ : state) {
  17. StringLiteral::Lex(x);
  18. }
  19. }
  20. static void BM_ValidString_Simple(benchmark::State& state) {
  21. BM_ValidString(state, "\"", "\"");
  22. }
  23. static void BM_ValidString_Multiline(benchmark::State& state) {
  24. BM_ValidString(state, "'''\n", "\n'''");
  25. }
  26. static void BM_ValidString_MultilineDoubleQuote(benchmark::State& state) {
  27. BM_ValidString(state, "\"\"\"\n", "\n\"\"\"");
  28. }
  29. static void BM_ValidString_Raw(benchmark::State& state) {
  30. BM_ValidString(state, "#\"", "\"#");
  31. }
  32. BENCHMARK(BM_ValidString_Simple);
  33. BENCHMARK(BM_ValidString_Multiline);
  34. BENCHMARK(BM_ValidString_MultilineDoubleQuote);
  35. BENCHMARK(BM_ValidString_Raw);
  36. static void BM_IncompleteWithRepeatedEscapes(benchmark::State& state,
  37. std::string_view introducer,
  38. std::string_view escape) {
  39. std::string x(introducer);
  40. // Aim for about 100k to emphasize escape parsing issues.
  41. while (x.size() < 100000) {
  42. x.append("key: ");
  43. x.append(escape);
  44. x.append("\"");
  45. x.append(escape);
  46. x.append("\"");
  47. x.append(escape);
  48. x.append("n ");
  49. }
  50. for (auto _ : state) {
  51. StringLiteral::Lex(x);
  52. }
  53. }
  54. static void BM_IncompleteWithEscapes_Simple(benchmark::State& state) {
  55. BM_IncompleteWithRepeatedEscapes(state, "\"", "\\");
  56. }
  57. static void BM_IncompleteWithEscapes_Multiline(benchmark::State& state) {
  58. BM_IncompleteWithRepeatedEscapes(state, "'''\n", "\\");
  59. }
  60. static void BM_IncompleteWithEscapes_MultilineDoubleQuote(
  61. benchmark::State& state) {
  62. BM_IncompleteWithRepeatedEscapes(state, "\"\"\"\n", "\\");
  63. }
  64. static void BM_IncompleteWithEscapes_Raw(benchmark::State& state) {
  65. BM_IncompleteWithRepeatedEscapes(state, "#\"", "\\#");
  66. }
  67. BENCHMARK(BM_IncompleteWithEscapes_Simple);
  68. BENCHMARK(BM_IncompleteWithEscapes_Multiline);
  69. BENCHMARK(BM_IncompleteWithEscapes_MultilineDoubleQuote);
  70. BENCHMARK(BM_IncompleteWithEscapes_Raw);
  71. static void BM_SimpleStringValue(benchmark::State& state, int size,
  72. std::string_view introducer, bool add_escape,
  73. std::string_view terminator) {
  74. llvm::BumpPtrAllocator allocator;
  75. std::string x(introducer);
  76. x.append(size, 'a');
  77. if (add_escape) {
  78. // Adds a basic escape that forces ComputeValue to generate a new string.
  79. x.append("\\\\");
  80. }
  81. x.append(terminator);
  82. for (auto _ : state) {
  83. StringLiteral::Lex(x)->ComputeValue(
  84. allocator, Diagnostics::NullEmitter<const char*>());
  85. }
  86. }
  87. static void BM_ComputeValue_NoGenerate_Short(benchmark::State& state) {
  88. BM_SimpleStringValue(state, 10, "\"", /*add_escape=*/false, "\"");
  89. }
  90. static void BM_ComputeValue_NoGenerate_Long(benchmark::State& state) {
  91. BM_SimpleStringValue(state, 10000, "\"", /*add_escape=*/false, "\"");
  92. }
  93. static void BM_ComputeValue_WillGenerate_Short(benchmark::State& state) {
  94. BM_SimpleStringValue(state, 10, "\"", /*add_escape=*/true, "\"");
  95. }
  96. static void BM_ComputeValue_WillGenerate_Long(benchmark::State& state) {
  97. BM_SimpleStringValue(state, 10000, "\"", /*add_escape=*/true, "\"");
  98. }
  99. static void BM_ComputeValue_WillGenerate_Multiline(benchmark::State& state) {
  100. BM_SimpleStringValue(state, 10000, "'''\n", /*add_escape=*/true, "\n'''");
  101. }
  102. static void BM_ComputeValue_WillGenerate_MultilineDoubleQuote(
  103. benchmark::State& state) {
  104. BM_SimpleStringValue(state, 10000, "\"\"\"\n", /*add_escape=*/true,
  105. "\n\"\"\"");
  106. }
  107. static void BM_ComputeValue_WillGenerate_Raw(benchmark::State& state) {
  108. BM_SimpleStringValue(state, 10000, "#\"", /*add_escape=*/true, "\"#");
  109. }
  110. BENCHMARK(BM_ComputeValue_NoGenerate_Short);
  111. BENCHMARK(BM_ComputeValue_NoGenerate_Long);
  112. BENCHMARK(BM_ComputeValue_WillGenerate_Short);
  113. BENCHMARK(BM_ComputeValue_WillGenerate_Long);
  114. BENCHMARK(BM_ComputeValue_WillGenerate_Multiline);
  115. BENCHMARK(BM_ComputeValue_WillGenerate_MultilineDoubleQuote);
  116. BENCHMARK(BM_ComputeValue_WillGenerate_Raw);
  117. } // namespace
  118. } // namespace Carbon::Lex