-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MAJOR CHANGE: USE MEMORY MAPPED FILE
- This just makes everything so much more simpler - The performance seems to have improved a bit - However, it's a file searcher hence pretty much everything is IO bound...
- Loading branch information
Showing
14 changed files
with
267 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
#include "PCH.hpp" | ||
#include "MemoryMappedFile.hpp" | ||
|
||
#ifdef _WIN32 | ||
|
||
#define NOMINMAX | ||
#include <Windows.h> | ||
|
||
class MemoryMappedFileImpl | ||
{ | ||
public: | ||
MemoryMappedFileImpl(const std::filesystem::path& path, uint64_t fileSize) : | ||
_file(CreateFileW( | ||
path.c_str(), | ||
GENERIC_READ | GENERIC_WRITE, | ||
0, | ||
nullptr, | ||
OPEN_EXISTING, | ||
FILE_ATTRIBUTE_NORMAL, | ||
NULL)) | ||
{ | ||
if (!_file || _file == INVALID_HANDLE_VALUE) | ||
{ | ||
throw std::system_error(GetLastError(), std::system_category(), "CreateFileW"); | ||
} | ||
|
||
LARGE_INTEGER mappingSize; | ||
mappingSize.QuadPart = fileSize; | ||
|
||
_mapping = CreateFileMappingW( | ||
_file, | ||
nullptr, | ||
PAGE_READWRITE, | ||
mappingSize.HighPart, | ||
mappingSize.LowPart, | ||
nullptr); | ||
|
||
if (!_mapping) | ||
{ | ||
throw std::system_error(GetLastError(), std::system_category(), "CreateFileMappingW"); | ||
} | ||
|
||
_view = MapViewOfFile(_mapping, FILE_MAP_ALL_ACCESS, 0, 0, fileSize); | ||
|
||
if (!_view) | ||
{ | ||
throw std::system_error(GetLastError(), std::system_category(), "MapViewOfFile"); | ||
} | ||
|
||
_size = fileSize; | ||
} | ||
|
||
~MemoryMappedFileImpl() | ||
{ | ||
if (_view) | ||
{ | ||
UnmapViewOfFile(_view); | ||
} | ||
|
||
if (_mapping) | ||
{ | ||
CloseHandle(_mapping); | ||
} | ||
|
||
if (_file) | ||
{ | ||
CloseHandle(_file); | ||
} | ||
} | ||
|
||
std::string_view Data() const | ||
{ | ||
return { reinterpret_cast<char*>(_view), _size }; | ||
} | ||
|
||
std::string_view Sample(size_t size) const | ||
{ | ||
return { reinterpret_cast<char*>(_view), std::min(size, _size) }; | ||
} | ||
|
||
private: | ||
HANDLE _file = nullptr; | ||
HANDLE _mapping = nullptr; | ||
void* _view = nullptr; | ||
uint64_t _size = 0; | ||
}; | ||
#else | ||
|
||
#include <fcntl.h> | ||
#include <sys/stat.h> | ||
#include <sys/mman.h> | ||
#include <unistd.h> | ||
|
||
class MemoryMappedFileImpl | ||
{ | ||
public: | ||
MemoryMappedFileImpl(const std::filesystem::path& path, uint64_t fileSize) : | ||
_descriptor(open(path.c_str(), O_RDONLY)) | ||
{ | ||
if (_descriptor == -1) | ||
{ | ||
throw std::system_error(errno, std::system_category(), "open"); | ||
} | ||
|
||
_view = mmap(nullptr, _size, PROT_READ, MAP_PRIVATE, _descriptor, 0); | ||
|
||
if (_view == MAP_FAILED) | ||
{ | ||
throw std::system_error(errno, std::system_category(), "mmap"); | ||
} | ||
|
||
_size = fileSize; | ||
} | ||
|
||
~MemoryMappedFileImpl() | ||
{ | ||
if (_descriptor) | ||
{ | ||
::close(_descriptor); | ||
} | ||
} | ||
|
||
std::string_view Sample(size_t size) | ||
{ | ||
return { reinterpret_cast<char*>(_view), std::min(_size, size) }; | ||
} | ||
|
||
std::string_view Data() const | ||
{ | ||
return { reinterpret_cast<char*>(_view), _size }; | ||
} | ||
|
||
private: | ||
int _descriptor = 0; | ||
void* _view = nullptr; | ||
uint64_t _size = 0; | ||
}; | ||
#endif | ||
|
||
MemoryMappedFile::MemoryMappedFile(const std::filesystem::path& path, uint64_t fileSize) : | ||
_impl(new MemoryMappedFileImpl(path, fileSize)) | ||
{ | ||
} | ||
|
||
MemoryMappedFile::~MemoryMappedFile() | ||
{ | ||
delete _impl; | ||
} | ||
|
||
std::string_view MemoryMappedFile::Sample(size_t size) const | ||
{ | ||
return _impl->Sample(size); | ||
} | ||
|
||
std::string_view MemoryMappedFile::Data() const | ||
{ | ||
return _impl->Data(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#pragma once | ||
|
||
#include "Pystykorva.hpp" | ||
|
||
class MemoryMappedFileImpl; | ||
|
||
// This interface has no other meaning than to allow testing | ||
|
||
class MemoryMappedFile : public Pystykorva::IFile | ||
{ | ||
public: | ||
MemoryMappedFile(const std::filesystem::path&, uint64_t); | ||
~MemoryMappedFile(); | ||
|
||
std::string_view Sample(size_t size = 0x400) const override; | ||
std::string_view Data() const override; | ||
|
||
private: | ||
MemoryMappedFileImpl* _impl; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.