mirror of
https://github.com/NixOS/nix
synced 2025-06-24 22:11:15 +02:00
Implement memory-mapped IO for Sinks
This commit is contained in:
parent
a87c3711b6
commit
b1783ff615
4 changed files with 20 additions and 4 deletions
|
@ -61,6 +61,7 @@ scope: {
|
|||
"--with-container"
|
||||
"--with-context"
|
||||
"--with-coroutine"
|
||||
"--with-iostreams"
|
||||
];
|
||||
}).overrideAttrs
|
||||
(old: {
|
||||
|
|
|
@ -21,6 +21,8 @@
|
|||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <boost/iostreams/device/mapped_file.hpp>
|
||||
|
||||
#ifdef _WIN32
|
||||
# include <io.h>
|
||||
#endif
|
||||
|
@ -273,9 +275,22 @@ std::string readFile(const std::filesystem::path & path)
|
|||
return readFile(os_string_to_string(PathViewNG { path }));
|
||||
}
|
||||
|
||||
|
||||
void readFile(const Path & path, Sink & sink)
|
||||
void readFile(const Path & path, Sink & sink, bool memory_map)
|
||||
{
|
||||
// Memory-map the file for faster processing where possible.
|
||||
if (memory_map) {
|
||||
try {
|
||||
boost::iostreams::mapped_file_source mmap(path);
|
||||
if (mmap.is_open()) {
|
||||
sink({mmap.data(), mmap.size()});
|
||||
return;
|
||||
}
|
||||
} catch (const boost::exception & e) {
|
||||
}
|
||||
debug("memory-mapping failed for path: %s", path);
|
||||
}
|
||||
|
||||
// Stream the file instead if memory-mapping fails or is disabled.
|
||||
AutoCloseFD fd = toDescriptor(open(path.c_str(), O_RDONLY
|
||||
// TODO
|
||||
#ifndef _WIN32
|
||||
|
|
|
@ -173,7 +173,7 @@ Descriptor openDirectory(const std::filesystem::path & path);
|
|||
*/
|
||||
std::string readFile(const Path & path);
|
||||
std::string readFile(const std::filesystem::path & path);
|
||||
void readFile(const Path & path, Sink & sink);
|
||||
void readFile(const Path & path, Sink & sink, bool memory_map = true);
|
||||
|
||||
/**
|
||||
* Write a string to a file.
|
||||
|
|
|
@ -56,7 +56,7 @@ deps_private += blake3
|
|||
|
||||
boost = dependency(
|
||||
'boost',
|
||||
modules : ['context', 'coroutine'],
|
||||
modules : ['context', 'coroutine', 'iostreams'],
|
||||
include_type: 'system',
|
||||
)
|
||||
# boost is a public dependency, but not a pkg-config dependency unfortunately, so we
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue