From db0f9f70d42361a99b3290f0cd8fbe66e276411e Mon Sep 17 00:00:00 2001 From: Maschell Date: Mon, 28 Dec 2020 14:46:30 +0100 Subject: [PATCH] Read plugins into a aligned buffer to improve reading speed. --- source/plugin/PluginDataFactory.cpp | 43 ++++++++++++++--------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/source/plugin/PluginDataFactory.cpp b/source/plugin/PluginDataFactory.cpp index 8a5fd08..350fca4 100644 --- a/source/plugin/PluginDataFactory.cpp +++ b/source/plugin/PluginDataFactory.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include "PluginDataFactory.h" #include "../utils/logger.h" #include "../utils/StringTools.h" @@ -64,32 +65,30 @@ std::vector PluginDataFactory::loadDir(const std::string &path, MEMH } std::optional PluginDataFactory::load(const std::string &filename, MEMHeapHandle heapHandle) { - // open the file: - std::ifstream file(filename, std::ios::binary); - if (!file.is_open()) { - DEBUG_FUNCTION_LINE("Failed to open %s", filename.c_str()); - return std::nullopt; + // Not going to explicitly check these. + // The use of gcount() below will compensate for a failure here. + std::ifstream is(filename, std::ios::binary); + + 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!!! - file.unsetf(std::ios::skipws); - // get its size: - std::streampos fileSize; + is.read(data, length); - file.seekg(0, std::ios::end); - fileSize = file.tellg(); - file.seekg(0, std::ios::beg); + std::vector result; + result.resize(length); + memcpy(&result[0], data, length); + free(data); - std::vector vBuffer; - vBuffer.reserve(fileSize); + DEBUG_FUNCTION_LINE("Loaded file!"); - // read the data: - vBuffer.insert(vBuffer.begin(), - std::istream_iterator(file), - std::istream_iterator()); - - DEBUG_FUNCTION_LINE("Loaded file"); - - return load(vBuffer, heapHandle); + return load(result, heapHandle); } std::optional PluginDataFactory::load(std::vector &buffer, MEMHeapHandle heapHandle) {