Skip to content

Commit

Permalink
Avoid copies of embedded_file data
Browse files Browse the repository at this point in the history
Addresses #3835
  • Loading branch information
lefticus committed Jul 18, 2020
1 parent 1a2828b commit a478258
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
5 changes: 3 additions & 2 deletions embedded/embedded_files.cxx.in
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace embedded_files {
@EMBEDDED_FILES@
};

std::vector<std::string> fileNames() {
const std::vector<std::string> &fileNames() {
static const std::vector<std::string> result = [](){
std::vector<std::string> val;
val.reserve(embedded_file_count);
Expand All @@ -29,10 +29,11 @@ namespace embedded_files {
}
return val;
}();

return result;
}

std::map<std::string, std::pair<size_t, const uint8_t *>> files()
const std::map<std::string, std::pair<size_t, const uint8_t *>> &files()
{
static const std::map<std::string, std::pair<size_t, const uint8_t *>> fs = [](){
std::map<std::string, std::pair<size_t, const uint8_t *>> val;
Expand Down
12 changes: 6 additions & 6 deletions embedded/embedded_files.hxx.in
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ inline int inf(const EmbeddedFile & file, std::vector<uint8_t> & result)


namespace embedded_files {
std::map<std::string, EmbeddedFile > files();
const std::map<std::string, EmbeddedFile> &files();

std::vector<std::string> fileNames();
const std::vector<std::string> &fileNames();

inline std::string allFileNamesAsString(){
std::string result;
for (const auto& fileName: fileNames()){
for (const auto& fileName : fileNames()){
result += (fileName + ";");
}
return result;
Expand All @@ -138,12 +138,12 @@ namespace embedded_files {
}

inline bool hasFile(const std::string &t_filename) {
const auto fs = fileNames();
const auto &fs = fileNames();
return (std::find(fs.begin(), fs.end(), t_filename) != fs.end());
}

inline std::string getFileAsString(const std::string &t_filename) {
const auto fs = files();
const auto &fs = files();
const auto f = fs.find(t_filename);
if (f == fs.end()){
throw std::runtime_error("Embedded file not found '" + t_filename + "'");
Expand All @@ -157,7 +157,7 @@ namespace embedded_files {

inline void extractFile(const std::string &t_filename, const std::string &t_location)
{
const auto fs = files();
const auto &fs = files();


const auto create_dirs = [](const std::string &t_path) {
Expand Down

0 comments on commit a478258

Please sign in to comment.