mirror of
https://github.com/NixOS/nix
synced 2025-06-25 10:41:16 +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-container"
|
||||||
"--with-context"
|
"--with-context"
|
||||||
"--with-coroutine"
|
"--with-coroutine"
|
||||||
|
"--with-iostreams"
|
||||||
];
|
];
|
||||||
}).overrideAttrs
|
}).overrideAttrs
|
||||||
(old: {
|
(old: {
|
||||||
|
|
|
@ -21,6 +21,8 @@
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <boost/iostreams/device/mapped_file.hpp>
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
# include <io.h>
|
# include <io.h>
|
||||||
#endif
|
#endif
|
||||||
|
@ -273,9 +275,22 @@ std::string readFile(const std::filesystem::path & path)
|
||||||
return readFile(os_string_to_string(PathViewNG { path }));
|
return readFile(os_string_to_string(PathViewNG { path }));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void readFile(const Path & path, Sink & sink, bool memory_map)
|
||||||
void readFile(const Path & path, Sink & sink)
|
|
||||||
{
|
{
|
||||||
|
// 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
|
AutoCloseFD fd = toDescriptor(open(path.c_str(), O_RDONLY
|
||||||
// TODO
|
// TODO
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
|
|
|
@ -173,7 +173,7 @@ Descriptor openDirectory(const std::filesystem::path & path);
|
||||||
*/
|
*/
|
||||||
std::string readFile(const Path & path);
|
std::string readFile(const Path & path);
|
||||||
std::string readFile(const std::filesystem::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.
|
* Write a string to a file.
|
||||||
|
|
|
@ -56,7 +56,7 @@ deps_private += blake3
|
||||||
|
|
||||||
boost = dependency(
|
boost = dependency(
|
||||||
'boost',
|
'boost',
|
||||||
modules : ['context', 'coroutine'],
|
modules : ['context', 'coroutine', 'iostreams'],
|
||||||
include_type: 'system',
|
include_type: 'system',
|
||||||
)
|
)
|
||||||
# boost is a public dependency, but not a pkg-config dependency unfortunately, so we
|
# 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