SMV reader: convert frames straight out of the page cache (mmap)
The geometry walk re-integrates the whole sweep in every round, and on an
uncompressed SMV sweep that image loop is bound by memory traffic:
smv::ReadInto was the top function (33% of all user cycles on 2xfw), and
16 threads read 360 frames no faster than 1 thread. On Linux/macOS the
frame is now mapped and widened from the mapping, which drops the
read() copy of the whole file into a scratch buffer. Windows keeps the
ifstream read. Pixel values are unchanged; every pass benefits, not only
the walk.
Measured (RTX 5080 workstation, load 9-23 from other agents, 2 repeats,
base = rc175 0457cecaf, same flags):
2xfw WALL 48.15/48.33 -> 40.49/41.98 s, Geometry check 17.2/17.4 -> 13.6/13.8 s
6zqy WALL 26.10/.. -> 23.60/23.16 s, Geometry check 8.6 -> 7.1/7.2 s
2ygz WALL 32.05 -> 26.27 s
-N 8: 2xfw 50.09 -> 42.82 s, 6zqy 26.78 -> 22.97 s
peak RSS -0.5 GB (no scratch copy)
Microbenchmark, 360 2xfw frames, 16 threads: read 1.8-1.9 s, mmap 1.2-1.4 s.
p.hkl md5 identical to the base on every set run (2xfw, 6zqy, 2ygz, -N 8,
and the small SMV sets of the open arm).
Tried first and dropped: keeping the decoded frames in memory across the
walk's rounds. Exact, but filling 13.6 GB of fresh memory cost more than
the hits saved (2xfw round 1 +2.4 s, release +0.9 s, later rounds -0.8 s
each; CBF sets 5m17/7ph1 got slower), at +12.5 GB peak RSS.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SVmAWnzCmRKAXVUCdc4iNi
This commit is contained in:
1 parent
0457cecafa
commit
9656a3b235
2 files changed
+44
-5
No files matched your search
+42
-4
@@ -15,6 +15,13 @@
|
||||
|
||||
#include "../common/JFJochException.h"
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace smv {
|
||||
|
||||
namespace {
|
||||
@@ -369,18 +376,49 @@ Header ReadInto(const std::string &path, int32_t *out, size_t capacity, std::vec
|
||||
throw JFJochException(JFJochExceptionCategory::ArrayOutOfBounds,
|
||||
path + ": image is larger than the buffer given for it");
|
||||
|
||||
const size_t image_bytes = npixel * sizeof(uint16_t);
|
||||
const auto too_short = [&] {
|
||||
return JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
||||
path + ": file ends before the image does");
|
||||
};
|
||||
#ifdef _WIN32
|
||||
std::ifstream f(path, std::ios::binary);
|
||||
if (!f)
|
||||
throw JFJochException(JFJochExceptionCategory::MockFileOpenError, "Cannot open " + path);
|
||||
f.seekg(static_cast<std::streamoff>(h.data_offset), std::ios::beg);
|
||||
scratch.resize(npixel * sizeof(uint16_t));
|
||||
scratch.resize(image_bytes);
|
||||
f.read(reinterpret_cast<char *>(scratch.data()), static_cast<std::streamsize>(scratch.size()));
|
||||
if (static_cast<size_t>(f.gcount()) != scratch.size())
|
||||
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
|
||||
path + ": file ends before the image does");
|
||||
throw too_short();
|
||||
const uint8_t *src = scratch.data();
|
||||
#else
|
||||
// Mapped rather than read: the pixels are converted straight out of the page cache, where a read
|
||||
// first copies the whole frame into a buffer. The image loop that reads a sweep - again in every
|
||||
// pass, and in every round of the geometry walk - is bound by memory traffic, and the copy adds the
|
||||
// whole file to it: measured on a 9 Mpx sweep of 360 frames, mapping took a seventh off the run.
|
||||
(void) scratch;
|
||||
const int fd = open(path.c_str(), O_RDONLY);
|
||||
if (fd < 0)
|
||||
throw JFJochException(JFJochExceptionCategory::MockFileOpenError, "Cannot open " + path);
|
||||
struct stat st{};
|
||||
if (fstat(fd, &st) != 0 || static_cast<size_t>(st.st_size) < h.data_offset + image_bytes) {
|
||||
close(fd);
|
||||
throw too_short();
|
||||
}
|
||||
const size_t mapped_bytes = h.data_offset + image_bytes;
|
||||
void *mapped = mmap(nullptr, mapped_bytes, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
close(fd);
|
||||
if (mapped == MAP_FAILED)
|
||||
throw JFJochException(JFJochExceptionCategory::MockFileOpenError, "Cannot map " + path);
|
||||
struct Unmap {
|
||||
void *p;
|
||||
size_t bytes;
|
||||
~Unmap() { munmap(p, bytes); }
|
||||
} unmap{mapped, mapped_bytes};
|
||||
const uint8_t *src = static_cast<const uint8_t *>(mapped) + h.data_offset;
|
||||
#endif
|
||||
|
||||
// Stored unsigned 16-bit, handed out as the signed 32-bit the rest of the code reads.
|
||||
const uint8_t *src = scratch.data();
|
||||
if (h.little_endian) {
|
||||
for (size_t i = 0; i < npixel; i++)
|
||||
out[i] = static_cast<int32_t>(static_cast<uint16_t>(src[2 * i] | (src[2 * i + 1] << 8)));
|
||||
|
||||
Reference in new issue
Block a user