BLI: Improve IndexMask::complement() performance #108331

Merged
Hans Goudey merged 15 commits from HooglyBoogly/blender:index-mask-complement-performance into main 2023-05-31 17:11:11 +02:00
Member

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.

image

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. ![image](/attachments/d2f6b0be-f195-4206-9bf4-c0ab20041d1b)
Hans Goudey added 1 commit 2023-05-26 21:23:01 +02:00
339cf787c2 WIP: BLI: Improve IndexMask::complement() performance
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 won't
be that nice.

TODO:
- More performance testing. I didn't see much change in the test code runtime.
Hans Goudey added this to the Core Libraries project 2023-05-26 21:23:39 +02:00
Hans Goudey requested review from Jacques Lucke 2023-05-26 21:23:54 +02:00
Jacques Lucke requested changes 2023-05-27 08:36:22 +02:00
@ -333,0 +337,4 @@
static void inverted_indices_to_segments(const IndexMaskSegment segment,
const int64_t range_threshold,
LinearAllocator<> &allocator,
Vector<IndexMaskSegment, 16> &segments)
Member

r_segments

`r_segments`
HooglyBoogly marked this conversation as resolved
@ -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);
Member

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.

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.
Author
Member

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!

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));
Member

Add indices "at once" instead of one by one. Essentially increasing inverted_indices_count only once.

Add indices "at once" instead of one by one. Essentially increasing ` inverted_indices_count` only once.
Author
Member

This didn't seem to change the performance, but I did it anyway just in case, it is a bit clearer

This didn't seem to change the performance, but I did it anyway just in case, it is a bit clearer
HooglyBoogly marked this conversation as resolved
@ -333,0 +400,4 @@
if (!this->to_range()) {
const int64_t segments_num = this->segments_num();
ParallelSegmentsCollector segments_collector;
threading::parallel_for(
Member

There should be a separate code path that does not use EnumerableThreadSpecific.

There should be a separate code path that does not use `EnumerableThreadSpecific`.
HooglyBoogly marked this conversation as resolved
@ -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) {
Member

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.

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.
HooglyBoogly marked this conversation as resolved
Hans Goudey added 11 commits 2023-05-31 01:19:23 +02:00
Hans Goudey requested review from Jacques Lucke 2023-05-31 01:24:21 +02:00
Hans Goudey changed title from WIP: BLI: Improve IndexMask::complement() performance to BLI: Improve IndexMask::complement() performance 2023-05-31 01:24:33 +02:00
Member

Looks 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.

  if (universe.is_empty()) {
    return {};
  }
  const std::optional<IndexRange> this_range = this->to_range();
  const bool this_is_range = this_range.has_value();
  if (this_is_range) {
    const bool first_in_range = this_range->first() <= universe.first();
    const bool last_in_range = this_range->last() >= universe.last();
    if (first_in_range && last_in_range) {
      /* This mask fills the entire universe, so the complement is empty. */
      return {};
    }
    if (first_in_range) {
      /* This mask is a range that contains the start of the universe. The complement is a range
       * that contains the end of the universe. */
      const int64_t complement_start = this_range->one_after_last();
      const int64_t complement_size = universe.one_after_last() - complement_start;
      return IndexRange(complement_start, complement_size);
    }
    if (last_in_range) {
      /* This mask is a range that contains the end of the universe. The complement is a range
      that
       * contains the start of the universe. */
      const int64_t complement_start = universe.first();
      const int64_t complement_size = this_range->first() - complement_start;
      return IndexRange(complement_start, complement_size);
    }
  }
Looks 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. ```cpp if (universe.is_empty()) { return {}; } const std::optional<IndexRange> this_range = this->to_range(); const bool this_is_range = this_range.has_value(); if (this_is_range) { const bool first_in_range = this_range->first() <= universe.first(); const bool last_in_range = this_range->last() >= universe.last(); if (first_in_range && last_in_range) { /* This mask fills the entire universe, so the complement is empty. */ return {}; } if (first_in_range) { /* This mask is a range that contains the start of the universe. The complement is a range * that contains the end of the universe. */ const int64_t complement_start = this_range->one_after_last(); const int64_t complement_size = universe.one_after_last() - complement_start; return IndexRange(complement_start, complement_size); } if (last_in_range) { /* This mask is a range that contains the end of the universe. The complement is a range that * contains the start of the universe. */ const int64_t complement_start = universe.first(); const int64_t complement_size = this_range->first() - complement_start; return IndexRange(complement_start, complement_size); } } ```
Jacques Lucke approved these changes 2023-05-31 09:28:50 +02:00
Hans Goudey added 3 commits 2023-05-31 17:07:19 +02:00
Hans Goudey merged commit 49b48209e7 into main 2023-05-31 17:11:11 +02:00
Hans Goudey deleted branch index-mask-complement-performance 2023-05-31 17:11:12 +02:00
Sign in to join this conversation.
No reviewers
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
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#108331
No description provided.