BLI: Improve IndexMask::complement() performance #108331
No reviewers
Labels
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: blender/blender#108331
Loading…
Reference in New Issue
No description provided.
Delete Branch "HooglyBoogly/blender:index-mask-complement-performance"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
IndexMask::complement() is often used in geometry processing
algorithms when a selection needs to be inverted, mostly just in
curves code so far.
Instead of reusing
from_predicate
and lookup in the source mask,scan the mask once, inserting segments between the original indices.
Theoretically this improves the performance from O(N*log(N)) to O(N).
But with the small constant offset of the former, the improvement is
generally just 3-4 times faster. However in some special cases the
new code will take constant time.
@ -333,0 +337,4 @@
static void inverted_indices_to_segments(const IndexMaskSegment segment,
const int64_t range_threshold,
LinearAllocator<> &allocator,
Vector<IndexMaskSegment, 16> &segments)
r_segments
@ -333,0 +361,4 @@
Span<int16_t> indices = segment.base_span();
while (indices.size() > 1) {
const int64_t size_before_gap = unique_sorted_indices::find_size_of_next_range(indices);
Doing this logarithmic range-size-search for potentially every index is not efficient. It may be possible to improve performance of
find_size_of_next_range
for small ranges.I did some experimenting with this and ended up specializing it for single indices. I'm sure there are more possibilities here for the future too!
@ -333,0 +375,4 @@
}
else {
for (const int64_t i : IndexRange(gap_size)) {
add_index(gap_first + int16_t(i));
Add indices "at once" instead of one by one. Essentially increasing
inverted_indices_count
only once.This didn't seem to change the performance, but I did it anyway just in case, it is a bit clearer
@ -333,0 +400,4 @@
if (!this->to_range()) {
const int64_t segments_num = this->segments_num();
ParallelSegmentsCollector segments_collector;
threading::parallel_for(
There should be a separate code path that does not use
EnumerableThreadSpecific
.@ -333,0 +401,4 @@
const int64_t segments_num = this->segments_num();
ParallelSegmentsCollector segments_collector;
threading::parallel_for(
IndexRange(segments_num).drop_back(1), 512, [&](const IndexRange range) {
Processing 512 segments at once is likely too much in practice and causes the algorithm to be single threaded in too many cases. Generally it's hard to find a good grain size with these algorithms here, because the time per segment can vary wildly, but 512 is still too much.
WIP: BLI: Improve IndexMask::complement() performanceto BLI: Improve IndexMask::complement() performanceLooks good. There are a few more cases where the algorithm can become O(1) instead of O(n). Mainly when the output is a single range. In this case no new memory has to be allocated either. Would be nice if you could add some tests for these special cases as well. Might be good to put "fuzzy" into the names of tests that use random numbers and to have some tests that don't rely on random numbers.