From 1c61e7025cc3f8be53ebfad50293f9b388667941 Mon Sep 17 00:00:00 2001 From: WillisMedwell Date: Tue, 30 Jan 2024 15:33:44 +1100 Subject: [PATCH] Experimenting with unix i/o. Not much success --- src/FileReader.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/FileReader.cpp b/src/FileReader.cpp index 366e981..466f7ee 100644 --- a/src/FileReader.cpp +++ b/src/FileReader.cpp @@ -77,7 +77,54 @@ namespace Utily { } } +/* +Using mmap or sys read is not as fast as just using clib functions. + +#elif defined(__unix__) + +#include +#include +#include + +namespace Utily { + auto FileReader::load_entire_file(std::filesystem::path file_path) + -> Utily::Result, Utily::Error> { + + auto fp = file_path.c_str(); + + constexpr bool NeedsPathConversion = std::same_as; + + int handle = -1; + + if constexpr (NeedsPathConversion) { + std::string utf8 = file_path.string(); + handle = open(utf8.c_str(), O_RDONLY); + } else { + handle = open(fp, O_RDONLY); + } + + if (handle == -1) { + return Utily::Error { std::format("The file {} could not be opened.", fp) }; + } + + size_t file_size = static_cast(lseek(handle, 0, SEEK_END)); + lseek(handle, 0, SEEK_SET); + + std::vector buffer(file_size); + + uint8_t* mapped_memory= reinterpret_cast(mmap(NULL, file_size, PROT_READ, MAP_SHARED, handle, 0)); + madvise(mapped_memory, file_size, MADV_WILLNEED); + + std::copy(mapped_memory, mapped_memory + file_size, buffer.data()); + + munmap(mapped_memory, file_size); + close(handle); + return buffer; + } +} + #else +*/ #warning "Platform is not optimised for file reading." #include