BLI: refactor IndexMask for better performance and memory usage #104629

Merged
Jacques Lucke merged 254 commits from JacquesLucke/blender:index-mask-refactor into main 2023-05-24 18:11:47 +02:00
3 changed files with 19 additions and 8 deletions
Showing only changes of commit 4c1c1cf6e2 - Show all commits

View File

@ -218,6 +218,8 @@ class IndexMask {
std::optional<IndexRange> to_range() const;
Vector<IndexRange> to_ranges() const;
Vector<IndexRange> to_ranges_invert(IndexRange universe) const;
void to_ranges_and_spans(Vector<IndexRange> &r_ranges,
Vector<OffsetSpan<int64_t, int16_t>> &r_spans) const;
const IndexMaskData &data() const;
IndexMaskData &data_for_inplace_construction();

View File

@ -891,6 +891,22 @@ Vector<IndexRange> IndexMask::to_ranges_invert(const IndexRange universe) const
return this->complement(universe, memory).to_ranges();
}
void IndexMask::to_ranges_and_spans(Vector<IndexRange> &r_ranges,
Vector<OffsetSpan<int64_t, int16_t>> &r_spans) const
{
const IndexRangeChecker range_checker;
this->foreach_span_template(
[&, range_checker](const int64_t chunk_id, const Span<int16_t> indices) {
const int64_t chunk_offset = chunk_id << chunk_size_shift;
if (range_checker.check(indices)) {
r_ranges.append_as(indices[0] + chunk_offset, indices.size());
}
else {
r_spans.append_as(chunk_offset, indices);
}
});
}
template IndexMask IndexMask::from_indices(Span<int32_t>, IndexMaskMemory &);
template IndexMask IndexMask::from_indices(Span<int64_t>, IndexMaskMemory &);
template void IndexMask::to_indices(MutableSpan<int32_t>) const;

View File

@ -405,14 +405,7 @@ inline void execute_element_fn_as_multi_function(const ElementFn element_fn,
Vector<IndexRange> mask_ranges;
Vector<OffsetSpan<int64_t, int16_t>> mask_spans;
mask.foreach_span_or_range([&](const auto segment) {
if constexpr (std::is_same_v<std::decay_t<decltype(segment)>, IndexRange>) {
mask_ranges.append(segment);
}
else {
mask_spans.append(segment);
}
});
mask.to_ranges_and_spans(mask_ranges, mask_spans);
/* Try execute devirtualized if enabled and the input types allow it. */
bool executed_devirtualized = false;