mirror of
https://github.com/FEX-Emu/FEX.git
synced 2026-10-06 22:00:16 +02:00
Optimize `FileLoad` by not using fstream. For some reason fstream is just really bad. Switching over to raw pread cuts the amount of time it takes to read files by a quarter of CPU time.
53 lines
979 B
C++
53 lines
979 B
C++
#include <string>
|
|
#include <vector>
|
|
#include <fcntl.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
#include <span>
|
|
#include <unistd.h>
|
|
|
|
namespace FEXCore::FileLoading {
|
|
bool LoadFile(std::vector<char> &Data, const std::string &Filepath, size_t FixedSize) {
|
|
int FD = open(Filepath.c_str(), O_RDONLY);
|
|
|
|
if (FD == -1) {
|
|
return false;
|
|
}
|
|
|
|
size_t FileSize{};
|
|
if (FixedSize == 0) {
|
|
struct stat buf;
|
|
if (fstat(FD, &buf) != 0) {
|
|
close(FD);
|
|
return false;
|
|
}
|
|
|
|
FileSize = buf.st_size;
|
|
}
|
|
else {
|
|
FileSize = FixedSize;
|
|
}
|
|
|
|
ssize_t Read = -1;
|
|
if (FileSize > 0) {
|
|
Data.resize(FileSize);
|
|
Read = pread(FD, &Data.at(0), FileSize, 0);
|
|
}
|
|
close(FD);
|
|
return Read == FileSize;
|
|
}
|
|
|
|
ssize_t LoadFileToBuffer(const std::string &Filepath, std::span<char> Buffer) {
|
|
int FD = open(Filepath.c_str(), O_RDONLY);
|
|
|
|
if (FD == -1) {
|
|
return -1;
|
|
}
|
|
|
|
ssize_t Read = pread(FD, Buffer.data(), Buffer.size(), 0);
|
|
close(FD);
|
|
return Read;
|
|
}
|
|
|
|
}
|