Files
FEX-Emu--FEX/External/FEXCore/Source/Utils/FileLoading.cpp
T
Ryan Houdek c4a1d7e0cd FileLoading: Optimize FileLoad
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.
2023-01-18 17:51:38 -08:00

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;
}
}