From 7e4a10160e250f5880016c92f35edbef08a78ed6 Mon Sep 17 00:00:00 2001 From: Raimund Merkert Date: Wed, 28 Sep 2022 18:29:34 -0400 Subject: [PATCH] Use regex cache in the synthesized C++ code Update the synthesizer to use a cache for regexes. One drawback is that we now need to #include , but the structure of the synthesizer makes impossible to generate the cache only when needed. --- src/include/souffle/CompiledSouffle.h | 1 + src/include/souffle/SouffleInterface.h | 2 ++ src/synthesiser/Synthesiser.cpp | 10 ++++++++-- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/include/souffle/CompiledSouffle.h b/src/include/souffle/CompiledSouffle.h index e13579b81e0..9183e9802e1 100644 --- a/src/include/souffle/CompiledSouffle.h +++ b/src/include/souffle/CompiledSouffle.h @@ -23,6 +23,7 @@ #include "souffle/SymbolTable.h" #include "souffle/datastructure/BTreeDelete.h" #include "souffle/datastructure/Brie.h" +#include "souffle/datastructure/ConcurrentCache.h" #include "souffle/datastructure/EqRel.h" #include "souffle/datastructure/Info.h" #include "souffle/datastructure/Nullaries.h" diff --git a/src/include/souffle/SouffleInterface.h b/src/include/souffle/SouffleInterface.h index 2087b38456f..be9a3f582a7 100644 --- a/src/include/souffle/SouffleInterface.h +++ b/src/include/souffle/SouffleInterface.h @@ -19,6 +19,7 @@ #include "souffle/RamTypes.h" #include "souffle/RecordTable.h" #include "souffle/SymbolTable.h" +#include "souffle/datastructure/ConcurrentCache.h" #include "souffle/utility/MiscUtil.h" #include #include @@ -29,6 +30,7 @@ #include #include #include +#include #include #include #include diff --git a/src/synthesiser/Synthesiser.cpp b/src/synthesiser/Synthesiser.cpp index 6865fd5ddd5..07379fd02a3 100644 --- a/src/synthesiser/Synthesiser.cpp +++ b/src/synthesiser/Synthesiser.cpp @@ -2607,6 +2607,7 @@ void Synthesiser::generateCode(GenDb& db, const std::string& id, bool& withShare std::vector> args; args.push_back(std::make_tuple(Reference, "symTable", "SymbolTable")); args.push_back(std::make_tuple(Reference, "recordTable", "RecordTable")); + args.push_back(std::make_tuple(Reference, "regexCache", "ConcurrentCache")); args.push_back(std::make_tuple(Reference, "pruneImdtRels", "bool")); args.push_back(std::make_tuple(Reference, "performIO", "bool")); args.push_back(std::make_tuple(Reference, "signalHandler", "SignalHandler*")); @@ -2665,13 +2666,14 @@ void Synthesiser::generateCode(GenDb& db, const std::string& id, bool& withShare if (SubroutineUsingStdRegex) { // regex wrapper GenFunction& wrapper = gen.addFunction("regex_wrapper", Visibility::Private); - gen.addInclude(""); wrapper.setRetType("inline bool"); wrapper.setNextArg("const std::string&", "pattern"); wrapper.setNextArg("const std::string&", "text"); wrapper.body() << " bool result = false; \n" - << " try { result = std::regex_match(text, std::regex(pattern)); } catch(...) { \n" + << " try { result = std::regex_match(text, regexCache.getOrCreate(pattern)); } " + "catch(...) { " + "\n" << " std::cerr << \"warning: wrong pattern provided for match(\\\"\" << pattern << " "\"\\\",\\\"\" " "<< text << \"\\\").\\n\";\n}\n" @@ -2728,6 +2730,9 @@ void Synthesiser::generateCode(GenDb& db, const std::string& id, bool& withShare mainClass.addField(rt.str(), "recordTable", Visibility::Private); constructor.setNextInitializer("recordTable", ""); + mainClass.addField("ConcurrentCache", "regexCache", Visibility::Private); + constructor.setNextInitializer("regexCache", ""); + if (Global::config().has("profile")) { std::size_t numFreq = 0; visit(prog, [&](const Statement&) { numFreq++; }); @@ -3024,6 +3029,7 @@ void Synthesiser::generateCode(GenDb& db, const std::string& id, bool& withShare setNumThreads.body() << "SouffleProgram::setNumThreads(numThreadsValue);\n"; setNumThreads.body() << "symTable.setNumLanes(getNumThreads());\n"; setNumThreads.body() << "recordTable.setNumLanes(getNumThreads());\n"; + setNumThreads.body() << "regexCache.setNumLanes(getNumThreads());\n"; if (!prog.getSubroutines().empty()) { // generate subroutine adapter