BLI: refactor IndexMask for better performance and memory usage #104629
|
@ -57,7 +57,7 @@ struct IndexMaskData {
|
|||
int64_t indices_num;
|
||||
const Chunk *chunks;
|
||||
const int64_t *chunk_ids;
|
||||
const int64_t *chunk_sizes_cumulative;
|
||||
const int64_t *cumulative_chunk_sizes;
|
||||
RawChunkIterator begin_it;
|
||||
RawChunkIterator end_it;
|
||||
};
|
||||
|
@ -320,7 +320,7 @@ inline IndexMask::IndexMask()
|
|||
data_.chunks_num = 0;
|
||||
data_.chunks = nullptr;
|
||||
data_.chunk_ids = nullptr;
|
||||
data_.chunk_sizes_cumulative = cumulative_sizes_for_empty_mask;
|
||||
data_.cumulative_chunk_sizes = cumulative_sizes_for_empty_mask;
|
||||
data_.begin_it.segment_i = 0;
|
||||
data_.begin_it.index_in_segment = 0;
|
||||
data_.end_it.segment_i = 0;
|
||||
|
@ -349,7 +349,7 @@ inline IndexMask::IndexMask(const IndexRange range)
|
|||
data_.indices_num = range.size();
|
||||
data_.chunks -= first_chunk_id;
|
||||
data_.chunk_ids -= first_chunk_id;
|
||||
data_.chunk_sizes_cumulative -= first_chunk_id;
|
||||
data_.cumulative_chunk_sizes -= first_chunk_id;
|
||||
data_.begin_it.segment_i = 0;
|
||||
data_.begin_it.index_in_segment = range.first() & chunk_mask_low;
|
||||
data_.end_it.segment_i = 0;
|
||||
|
@ -363,7 +363,7 @@ inline int64_t IndexMask::size() const
|
|||
|
||||
inline OffsetIndices<int64_t> IndexMask::chunk_offsets() const
|
||||
{
|
||||
return Span<int64_t>(data_.chunk_sizes_cumulative, data_.chunks_num + 1);
|
||||
return Span<int64_t>(data_.cumulative_chunk_sizes, data_.chunks_num + 1);
|
||||
}
|
||||
|
||||
inline RawMaskIterator IndexMask::index_to_iterator(const int64_t index) const
|
||||
|
@ -373,7 +373,7 @@ inline RawMaskIterator IndexMask::index_to_iterator(const int64_t index) const
|
|||
RawMaskIterator it;
|
||||
const int16_t begin_index = data_.chunks[0].iterator_to_index(data_.begin_it);
|
||||
it.chunk_i = this->chunk_offsets().find_range_index(index + begin_index +
|
||||
data_.chunk_sizes_cumulative[0]);
|
||||
data_.cumulative_chunk_sizes[0]);
|
||||
const Chunk &chunk = data_.chunks[it.chunk_i];
|
||||
it.chunk_it = chunk.index_to_iterator((index + begin_index) & chunk_mask_low);
|
||||
return it;
|
||||
|
@ -384,7 +384,7 @@ inline int64_t IndexMask::iterator_to_index(const RawMaskIterator &it) const
|
|||
BLI_assert(it.chunk_i >= 0);
|
||||
BLI_assert(it.chunk_i < data_.chunks_num);
|
||||
const int16_t begin_index = data_.chunks[0].iterator_to_index(data_.begin_it);
|
||||
return data_.chunk_sizes_cumulative[it.chunk_i] - data_.chunk_sizes_cumulative[0] - begin_index;
|
||||
return data_.cumulative_chunk_sizes[it.chunk_i] - data_.cumulative_chunk_sizes[0] - begin_index;
|
||||
}
|
||||
|
||||
inline IndexMask IndexMask::slice(const IndexRange range) const
|
||||
|
@ -400,7 +400,7 @@ inline IndexMask IndexMask::slice(const IndexRange range) const
|
|||
sliced.data_.indices_num = range.size();
|
||||
sliced.data_.chunks = data_.chunks + first_it.chunk_i;
|
||||
sliced.data_.chunk_ids = data_.chunk_ids + first_it.chunk_i;
|
||||
sliced.data_.chunk_sizes_cumulative = data_.chunk_sizes_cumulative + first_it.chunk_i;
|
||||
sliced.data_.cumulative_chunk_sizes = data_.cumulative_chunk_sizes + first_it.chunk_i;
|
||||
sliced.data_.begin_it = first_it.chunk_it;
|
||||
sliced.data_.end_it.segment_i = last_it.chunk_it.segment_i;
|
||||
sliced.data_.end_it.index_in_segment = last_it.chunk_it.index_in_segment + 1;
|
||||
|
|
|
@ -30,7 +30,7 @@ const IndexMask &get_static_index_mask_for_min_size(const int64_t min_size)
|
|||
static IndexMask static_mask = []() {
|
||||
static Array<Chunk> chunks_array(chunks_num);
|
||||
static Array<int64_t> chunk_ids_array(chunks_num);
|
||||
static Array<int64_t> chunk_sizes_cumulative(chunks_num + 1);
|
||||
static Array<int64_t> cumulative_chunk_sizes(chunks_num + 1);
|
||||
|
||||
static const int16_t *static_offsets = get_static_indices_array().data();
|
||||
static const int16_t static_cumulative_segment_sizes[2] = {0, chunk_capacity};
|
||||
|
@ -43,10 +43,10 @@ const IndexMask &get_static_index_mask_for_min_size(const int64_t min_size)
|
|||
chunk.cumulative_segment_sizes = static_cumulative_segment_sizes;
|
||||
|
||||
chunk_ids_array[i] = i;
|
||||
chunk_sizes_cumulative[i] = i * chunk_capacity;
|
||||
cumulative_chunk_sizes[i] = i * chunk_capacity;
|
||||
}
|
||||
});
|
||||
chunk_sizes_cumulative.last() = max_size;
|
||||
cumulative_chunk_sizes.last() = max_size;
|
||||
|
||||
IndexMask mask;
|
||||
IndexMaskData &mask_data = mask.data_for_inplace_construction();
|
||||
|
@ -54,7 +54,7 @@ const IndexMask &get_static_index_mask_for_min_size(const int64_t min_size)
|
|||
mask_data.indices_num = max_size;
|
||||
mask_data.chunks = chunks_array.data();
|
||||
mask_data.chunk_ids = chunk_ids_array.data();
|
||||
mask_data.chunk_sizes_cumulative = chunk_sizes_cumulative.data();
|
||||
mask_data.cumulative_chunk_sizes = cumulative_chunk_sizes.data();
|
||||
mask_data.begin_it.segment_i = 0;
|
||||
mask_data.begin_it.index_in_segment = 0;
|
||||
mask_data.end_it.segment_i = 0;
|
||||
|
@ -275,14 +275,14 @@ template<typename T> IndexMask to_index_mask(const Span<T> indices, ResourceScop
|
|||
}
|
||||
});
|
||||
|
||||
MutableSpan<int64_t> chunk_sizes_cumulative = main_allocator.allocate_array<int64_t>(chunks_num +
|
||||
MutableSpan<int64_t> cumulative_chunk_sizes = main_allocator.allocate_array<int64_t>(chunks_num +
|
||||
1);
|
||||
int64_t cumulative_size = 0;
|
||||
for (const int64_t i : chunks.index_range()) {
|
||||
chunk_sizes_cumulative[i] = cumulative_size;
|
||||
cumulative_chunk_sizes[i] = cumulative_size;
|
||||
cumulative_size += chunks[i].size();
|
||||
}
|
||||
chunk_sizes_cumulative.last() = cumulative_size;
|
||||
cumulative_chunk_sizes.last() = cumulative_size;
|
||||
|
||||
IndexMask mask;
|
||||
IndexMaskData &mask_data = mask.data_for_inplace_construction();
|
||||
|
@ -290,7 +290,7 @@ template<typename T> IndexMask to_index_mask(const Span<T> indices, ResourceScop
|
|||
mask_data.indices_num = indices.size();
|
||||
mask_data.chunks = chunks.data();
|
||||
mask_data.chunk_ids = chunk_ids.data();
|
||||
mask_data.chunk_sizes_cumulative = chunk_sizes_cumulative.data();
|
||||
mask_data.cumulative_chunk_sizes = cumulative_chunk_sizes.data();
|
||||
mask_data.begin_it = {0, 0};
|
||||
mask_data.end_it = chunks.last().end_iterator();
|
||||
return mask;
|
||||
|
|
Loading…
Reference in New Issue