Skip to content

Commit e326445

Browse files
committed
avoid huge string acquisition during generator file conversion
1 parent 2abe8ca commit e326445

File tree

3 files changed

+48
-14
lines changed

3 files changed

+48
-14
lines changed

nortmann/DlProofEnumerator.cpp

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ void DlProofEnumerator::generateDProofRepresentativeFiles(uint32_t limit, bool r
595595
}
596596
}
597597
else
598-
for (const string& s : allRepresentatives.back()) {
598+
for (const string& s : allRepresentatives.back())
599599
if (first) {
600600
bytes += s.length();
601601
fout << s;
@@ -604,7 +604,6 @@ void DlProofEnumerator::generateDProofRepresentativeFiles(uint32_t limit, bool r
604604
bytes += s.length() + 1;
605605
fout << "\n" << s;
606606
}
607-
}
608607
}
609608
cout << FctHelper::durationStringMs(chrono::duration_cast<chrono::microseconds>(chrono::steady_clock::now() - startTime)) << " taken to print and save " << bytes << " bytes of representative condensed detachment proof strings to " << file.string() << "." << endl;
610609
}
@@ -648,12 +647,31 @@ void DlProofEnumerator::createGeneratorFilesWithConclusions(const string& inputF
648647
//#cout << FctHelper::mapStringF(result, [](const pair<const string, string>& p) { return p.first + ":" + p.second; }, { }, { }, "\n");
649648
}
650649

651-
// 3. Store generated D-proofs together with their conclusions permanently.
650+
// 3. Store generated D-proofs together with their conclusions permanently. Not using FctHelper::writeToFile() in order to write huge files without huge string acquisition.
652651
startTime = chrono::steady_clock::now();
653-
string file = outputFilePrefix + to_string(wordLengthLimit) + (wordLengthLimit < filteredMissing ? ".txt" : filePostfix);
654-
string content = FctHelper::mapStringF(result, [](const pair<const string, string>& p) { return p.first + ":" + p.second; }, { }, { }, "\n");
655-
FctHelper::writeToFile(file, content);
656-
cout << FctHelper::durationStringMs(chrono::duration_cast<chrono::microseconds>(chrono::steady_clock::now() - startTime)) << " taken to print and save " << content.length() << " bytes of representative condensed detachment proof strings to " << file << "." << endl;
652+
filesystem::path file = filesystem::u8path(outputFilePrefix + to_string(wordLengthLimit) + (wordLengthLimit < filteredMissing ? ".txt" : filePostfix));
653+
string::size_type bytes = 0;
654+
{
655+
while (!filesystem::exists(file) && !FctHelper::ensureDirExists(file.string()))
656+
cerr << "Failed to create file at \"" << file.string() << "\", trying again." << endl;
657+
time_t time = chrono::system_clock::to_time_t(chrono::system_clock::now());
658+
cout << strtok(ctime(&time), "\n") << ": Starting to write " << result.size() << " entries to " << file.string() << "." << endl;
659+
ofstream fout(file, fstream::out | fstream::binary);
660+
bool first = true;
661+
for (const pair<const string, string>& p : result) {
662+
const string& dProof = p.first;
663+
const string& conclusion = p.second;
664+
if (first) {
665+
bytes += dProof.length() + conclusion.length() + 1;
666+
fout << dProof << ":" << conclusion;
667+
first = false;
668+
} else {
669+
bytes += dProof.length() + conclusion.length() + 2;
670+
fout << "\n" << dProof << ":" << conclusion;
671+
}
672+
}
673+
}
674+
cout << FctHelper::durationStringMs(chrono::duration_cast<chrono::microseconds>(chrono::steady_clock::now() - startTime)) << " taken to print and save " << bytes << " bytes of representative condensed detachment proof strings to " << file.string() << "." << endl;
657675

