Geometry Nodes: Avoid index lookup from index mask #109174

Merged
Hans Goudey merged 9 commits from mod_moder/blender:avoid_mask_lookup into main 2023-06-23 19:20:30 +02:00

Recent IndexMask refactoring introduce log(N) complexity for mask[i].
The greater the fragmentation of the mask, the greater the complexity.
Also, new IndexMask implementation have new iterators represented
both real index, and position (index = mask[position]).
This PR simply replace manual loops by new methods for iterating.
Added optimized in some place as slightly speed up.

The Attribute Statistic became 5 times better, but due to multithreading (for hight mesh size).
The Extrude (Faces) (Individual) has avarange changed 42 ms -> 36 ms.
Duplicate Elements (for Faces) has avarange changed 220 ms -> 150 ms.
Transform Instances has avarange changed 12 ms ~> 8 ms.
Other nodes have approximately similar improvement numbers.
All tests use Random(50%) selection as mask.

Recent `IndexMask` refactoring introduce log(N) complexity for `mask[i]`. The greater the fragmentation of the mask, the greater the complexity. Also, new `IndexMask` implementation have new iterators represented both real index, and position (`index = mask[position]`). This PR simply replace manual loops by new methods for iterating. Added `optimized` in some place as slightly speed up. The Attribute Statistic became 5 times better, but due to multithreading (for hight mesh size). The Extrude (Faces) (Individual) has avarange changed 42 ms -> 36 ms. Duplicate Elements (for Faces) has avarange changed 220 ms -> 150 ms. Transform Instances has avarange changed 12 ms ~> 8 ms. Other nodes have approximately similar improvement numbers. All tests use Random(50%) selection as mask.
Iliya Katushenock added 1 commit 2023-06-20 21:35:42 +02:00
Iliya Katushenock changed title from Geometry Nodes: Avoid to lookup index from index mask to Geometry Nodes: Avoid index lookup from index mask 2023-06-20 21:36:01 +02:00
Iliya Katushenock added this to the Nodes & Physics project 2023-06-20 21:41:20 +02:00
Iliya Katushenock added the
Interest
Geometry Nodes
label 2023-06-20 21:41:23 +02:00
Hans Goudey requested changes 2023-06-20 21:57:41 +02:00
Hans Goudey left a comment
Member

Yikes, I didn't realize how much of this was left, thanks!

Yikes, I didn't realize how much of this was left, thanks!
@ -218,3 +218,1 @@
for (const int i : selection.index_range()) {
selected_data[i] = component_data[selection[i]];
}
selection.foreach_index_optimized<int>(GrainSize(2048),
Member

Looks like this is just array_utils::gather

Looks like this is just `array_utils::gather`
Author
Member

This is actually materialize_compressed )

This is actually `materialize_compressed` )
Member

They're the same, but array_utils::gather is multithreaded

They're the same, but `array_utils::gather` is multithreaded
Author
Member

Ah, didn't notice this. It's strange that we can't just have one multi-threaded variant.

Ah, didn't notice this. It's strange that we can't just have one multi-threaded variant.
Member

It makes some sense if multithreading is generally handled outside of the virtual array class I think, it gives the API user a bit better control.

