RegexStats.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "ParserPch.h"
  6. #if ENABLE_REGEX_CONFIG_OPTIONS
  7. namespace UnifiedRegex
  8. {
  9. const wchar_t* RegexStats::PhaseNames[RegexStats::NumPhases] = { L"parse", L"compile", L"execute" };
  10. const wchar_t* RegexStats::UseNames[RegexStats::NumUses] = { L"match", L"exec", L"test", L"replace", L"split", L"search" };
  11. RegexStats::RegexStats(RegexPattern* pattern)
  12. : pattern(pattern)
  13. , inputLength(0)
  14. , numCompares(0)
  15. , numPushes(0)
  16. , numPops(0)
  17. , stackHWM(0)
  18. , numInsts(0)
  19. {
  20. for (int i = 0; i < NumPhases; i++)
  21. phaseTicks[i] = 0;
  22. for (int i = 0; i < NumUses; i++)
  23. useCounts[i] = 0;
  24. }
  25. void RegexStats::Print(DebugWriter* w, RegexStats* totals, Ticks ticksPerMillisecond)
  26. {
  27. if (pattern == 0)
  28. w->PrintEOL(L"TOTAL");
  29. else
  30. pattern->Print(w);
  31. w->EOL();
  32. w->Indent();
  33. for (int i = 0; i < NumPhases; i++)
  34. {
  35. double ms = (double)phaseTicks[i] / (double)ticksPerMillisecond;
  36. if (totals == 0 || totals->phaseTicks[i] == 0)
  37. w->PrintEOL(L"%-12s: %10.4fms", PhaseNames[i], ms);
  38. else
  39. {
  40. double pc = (double)phaseTicks[i] * 100.0 / (double)totals->phaseTicks[i];
  41. w->PrintEOL(L"%-12s: %10.4fms (%10.4f%%)", PhaseNames[i], ms, pc);
  42. }
  43. }
  44. for (int i = 0; i < NumUses; i++)
  45. {
  46. if (useCounts[i] > 0)
  47. {
  48. if (totals == 0 || totals->useCounts[i] == 0)
  49. w->PrintEOL(L"#%-11s: %10I64u", UseNames[i], useCounts[i]);
  50. else
  51. {
  52. double pc = (double)useCounts[i] * 100.0 / (double)totals->useCounts[i];
  53. w->PrintEOL(L"#%-11s: %10I64u (%10.4f%%)", UseNames[i], useCounts[i], pc);
  54. }
  55. }
  56. }
  57. if (inputLength > 0)
  58. {
  59. double r = (double)numCompares * 100.0 / (double)inputLength;
  60. if (totals == 0 || totals->numCompares == 0)
  61. w->PrintEOL(L"numCompares : %10.4f%%", r);
  62. else
  63. {
  64. double pc = (double)numCompares * 100.0 / (double)totals->numCompares;
  65. w->PrintEOL(L"numCompares : %10.4f%% (%10.4f%%)", r, pc);
  66. }
  67. }
  68. if (totals == 0 || totals->inputLength == 0)
  69. w->PrintEOL(L"inputLength : %10I64u", inputLength);
  70. else
  71. {
  72. double pc = (double)inputLength * 100.0 / (double)totals->inputLength;
  73. w->PrintEOL(L"inputLength : %10I64u (%10.4f%%)", inputLength, pc);
  74. }
  75. if (totals == 0 || totals->numPushes == 0)
  76. w->PrintEOL(L"numPushes : %10I64u", numPushes);
  77. else
  78. {
  79. double pc = (double)numPushes * 100.0 / (double)totals->numPushes;
  80. w->PrintEOL(L"numPushes : %10I64u (%10.4f%%)", numPushes, pc);
  81. }
  82. if (totals == 0 || totals->numPops == 0)
  83. w->PrintEOL(L"numPops : %10I64u", numPops);
  84. else
  85. {
  86. double pc = (double)numPops * 100.0 / (double)totals->numPops;
  87. w->PrintEOL(L"numPops : %10I64u (%10.4f%%)", numPops, pc);
  88. }
  89. if (totals == 0 || totals->stackHWM == 0)
  90. w->PrintEOL(L"stackHWM : %10I64u", stackHWM);
  91. else
  92. {
  93. double pc = (double)stackHWM * 100.0 / (double)totals->stackHWM;
  94. w->PrintEOL(L"stackHWM : %10I64u (%10.4f%%)", stackHWM, pc);
  95. }
  96. if (totals == 0 || totals->numInsts == 0)
  97. w->PrintEOL(L"numInsts : %10I64u", numInsts);
  98. else
  99. {
  100. double pc = (double)numInsts * 100.0 / (double)totals->numInsts;
  101. w->PrintEOL(L"numInsts : %10I64u (%10.4f%%)", numInsts, pc);
  102. }
  103. w->Unindent();
  104. }
  105. void RegexStats::Add(RegexStats* other)
  106. {
  107. for (int i = 0; i < NumPhases; i++)
  108. phaseTicks[i] += other->phaseTicks[i];
  109. for (int i = 0; i < NumUses; i++)
  110. useCounts[i] += other->useCounts[i];
  111. inputLength += other->inputLength;
  112. numCompares += other->numCompares;
  113. numPushes += other->numPushes;
  114. numPops += other->numPops;
  115. if (other->stackHWM > stackHWM)
  116. stackHWM = other->stackHWM;
  117. numInsts += other->numInsts;
  118. }
  119. RegexStats::Ticks RegexStatsDatabase::Now()
  120. {
  121. LARGE_INTEGER tmp;
  122. if (QueryPerformanceCounter(&tmp))
  123. return tmp.QuadPart;
  124. else
  125. {
  126. Assert(false);
  127. return 0;
  128. }
  129. }
  130. RegexStats::Ticks RegexStatsDatabase::Freq()
  131. {
  132. LARGE_INTEGER tmp;
  133. if (QueryPerformanceFrequency(&tmp))
  134. {
  135. return tmp.QuadPart / 1000;
  136. }
  137. else
  138. {
  139. Assert(false);
  140. return 1;
  141. }
  142. }
  143. RegexStatsDatabase::RegexStatsDatabase(ArenaAllocator* allocator)
  144. : start(0), allocator(allocator)
  145. {
  146. ticksPerMillisecond = Freq();
  147. map = Anew(allocator, RegexStatsMap, allocator, 17);
  148. }
  149. RegexStats* RegexStatsDatabase::GetRegexStats(RegexPattern* pattern)
  150. {
  151. Js::InternalString str = pattern->GetSource();
  152. RegexStats *res;
  153. if (!map->TryGetValue(str, &res))
  154. {
  155. res = Anew(allocator, RegexStats, pattern);
  156. map->Add(str, res);
  157. }
  158. return res;
  159. }
  160. void RegexStatsDatabase::BeginProfile()
  161. {
  162. start = Now();
  163. }
  164. void RegexStatsDatabase::EndProfile(RegexStats* stats, RegexStats::Phase phase)
  165. {
  166. stats->phaseTicks[phase] += Now() - start;
  167. }
  168. void RegexStatsDatabase::Print(DebugWriter* w)
  169. {
  170. RegexStats totals(0);
  171. Output::Print(L"Regular Expression Statistics\n");
  172. Output::Print(L"=============================\n");
  173. for (int i = 0; i < map->Count(); i++)
  174. totals.Add(map->GetValueAt(i));
  175. for (int i = 0; i < map->Count(); i++)
  176. map->GetValueAt(i)->Print(w, &totals, ticksPerMillisecond);
  177. totals.Print(w, 0, ticksPerMillisecond);
  178. allocator->Free(w, sizeof(DebugWriter));
  179. }
  180. }
  181. #endif