658676
//#if (wordLengthLimit <= 15)
659677
//# cout << "const vector<string> Resources::dProofConclusions" << wordLengthLimit << " = " << FctHelper::mapStringF(result, [](const pair<const string, string>& p) { return p.second; }, "{ \"", "\" };", "\", \"") << endl;
@@ -681,13 +699,29 @@ void DlProofEnumerator::createGeneratorFilesWithoutConclusions(const string& inp
681699
return;
682700
}
683701

684-
// 2. Store generated D-proofs without their conclusions permanently.
702+
// 2. Store generated D-proofs without their conclusions permanently. Not using FctHelper::writeToFile() in order to write huge files without huge string acquisition.
685703
for (uint32_t wordLengthLimit = 1; wordLengthLimit < allRepresentatives.size(); wordLengthLimit += 2) {
686704
startTime = chrono::steady_clock::now();
687-
string file = outputFilePrefix + to_string(wordLengthLimit) + (wordLengthLimit < filteredMissing ? ".txt" : filePostfix);
688-
string content = FctHelper::vectorString(allRepresentatives[wordLengthLimit], { }, { }, "\n");
689-
FctHelper::writeToFile(file, content);
690-
cout << FctHelper::durationStringMs(chrono::duration_cast<chrono::microseconds>(chrono::steady_clock::now() - startTime)) << " taken to print and save " << content.length() << " bytes of representative condensed detachment proof strings to " << file << "." << endl;
705+
filesystem::path file = filesystem::u8path(outputFilePrefix + to_string(wordLengthLimit) + (wordLengthLimit < filteredMissing ? ".txt" : filePostfix));
706+
string::size_type bytes = 0;
707+
{
708+
while (!filesystem::exists(file) && !FctHelper::ensureDirExists(file.string()))
709+
cerr << "Failed to create file at \"" << file.string() << "\", trying again." << endl;
710+
time_t time = chrono::system_clock::to_time_t(chrono::system_clock::now());
711+
cout << strtok(ctime(&time), "\n") << ": Starting to write " << allRepresentatives[wordLengthLimit].size() << " entries to " << file.string() << "." << endl;
712+
ofstream fout(file, fstream::out | fstream::binary);
713+
bool first = true;
714+
for (const string& s : allRepresentatives[wordLengthLimit])
715+
if (first) {
716+
bytes += s.length();
717+
fout << s;
718+
first = false;
719+
} else {
720+
bytes += s.length() + 1;
721+
fout << "\n" << s;
722+
}
723+
}
724+
cout << FctHelper::durationStringMs(chrono::duration_cast<chrono::microseconds>(chrono::steady_clock::now() - startTime)) << " taken to print and save " << bytes << " bytes of representative condensed detachment proof strings to " << file.string() << "." << endl;
691725
}
692726
}
693727

nortmann/DlProofEnumerator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ void DlProofEnumerator::_loadAndProcessQueuesConcurrently(unsigned concurrencyCo
112112
std::condition_variable cond;
113113
std::vector<std::thread> threads;
114114
std::atomic<bool> incomplete = true; // NOTE: Indicates whether balancing may still take place, not whether all all queues are empty.
115-
auto worker = [&process, &tinyBound, &queues, &cond, &mtxs, &incomplete](unsigned t) {
115+
auto worker = [&process, &queues, &cond, &mtxs, &incomplete](unsigned t) {
116116
std::deque<std::string>& queue = queues[t];
117117
size_t size = 0;
118118
// NOTE: It is important that we update 'size' here in case !incomplete, since 'queue' might become

nortmann/DlStructure.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct DlStructure {
2222
// Y -> 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Y 0 | Y 1 | Y 2 | Y 3 | Y 4 | Y 5 | Y 6 | Y 7 | Y 8 | Y 9
2323
static grammar::CfgGrammar& dlEvaluationGrammar();
2424

25-
// Grammar constants (of dlEvaluationGrammar()) ; used in method evaluateParseTree() for fast expression evaluation
25+
// Grammar constants (of dlEvaluationGrammar())
2626
static const uint32_t& nonterminal_at();
2727
static const uint32_t& nonterminal_S();
2828
static const uint32_t& nonterminal_A();

0 commit comments

Comments
 (0)