miniCBF: read the compressed file through a buffer the caller keeps

Slurp() returned a fresh std::vector per call, so every frame allocated the
whole compressed file - 18 MB on a 16 Mpx detector - and value-initialised it
before the read overwrote every byte. Measured on one such frame that is 9.2 ms
against 5.4 ms into a buffer that is already there.

ReadInto() now takes the scratch, and the raw image carries it, so a worker
reading a sweep reads every file into the same bytes. Slurp() fills a buffer
instead of returning one, which is also what makes the resize cost nothing when
the next file is the same size as the last.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
leonarski_fandClaude Opus 5 committed 2026-09-09 20:14:04 +02:00
1 parent 4bac6d3f21
commit 391736425f
5 files changed
+24 -16

No files matched your search

+6 -4
View File
@@ -301,7 +301,8 @@ void JFJochCBFReader::Close() {
}
template <class Buffer>
CompressedImage JFJochCBFReader::DecodeInto(int64_t image_number, Buffer &buffer) const {
CompressedImage JFJochCBFReader::DecodeInto(int64_t image_number, Buffer &buffer,
std::vector<uint8_t> &scratch) const {
if (image_number < 0 || static_cast<size_t>(image_number) >= files_.size())
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
"Image number out of range");
@@ -312,7 +313,7 @@ CompressedImage JFJochCBFReader::DecodeInto(int64_t image_number, Buffer &buffer
// Decode straight into the caller's bytes: the pixels are plain int32 and nothing downstream has
// to decompress them, so NO_COMPRESSION over that buffer is the whole image.
const auto h = minicbf::ReadInto(files_[image_number],
reinterpret_cast<int32_t *>(buffer.data()), npixel);
reinterpret_cast<int32_t *>(buffer.data()), npixel, scratch);
if (static_cast<size_t>(h.nelem) != npixel)
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
"CBF image size differs from the first image of the sweep");
@@ -333,13 +334,14 @@ bool JFJochCBFReader::LoadImage_i(std::shared_ptr<JFJochReaderDataset> &dataset,
// The image must outlive this call, so it is decoded straight into the caller's buffer - the same
// thing the argument is for on the HDF5 path - and message.image only points at it.
message.image = DecodeInto(image_number, buffer);
std::vector<uint8_t> scratch;
message.image = DecodeInto(image_number, buffer, scratch);
message.number = image_number;
return true;
}
bool JFJochCBFReader::ReadRawImage(int64_t image_number, JFJochReaderRawImage &image) {
image.image = DecodeInto(image_number, image.image_buffer);
image.image = DecodeInto(image_number, image.image_buffer, image.read_buffer);
return true;
}
+3 -2
View File
@@ -32,9 +32,10 @@ class JFJochCBFReader : public JFJochReader {
bool update_dataset) override;
// Decodes one image into the caller's byte buffer (RawByteBuffer or std::vector<uint8_t>) and
// returns the image that points at it.
// returns the image that points at it. The compressed file is read through scratch, which the
// caller keeps between frames.
template <class Buffer>
CompressedImage DecodeInto(int64_t image_number, Buffer &buffer) const;
CompressedImage DecodeInto(int64_t image_number, Buffer &buffer, std::vector<uint8_t> &scratch) const;
public:
~JFJochCBFReader() override = default;
+4
View File
@@ -27,6 +27,10 @@ constexpr static int32_t SATURATED_PXL_VALUE = INT32_MAX;
struct JFJochReaderRawImage {
RawByteBuffer image_buffer;
CompressedImage image;
// Scratch the reader needs to produce the image and that nothing else reads: the CBF reader
// reads the compressed file into it before decoding. It lives here so that a raw image reused
// from one frame to the next reuses this buffer too.
std::vector<uint8_t> read_buffer;
};
class JFJochReaderImage {
+8 -9
View File
@@ -340,7 +340,7 @@ void DecodeByteOffset(const uint8_t *data, size_t size, int32_t *out, size_t n_p
namespace {
std::vector<uint8_t> Slurp(const std::string &path, size_t max_bytes) {
void Slurp(const std::string &path, size_t max_bytes, std::vector<uint8_t> &buf) {
std::ifstream f(path, std::ios::binary);
if (!f)
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
@@ -348,10 +348,9 @@ std::vector<uint8_t> Slurp(const std::string &path, size_t max_bytes) {
f.seekg(0, std::ios::end);
const auto file_size = static_cast<size_t>(f.tellg());
f.seekg(0, std::ios::beg);
std::vector<uint8_t> buf(std::min(file_size, max_bytes));
buf.resize(std::min(file_size, max_bytes));
f.read(reinterpret_cast<char *>(buf.data()), static_cast<std::streamsize>(buf.size()));
buf.resize(static_cast<size_t>(f.gcount()));
return buf;
}
// Enough to reach the separator on any header seen in the wild (the longest measured is ~6.3 kB).
@@ -360,7 +359,8 @@ constexpr size_t HEADER_PROBE_BYTES = 256 * 1024;
} // namespace
Header ReadHeader(const std::string &path) {
const auto buf = Slurp(path, HEADER_PROBE_BYTES);
std::vector<uint8_t> buf;
Slurp(path, HEADER_PROBE_BYTES, buf);
const auto start = FindBinarySection(buf.data(), buf.size());
if (!start.has_value())
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
@@ -373,7 +373,7 @@ namespace {
// One read of the file, header parsed, dimensions checked. The caller supplies where the pixels go.
Header ReadCommon(const std::string &path, std::vector<uint8_t> &buf, size_t &binary_start) {
buf = Slurp(path, std::numeric_limits<size_t>::max());
Slurp(path, std::numeric_limits<size_t>::max(), buf);
const auto start = FindBinarySection(buf.data(), buf.size());
if (!start.has_value())
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
@@ -413,14 +413,13 @@ Header Read(const std::string &path, std::vector<int32_t> &out) {
return h;
}
Header ReadInto(const std::string &path, int32_t *out, size_t capacity) {
std::vector<uint8_t> buf;
Header ReadInto(const std::string &path, int32_t *out, size_t capacity, std::vector<uint8_t> &scratch) {
size_t start = 0;
const Header h = ReadCommon(path, buf, start);
const Header h = ReadCommon(path, scratch, start);
if (static_cast<size_t>(h.nelem) > capacity)
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid,
"CBF image does not fit the supplied buffer: " + path);
DecodeByteOffset(buf.data() + start, buf.size() - start, out, static_cast<size_t>(h.nelem));
DecodeByteOffset(scratch.data() + start, scratch.size() - start, out, static_cast<size_t>(h.nelem));
return h;
}
+3 -1
View File
@@ -84,8 +84,10 @@ void DecodeByteOffset(const uint8_t *data, size_t size, int32_t *out, size_t n_p
Header Read(const std::string &path, std::vector<int32_t> &out);
// The same, decoding into memory the caller already has (one read of the file, no extra copy).
// The compressed file is read through scratch, which the caller keeps between frames: a fresh
// vector per image is megabytes of allocation and of zeroing nothing ever reads.
// Throws if the image does not fit in capacity pixels.
Header ReadInto(const std::string &path, int32_t *out, size_t capacity);
Header ReadInto(const std::string &path, int32_t *out, size_t capacity, std::vector<uint8_t> &scratch);
// Header only - reads just enough of the file to reach the separator. Cheap enough to call per
// frame for the rotation angles.