From 9656a3b2357edceda65c7086e743dd7c3885ccb5 Mon Sep 17 00:00:00 2001 From: Filip Leonarski Date: Sat, 10 Oct 2026 00:00:44 +0200 Subject: [PATCH] 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) Claude-Session: https://claude.ai/code/session_01SVmAWnzCmRKAXVUCdc4iNi --- reader/SMV.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++---- reader/SMV.h | 3 ++- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/reader/SMV.cpp b/reader/SMV.cpp index c497afb36..16c30011c 100644 --- a/reader/SMV.cpp +++ b/reader/SMV.cpp @@ -15,6 +15,13 @@ #include "../common/JFJochException.h" +#ifndef _WIN32 +#include +#include +#include +#include +#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(h.data_offset), std::ios::beg); - scratch.resize(npixel * sizeof(uint16_t)); + scratch.resize(image_bytes); f.read(reinterpret_cast(scratch.data()), static_cast(scratch.size())); if (static_cast(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(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(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(static_cast(src[2 * i] | (src[2 * i + 1] << 8))); diff --git a/reader/SMV.h b/reader/SMV.h index 9bbf3a14d..708a5d17a 100644 --- a/reader/SMV.h +++ b/reader/SMV.h @@ -85,7 +85,8 @@ Header ReadHeader(const std::string &path); Header Read(const std::string &path, std::vector &out); // The same, widening the stored pixels into memory the caller already has. scratch carries the -// raw bytes between frames so a worker walking a sweep allocates once. +// raw bytes between frames so a worker walking a sweep allocates once - on Windows only; elsewhere the +// file is mapped and scratch is not used. Header ReadInto(const std::string &path, int32_t *out, size_t capacity, std::vector &scratch); } // namespace smv