From 15aec8a0c7118936af9eb0346f2b3aa565d24ad8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=BF=97=E6=B6=9B?= Date: Tue, 1 Nov 2022 13:57:19 +0000 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E4=BA=86=E6=97=A0=E5=85=B3?= =?UTF-8?q?=E7=9A=84=E6=80=A7=E8=83=BD=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 刘志涛 --- re2/testing/regexp_benchmark.cc | 323 -------------------------------- 1 file changed, 323 deletions(-) diff --git a/re2/testing/regexp_benchmark.cc b/re2/testing/regexp_benchmark.cc index 23332dd..eb0e36a 100644 --- a/re2/testing/regexp_benchmark.cc +++ b/re2/testing/regexp_benchmark.cc @@ -42,124 +42,11 @@ void MemoryUsage(); typedef testing::MallocCounter MallocCounter; namespace re2 { -/* -void Test() { - Regexp* re = Regexp::Parse("(\\d+)-(\\d+)-(\\d+)", Regexp::LikePerl, NULL); - CHECK(re); - Prog* prog = re->CompileToProg(0); - CHECK(prog); - CHECK(prog->IsOnePass()); - CHECK(prog->CanBitState()); - const char* text = "650-253-0001"; - StringPiece sp[4]; - CHECK(prog->SearchOnePass(text, text, Prog::kAnchored, Prog::kFullMatch, sp, 4)); - CHECK_EQ(sp[0], "650-253-0001"); - CHECK_EQ(sp[1], "650"); - CHECK_EQ(sp[2], "253"); - CHECK_EQ(sp[3], "0001"); - delete prog; - re->Decref(); - LOG(INFO) << "test passed\n"; -} - -void MemoryUsage() { - const char* regexp = "(\\d+)-(\\d+)-(\\d+)"; - const char* text = "650-253-0001"; - { - MallocCounter mc(MallocCounter::THIS_THREAD_ONLY); - Regexp* re = Regexp::Parse(regexp, Regexp::LikePerl, NULL); - CHECK(re); - // Can't pass mc.HeapGrowth() and mc.PeakHeapGrowth() to LOG(INFO) directly, - // because LOG(INFO) might do a big allocation before they get evaluated. - fprintf(stderr, "Regexp: %7lld bytes (peak=%lld)\n", - mc.HeapGrowth(), mc.PeakHeapGrowth()); - mc.Reset(); - - Prog* prog = re->CompileToProg(0); - CHECK(prog); - CHECK(prog->IsOnePass()); - CHECK(prog->CanBitState()); - fprintf(stderr, "Prog: %7lld bytes (peak=%lld)\n", - mc.HeapGrowth(), mc.PeakHeapGrowth()); - mc.Reset(); - - StringPiece sp[4]; - CHECK(prog->SearchOnePass(text, text, Prog::kAnchored, Prog::kFullMatch, sp, 4)); - fprintf(stderr, "Search: %7lld bytes (peak=%lld)\n", - mc.HeapGrowth(), mc.PeakHeapGrowth()); - delete prog; - re->Decref(); - } - - { - MallocCounter mc(MallocCounter::THIS_THREAD_ONLY); - - PCRE re(regexp, PCRE::UTF8); - fprintf(stderr, "RE: %7lld bytes (peak=%lld)\n", - mc.HeapGrowth(), mc.PeakHeapGrowth()); - PCRE::FullMatch(text, re); - fprintf(stderr, "RE: %7lld bytes (peak=%lld)\n", - mc.HeapGrowth(), mc.PeakHeapGrowth()); - } - - { - MallocCounter mc(MallocCounter::THIS_THREAD_ONLY); - - PCRE* re = new PCRE(regexp, PCRE::UTF8); - fprintf(stderr, "PCRE*: %7lld bytes (peak=%lld)\n", - mc.HeapGrowth(), mc.PeakHeapGrowth()); - PCRE::FullMatch(text, *re); - fprintf(stderr, "PCRE*: %7lld bytes (peak=%lld)\n", - mc.HeapGrowth(), mc.PeakHeapGrowth()); - delete re; - } - - { - MallocCounter mc(MallocCounter::THIS_THREAD_ONLY); - - RE2 re(regexp); - fprintf(stderr, "RE2: %7lld bytes (peak=%lld)\n", - mc.HeapGrowth(), mc.PeakHeapGrowth()); - RE2::FullMatch(text, re); - fprintf(stderr, "RE2: %7lld bytes (peak=%lld)\n", - mc.HeapGrowth(), mc.PeakHeapGrowth()); - } - fprintf(stderr, "sizeof: PCRE=%zd RE2=%zd Prog=%zd Inst=%zd\n", - sizeof(PCRE), sizeof(RE2), sizeof(Prog), sizeof(Prog::Inst)); -} -*/ int NumCPUs() { return static_cast(std::thread::hardware_concurrency()); } -// Regular expression implementation wrappers. -// Defined at bottom of file, but they are repetitive -// and not interesting. -/* -typedef void SearchImpl(benchmark::State& state, const char* regexp, - const StringPiece& text, Prog::Anchor anchor, - bool expect_match); - -SearchImpl SearchDFA, SearchNFA, SearchOnePass, SearchBitState, SearchPCRE, - SearchRE2, SearchCachedDFA, SearchCachedNFA, SearchCachedOnePass, - SearchCachedBitState, SearchCachedPCRE, SearchCachedRE2; - -typedef void ParseImpl(benchmark::State& state, const char* regexp, - const StringPiece& text); - -ParseImpl Parse1NFA, Parse1OnePass, Parse1BitState, Parse1PCRE, Parse1RE2, - Parse1Backtrack, Parse1CachedNFA, Parse1CachedOnePass, Parse1CachedBitState, - Parse1CachedPCRE, Parse1CachedRE2, Parse1CachedBacktrack; - -ParseImpl Parse3NFA, Parse3OnePass, Parse3BitState, Parse3PCRE, Parse3RE2, - Parse3Backtrack, Parse3CachedNFA, Parse3CachedOnePass, Parse3CachedBitState, - Parse3CachedPCRE, Parse3CachedRE2, Parse3CachedBacktrack; - -ParseImpl SearchParse2CachedPCRE, SearchParse2CachedRE2; - -ParseImpl SearchParse1CachedPCRE, SearchParse1CachedRE2; -*/ // Benchmark: failed search for regexp in random text. // Generate random text that won't contain the search string, @@ -183,16 +70,6 @@ std::string RandomText(int64_t nbytes) { return text->substr(0, nbytes); } -// Makes text of size nbytes, then calls run to search -// the text for regexp iters times. -/* -void Search(benchmark::State& state, const char* regexp, SearchImpl* search) { - std::string s = RandomText(state.range(0)); - search(state, regexp, s, Prog::kUnanchored, false); - state.SetBytesProcessed(state.iterations() * state.range(0)); -} -*/ - // Benchmark: FindAndConsume void FindAndConsume(benchmark::State& state) { @@ -210,206 +87,6 @@ void FindAndConsume(benchmark::State& state) { // BENCHMARK_RANGE(FindAndConsume, 8, 16)->ThreadRange(1, NumCPUs()); -//////////////////////////////////////////////////////////////////////// -// -// Implementation routines. Sad that there are so many, -// but all the interfaces are slightly different. - -// Runs implementation to search for regexp in text, iters times. -// Expect_match says whether the regexp should be found. -// Anchored says whether to run an anchored search. -/* -void SearchPCRE(benchmark::State& state, const char* regexp, - const StringPiece& text, Prog::Anchor anchor, - bool expect_match) { - for (auto _ : state) { - PCRE re(regexp, PCRE::UTF8); - CHECK_EQ(re.error(), ""); - if (anchor == Prog::kAnchored) - CHECK_EQ(PCRE::FullMatch(text, re), expect_match); - else - CHECK_EQ(PCRE::PartialMatch(text, re), expect_match); - } -} - -void SearchRE2(benchmark::State& state, const char* regexp, - const StringPiece& text, Prog::Anchor anchor, - bool expect_match) { - for (auto _ : state) { - RE2 re(regexp); - CHECK_EQ(re.error(), ""); - if (anchor == Prog::kAnchored) - CHECK_EQ(RE2::FullMatch(text, re), expect_match); - else - CHECK_EQ(RE2::PartialMatch(text, re), expect_match); - } -} - -PCRE* GetCachedPCRE(const char* regexp) { - static auto& mutex = *new Mutex; - MutexLock lock(&mutex); - static auto& cache = *new std::unordered_map; - PCRE* re = cache[regexp]; - if (re == NULL) { - re = new PCRE(regexp, PCRE::UTF8); - CHECK_EQ(re->error(), ""); - cache[regexp] = re; - } - return re; -} - -RE2* GetCachedRE2(const char* regexp) { - static auto& mutex = *new Mutex; - MutexLock lock(&mutex); - static auto& cache = *new std::unordered_map; - RE2* re = cache[regexp]; - if (re == NULL) { - re = new RE2(regexp); - CHECK_EQ(re->error(), ""); - cache[regexp] = re; - } - return re; -} - -void SearchCachedPCRE(benchmark::State& state, const char* regexp, - const StringPiece& text, Prog::Anchor anchor, - bool expect_match) { - PCRE& re = *GetCachedPCRE(regexp); - for (auto _ : state) { - if (anchor == Prog::kAnchored) - CHECK_EQ(PCRE::FullMatch(text, re), expect_match); - else - CHECK_EQ(PCRE::PartialMatch(text, re), expect_match); - } -} - -void SearchCachedRE2(benchmark::State& state, const char* regexp, - const StringPiece& text, Prog::Anchor anchor, - bool expect_match) { - RE2& re = *GetCachedRE2(regexp); - for (auto _ : state) { - if (anchor == Prog::kAnchored) - CHECK_EQ(RE2::FullMatch(text, re), expect_match); - else - CHECK_EQ(RE2::PartialMatch(text, re), expect_match); - } -} - - -void Parse3PCRE(benchmark::State& state, const char* regexp, - const StringPiece& text) { - for (auto _ : state) { - PCRE re(regexp, PCRE::UTF8); - CHECK_EQ(re.error(), ""); - StringPiece sp1, sp2, sp3; - CHECK(PCRE::FullMatch(text, re, &sp1, &sp2, &sp3)); - } -} - -void Parse3RE2(benchmark::State& state, const char* regexp, - const StringPiece& text) { - for (auto _ : state) { - RE2 re(regexp); - CHECK_EQ(re.error(), ""); - StringPiece sp1, sp2, sp3; - CHECK(RE2::FullMatch(text, re, &sp1, &sp2, &sp3)); - } -} - -void Parse3CachedPCRE(benchmark::State& state, const char* regexp, - const StringPiece& text) { - PCRE& re = *GetCachedPCRE(regexp); - StringPiece sp1, sp2, sp3; - for (auto _ : state) { - CHECK(PCRE::FullMatch(text, re, &sp1, &sp2, &sp3)); - } -} - -void Parse3CachedRE2(benchmark::State& state, const char* regexp, - const StringPiece& text) { - RE2& re = *GetCachedRE2(regexp); - StringPiece sp1, sp2, sp3; - for (auto _ : state) { - CHECK(RE2::FullMatch(text, re, &sp1, &sp2, &sp3)); - } -} - - -void Parse1PCRE(benchmark::State& state, const char* regexp, - const StringPiece& text) { - for (auto _ : state) { - PCRE re(regexp, PCRE::UTF8); - CHECK_EQ(re.error(), ""); - StringPiece sp1; - CHECK(PCRE::FullMatch(text, re, &sp1)); - } -} - -void Parse1RE2(benchmark::State& state, const char* regexp, - const StringPiece& text) { - for (auto _ : state) { - RE2 re(regexp); - CHECK_EQ(re.error(), ""); - StringPiece sp1; - CHECK(RE2::FullMatch(text, re, &sp1)); - } -} - -void Parse1CachedPCRE(benchmark::State& state, const char* regexp, - const StringPiece& text) { - PCRE& re = *GetCachedPCRE(regexp); - StringPiece sp1; - for (auto _ : state) { - CHECK(PCRE::FullMatch(text, re, &sp1)); - } -} - -void Parse1CachedRE2(benchmark::State& state, const char* regexp, - const StringPiece& text) { - RE2& re = *GetCachedRE2(regexp); - StringPiece sp1; - for (auto _ : state) { - CHECK(RE2::FullMatch(text, re, &sp1)); - } -} - -void SearchParse2CachedPCRE(benchmark::State& state, const char* regexp, - const StringPiece& text) { - PCRE& re = *GetCachedPCRE(regexp); - for (auto _ : state) { - StringPiece sp1, sp2; - CHECK(PCRE::PartialMatch(text, re, &sp1, &sp2)); - } -} - -void SearchParse2CachedRE2(benchmark::State& state, const char* regexp, - const StringPiece& text) { - RE2& re = *GetCachedRE2(regexp); - for (auto _ : state) { - StringPiece sp1, sp2; - CHECK(RE2::PartialMatch(text, re, &sp1, &sp2)); - } -} - -void SearchParse1CachedPCRE(benchmark::State& state, const char* regexp, - const StringPiece& text) { - PCRE& re = *GetCachedPCRE(regexp); - for (auto _ : state) { - StringPiece sp1; - CHECK(PCRE::PartialMatch(text, re, &sp1)); - } -} - -void SearchParse1CachedRE2(benchmark::State& state, const char* regexp, - const StringPiece& text) { - RE2& re = *GetCachedRE2(regexp); - for (auto _ : state) { - StringPiece sp1; - CHECK(RE2::PartialMatch(text, re, &sp1)); - } -} -*/ - void EmptyPartialMatchPCRE(benchmark::State& state) { PCRE re(""); for (auto _ : state) { -- Gitee