Skip to content

Commit

Permalink
Use regex cache in the synthesized C++ code
Browse files Browse the repository at this point in the history
Update the synthesizer to use a cache for regexes.
One drawback is that we now need to #include <regex>,
but the structure of the synthesizer makes impossible
to generate the cache only when needed.
  • Loading branch information
strRM committed Oct 20, 2022
1 parent 97d0107 commit 7e4a101
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/include/souffle/CompiledSouffle.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions src/include/souffle/SouffleInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <algorithm>
#include <cassert>
Expand All @@ -29,6 +30,7 @@
#include <map>
#include <memory>
#include <optional>
#include <regex>
#include <string>
#include <tuple>
#include <utility>
Expand Down
10 changes: 8 additions & 2 deletions src/synthesiser/Synthesiser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2607,6 +2607,7 @@ void Synthesiser::generateCode(GenDb& db, const std::string& id, bool& withShare
std::vector<std::tuple<Mode, std::string /*name*/, std::string /*type*/>> 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<std::string,std::regex>"));
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*"));
Expand Down Expand Up @@ -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("<regex>");
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"
Expand Down Expand Up @@ -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<std::string,std::regex>", "regexCache", Visibility::Private);
constructor.setNextInitializer("regexCache", "");

if (Global::config().has("profile")) {
std::size_t numFreq = 0;
visit(prog, [&](const Statement&) { numFreq++; });
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7e4a101

Please sign in to comment.