Read plugins into a aligned buffer to improve reading speed.

This commit is contained in:
Maschell 2020-12-28 14:46:30 +01:00
parent f7b4b568d2
commit db0f9f70d4

View File

@ -17,6 +17,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <dirent.h> #include <dirent.h>
#include <memory>
#include "PluginDataFactory.h" #include "PluginDataFactory.h"
#include "../utils/logger.h" #include "../utils/logger.h"
#include "../utils/StringTools.h" #include "../utils/StringTools.h"
@ -64,32 +65,30 @@ std::vector<PluginData> PluginDataFactory::loadDir(const std::string &path, MEMH
} }
std::optional<PluginData> PluginDataFactory::load(const std::string &filename, MEMHeapHandle heapHandle) { std::optional<PluginData> PluginDataFactory::load(const std::string &filename, MEMHeapHandle heapHandle) {
// open the file: // Not going to explicitly check these.
std::ifstream file(filename, std::ios::binary); // The use of gcount() below will compensate for a failure here.
if (!file.is_open()) { std::ifstream is(filename, std::ios::binary);
DEBUG_FUNCTION_LINE("Failed to open %s", filename.c_str());
return std::nullopt; is.seekg(0, std::ios::end);
std::streampos length = is.tellg();
is.seekg(0, std::ios::beg);
// reading into a 0x40 aligned buffer increases reading speed.
char *data = (char *) memalign(0x40, length);
if (!data) {
DEBUG_FUNCTION_LINE("Failed to alloc memory for holding the plugin");
return {};
} }
// Stop eating new lines in binary mode!!! is.read(data, length);
file.unsetf(std::ios::skipws);
// get its size:
std::streampos fileSize;
file.seekg(0, std::ios::end); std::vector<uint8_t> result;
fileSize = file.tellg(); result.resize(length);
file.seekg(0, std::ios::beg); memcpy(&result[0], data, length);
free(data);
std::vector<uint8_t> vBuffer; DEBUG_FUNCTION_LINE("Loaded file!");
vBuffer.reserve(fileSize);
// read the data: return load(result, heapHandle);
vBuffer.insert(vBuffer.begin(),
std::istream_iterator<uint8_t>(file),
std::istream_iterator<uint8_t>());
DEBUG_FUNCTION_LINE("Loaded file");
return load(vBuffer, heapHandle);
} }
std::optional<PluginData> PluginDataFactory::load(std::vector<uint8_t> &buffer, MEMHeapHandle heapHandle) { std::optional<PluginData> PluginDataFactory::load(std::vector<uint8_t> &buffer, MEMHeapHandle heapHandle) {