It makes some sense if multithreading is generally handled outside of the virtual array class I think, it gives the API user a bit better control.
mod_moder marked this conversation as resolved
@ -240,3 +229,1 @@
}
}
});
selection.foreach_index(
Member

This looks like it might be attribute_math::gather_group_to_group?

This looks like it might be `attribute_math::gather_group_to_group`?
Author
Member

It seems, gather_group_to_group is static right now?
What do you think about declaring this into utilities?

It seems, `gather_group_to_group` is static right now? What do you think about declaring this into utilities?
Member

Ah good point. Yeah, that could work

Ah good point. Yeah, that could work
Author
Member

Looking at this more closely, I see that the functions are different and not interchangeable.

Looking at this more closely, I see that the functions are different and not interchangeable.
mod_moder marked this conversation as resolved
@ -352,3 +339,1 @@
dst_offsets[i_duplicate] = point_offsets[i_selection].start() +
src_curve_range.size() * i_duplicate;
}
selection.foreach_index_optimized<int>(GrainSize(512), [&](const int index, const int position) {
Member

foreach_index

`foreach_index`
mod_moder marked this conversation as resolved
@ -58,3 +43,1 @@
instance_transform *= math::from_location<float4x4>(-pivot);
instance_transform *= original_transform;
}
selection.foreach_index_optimized<int>(GrainSize(512), [&](const int index) {
Member

foreach_index

`foreach_index`
Author
Member

There is no nested loops, why?

There is no nested loops, why?
Member

The matrix math is not exactly cheap

The matrix math is not exactly cheap
Author
Member

Maybe, but it's not as bad as arbitrary sized data, nested loops, ..., is it?

Maybe, but it's not as bad as arbitrary sized data, nested loops, ..., is it?
Member

Would require some measurement to know for sure! But I would guess that the overhead of the non-optimized version wouldn't be significant in this case.

Would require some measurement to know for sure! But I would guess that the overhead of the non-optimized version wouldn't be significant in this case.
mod_moder marked this conversation as resolved
@ -61,0 +44,4 @@
const float3 pivot = pivots[index];
float4x4 &instance_transform = transforms[index];
if (local_spaces[index]) {
Member

index isn't really a more helpful name than i, it's just longer.

`index` isn't really a more helpful name than `i`, it's just longer.
Author
Member

At least it is more convenient for the subsequent search)
The truth is not sure if one character is a good name.

At least it is more convenient for the subsequent search) The truth is not sure if one character is a good name.
Member

Please just keep the variable name the same, even just to keep the diff smaller

Please just keep the variable name the same, even just to keep the diff smaller
mod_moder marked this conversation as resolved
@ -45,3 +39,1 @@
else {
transforms[i].location() += translations[i];
}
selection.foreach_index_optimized<int>(GrainSize(1024), [&](const int index) {
Member

Unless the loop is really very simple, just stick with the non "optimized" version that doesn't generate as much code.

Unless the loop is really very simple, just stick with the non "optimized" version that doesn't generate as much code.
Author
Member

Seems like a pretty simple loop?

Seems like a pretty simple loop?
Member

Yeah, this one is debatable I guess. Still though, I wonder if it's actually worth it..

Yeah, this one is debatable I guess. Still though, I wonder if it's actually worth it..
mod_moder marked this conversation as resolved
Hans Goudey reviewed 2023-06-20 22:29:07 +02:00
@ -82,3 +81,1 @@
counts.materialize_compressed(selection.slice(range),
r_offset_data.as_mutable_span().slice(range));
});
counts.materialize_compressed(selection, r_offset_data.as_mutable_span());
Member

array_utils here too I think

`array_utils` here too I think
mod_moder marked this conversation as resolved
Iliya Katushenock added 1 commit 2023-06-20 23:14:17 +02:00
Iliya Katushenock added 1 commit 2023-06-20 23:38:18 +02:00
buildbot/vexp-code-patch-coordinator Build done. Details
fc2783c745
progress
Jacques Lucke reviewed 2023-06-21 09:23:22 +02:00
@ -1284,3 +1259,1 @@
else {
connect_data[i_extrude] = bke::attribute_math::mix2(
0.5f, data[orig_edge], data[orig_edge_prev]);
poly_selection.foreach_index_optimized<int>(
Member

How much of a speedup do you get here from using optimized?

How much of a speedup do you get here from using `optimized`?
Author
Member

Haven't done any tests yet. But here, most likely, I simply overlooked the nested loop.

Haven't done any tests yet. But here, most likely, I simply overlooked the nested loop.
mod_moder marked this conversation as resolved
Member

@blender-bot build

@blender-bot build
Iliya Katushenock added 2 commits 2023-06-21 16:20:23 +02:00
Iliya Katushenock requested review from Hans Goudey 2023-06-21 21:06:33 +02:00
Hans Goudey approved these changes 2023-06-21 21:16:42 +02:00
Hans Goudey left a comment
Member

If you have a chance I'd be interested in seeing some performance comparisons. The changes look good to me.

If you have a chance I'd be interested in seeing some performance comparisons. The changes look good to me.
Author
Member

I added some tests result.

I added some tests result.
Iliya Katushenock added 3 commits 2023-06-22 19:00:30 +02:00
Iliya Katushenock added 1 commit 2023-06-22 20:33:59 +02:00
buildbot/vexp-code-patch-coordinator Build done. Details
ed0489306c
progress
Author
Member

It would be useful to run the test, otherwise, I manually randomly checked the speed in several places. Everything seems to be ready.

It would be useful to run the test, otherwise, I manually randomly checked the speed in several places. Everything seems to be ready.
Member

@blender-bot build

@blender-bot build
Hans Goudey merged commit 25c48782e1 into main 2023-06-23 19:20:30 +02:00
Iliya Katushenock deleted branch avoid_mask_lookup 2023-06-23 19:20:46 +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 Assignees
3 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#109174
No description provided.