Skip to content

Commit

Permalink
Enable multi-threading
Browse files Browse the repository at this point in the history
  • Loading branch information
MKlimenko committed Jan 4, 2024
1 parent ca949dc commit 10037de
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
13 changes: 10 additions & 3 deletions src/embed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "saver.hpp"

#include <algorithm>
#include <execution>
#include <fstream>

namespace fs = std::filesystem;
Expand All @@ -14,8 +16,12 @@ class Embed {
if (fs::is_directory(entry)) {
if (root_is_directory)
return;
std::vector<fs::path> files;
for (auto& el : fs::recursive_directory_iterator(entry))
Save(el, true);
if (!el.is_directory())
files.push_back(el.path());
std::for_each(std::execution::par_unseq, files.begin(), files.end(),
[this](auto& el) {Save(el, true); });
}
else {
if (!fs::exists(entry))
Expand All @@ -35,7 +41,8 @@ class Embed {
{}

void SaveAll(tcb::span<std::string> entries) {
for (auto& entry : entries)
Save(entry);
std::for_each(std::execution::par_unseq, entries.begin(), entries.end(),
[this](auto& entry) { Save(entry); }
);
}
};
44 changes: 34 additions & 10 deletions src/saver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <charconv>
#include <fstream>
#include <iostream>
#include <mutex>
#include <string>
#include <sstream>
#include <vector>
Expand All @@ -19,6 +20,7 @@ class Saver {
std::ofstream resource_holder_hpp;
std::ofstream resource_hpp;
std::ofstream span_hpp;
std::mutex m;
bool verbose = true;

const std::string subfolder_name = "embedded_resources";
Expand Down Expand Up @@ -897,7 +899,7 @@ class Saver {
};

auto Format(int val) const {
static std::array<char, 5> str;
std::array<char, 5> str;
auto [p, ec] = std::to_chars(str.data(), str.data() + str.size(), val);
*p++ = ',';
return std::string(str.data(), p - str.data());
Expand All @@ -915,6 +917,25 @@ class Saver {
return std::string(std::istreambuf_iterator<char>(test_file), std::istreambuf_iterator<char>()) == data.str();
}

struct AsyncPrinter final {
std::stringstream ss;

~AsyncPrinter() {
std::cout << ss.str();
}

template <typename T>
friend AsyncPrinter& operator<<(AsyncPrinter& printer, const T& value) {
printer.ss << value;
return printer;
}

friend AsyncPrinter& operator<<(AsyncPrinter& printer, std::ostream& (*f)(std::ostream&)) {
f(printer.ss);
return printer;
}
};

public:
Saver(fs::path root_path) :
root(root_path),
Expand Down Expand Up @@ -975,19 +996,22 @@ class Saver {

void Save(Resource::EmbeddedData data, fs::path resource_path) {
try {
AsyncPrinter console_output;
[[maybe_unused]]
auto corrected_path = resource_path.make_preferred();
if (verbose)
std::cout << "embed.exe: saving " << resource_path.string();
console_output << "embed.exe: saving " << resource_path.string();

auto array_filename = "resource_" + std::to_string(fs::hash_value(resource_path));
if (std::find(filenames.begin(), filenames.end(), array_filename) != filenames.end()) {
if (verbose)
std::cout << " ... Skipped as a duplicate" << std::endl;
return;
{
if (std::find(filenames.begin(), filenames.end(), array_filename) != filenames.end()) {
if (verbose)
console_output << " ... Skipped as a duplicate" << std::endl;
return;
}
auto lock = std::scoped_lock(m);
filenames.push_back(array_filename);
}

filenames.push_back(array_filename);
auto header_filename = array_filename + ".hpp";
auto header_path = fs::path(root).append(header_filename);

Expand All @@ -1005,7 +1029,7 @@ class Saver {

if (IsSame(out, header_path)) {
if (verbose)
std::cout << " ... Skipped" << std::endl;
console_output << " ... Skipped" << std::endl;
return;
}
std::ofstream out_file(header_path.c_str());
Expand All @@ -1014,7 +1038,7 @@ class Saver {

out_file << out.rdbuf();
if (verbose)
std::cout << std::endl;
console_output << std::endl;
}
catch (...) {
resource_holder_hpp << "static_assert(false, R\"(Error while embedding " << resource_path.string() << " file)\");" << std::endl;
Expand Down
2 changes: 1 addition & 1 deletion test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,5 @@ TEST_P(PerformanceTestFixture, performance_test) {
INSTANTIATE_TEST_SUITE_P(
PerformanceTest,
PerformanceTestFixture,
testing::Values(1, 8, 64, 512, 1024)
testing::Values(1, 8, 64, 512, 1024, 8192)
);

0 comments on commit 10037de

Please sign in to comment.