Skip to content

Commit

Permalink
Use a vector of spans to avoid copies.
Browse files Browse the repository at this point in the history
  • Loading branch information
aliddell committed Dec 4, 2024
1 parent 1f95b05 commit 5717867
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/streaming/vectorized.file.writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ zarr::VectorizedFileWriter::~VectorizedFileWriter()

bool
zarr::VectorizedFileWriter::write_vectors(
const std::vector<std::vector<std::byte>>& buffers,
const std::vector<std::span<std::byte>>& buffers,
size_t offset)
{
std::lock_guard<std::mutex> lock(mutex_);
Expand Down
2 changes: 1 addition & 1 deletion src/streaming/vectorized.file.writer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class VectorizedFileWriter
explicit VectorizedFileWriter(const std::string& path);
~VectorizedFileWriter();

bool write_vectors(const std::vector<std::vector<std::byte>>& buffers,
bool write_vectors(const std::vector<std::span<std::byte>>& buffers,
size_t offset);

std::mutex& mutex() { return mutex_; }
Expand Down
8 changes: 6 additions & 2 deletions tests/unit-tests/vectorized-file-write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@ write_to_file(const std::string& filename)
zarr::VectorizedFileWriter writer(filename);

std::vector<std::vector<std::byte>> data(10);
std::vector<std::span<std::byte>> spans(10);

for (auto i = 0; i < data.size(); ++i) {
data[i].resize((i + 1) * 1024);
std::fill(data[i].begin(), data[i].end(), std::byte(i));
file_size += data[i].size();
spans[i] = data[i];
}
CHECK(writer.write_vectors(data, 0));
CHECK(writer.write_vectors(spans, 0));

// write more data
for (auto i = 0; i < 10; ++i) {
auto& vec = data[i];
std::fill(vec.begin(), vec.end(), std::byte(i + 10));
spans[i] = vec;
}
CHECK(writer.write_vectors(data, file_size));
CHECK(writer.write_vectors(spans, file_size));

return 2 * file_size;
}
Expand Down

0 comments on commit 5717867

Please sign in to comment.