Skip to content

Commit

Permalink
Check for sortedness of the slabs to populate/reuse before re-sorting.
Browse files Browse the repository at this point in the history
This makes the dense oracular extractor consistent with its sparse counterpart.
  • Loading branch information
LTLA committed Sep 26, 2024
1 parent 902d8c2 commit 0bb4397
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions include/tatami_hdf5/DenseMatrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,16 @@ class OracularCoreNormal {
size_t my_offset = 0;

private:
template<class Function_>
static void sort_by_field(std::vector<std::pair<Index_, Slab*> >& indices, Function_ field) {
auto comp = [&field](const std::pair<Index_, Slab*>& l, const std::pair<Index_, Slab*>& r) -> bool {
return field(l) < field(r);
};
if (!std::is_sorted(indices.begin(), indices.end(), comp)) {
std::sort(indices.begin(), indices.end(), comp);
}
}

template<typename Value_, class Unionize_>
void fetch_raw([[maybe_unused]] Index_ i, Value_* buffer, size_t non_target_length, Unionize_ unionize) {
auto info = my_cache.next(
Expand All @@ -323,9 +333,7 @@ class OracularCoreNormal {
/* populate = */ [&](std::vector<std::pair<Index_, Slab*> >& chunks, std::vector<std::pair<Index_, Slab*> >& to_reuse) {
// Defragmenting the existing chunks. We sort by offset to make
// sure that we're not clobbering in-use slabs during the copy().
std::sort(to_reuse.begin(), to_reuse.end(), [](const std::pair<Index_, Slab*>& left, const std::pair<Index_, Slab*>& right) -> bool {
return left.second->offset < right.second->offset;
});
sort_by_field(to_reuse, [](const std::pair<Index_, Slab*>& x) -> size_t { return x.second->offset; });

auto dest = my_memory_pool.data();
size_t running_offset = 0;
Expand All @@ -343,7 +351,7 @@ class OracularCoreNormal {
// to populate the contiguous memory pool that we made available after
// defragmentation; then we just update the slab pointers to refer
// to the slices of memory corresponding to each slab.
std::sort(chunks.begin(), chunks.end());
sort_by_field(chunks, [](const std::pair<Index_, Slab*>& x) -> Index_ { return x.first; });

serialize([&]() -> void {
auto& components = *my_h5comp;
Expand Down

0 comments on commit 0bb4397

Please sign in to comment.