BMesh: Respect data alignment for attributes #109237

Open
Joseph Eagar wants to merge 8 commits from JosephEagar/blender:temp-customdata-aligned-offsets into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
Member

This PR modifies customData_update_offsets to respect
memory alignment when laying out BMesh attribute
blocks. While most consumer CPUs (and all x86s) support
nonaligned memory access it can lead to performance
degradation.

The alignment process is simple; large attributes
are laid out first and then small ones, and padding
bytes are added as necessary. Common vector types
are assumed to align to their base type (e.g.
CD_PROP_FLOAT3 has an alignment of 4).

In the future we should probably allocate BMesh attributes
separately in some kind of shared memory pool collection.
But for now this should improve performance.

This PR modifies `customData_update_offsets` to respect memory alignment when laying out BMesh attribute blocks. While most consumer CPUs (and all x86s) support nonaligned memory access it can lead to performance degradation. The alignment process is simple; large attributes are laid out first and then small ones, and padding bytes are added as necessary. Common vector types are assumed to align to their base type (e.g. CD_PROP_FLOAT3 has an alignment of 4). In the future we should probably allocate BMesh attributes separately in some kind of shared memory pool collection. But for now this should improve performance.
Joseph Eagar added 1 commit 2023-06-22 12:55:03 +02:00
This PR modifes customData_update_offsets to respect
memory alignment when laying out BMesh attribute
blocks. While most consumer CPUs (and all x86s) support
nonaligned memory access it can lead to performance
degredation.

The alignment process is simple; large attributes
are layed out first and then small ones, and padding
bytes are added as necessary.  Common vector types
are assumed to align to their base type (e.g.
CD_PROP_FLOAT3 has an alignment of 4).

In the future we should probably allocate BMesh attributes
seperately in some kind of shared memory pool collection.
But for now this should improve performance.
Hans Goudey reviewed 2023-06-22 13:19:12 +02:00
Hans Goudey left a comment
Member

If the goal here is improve performance, the PR should probably include some numbers to show the impact. Other than that the code looks reasonable, though now the function does get slower as we add more custom data types.

If the goal here is improve performance, the PR should probably include some numbers to show the impact. Other than that the code looks reasonable, though now the function does get slower as we add more custom data types.
@ -61,6 +62,7 @@
/* only for customdata_data_transfer_interp_normal_normals */
#include "data_transfer_intern.h"
using blender::Array;
Member

Unnecessary change

Unnecessary change
JosephEagar marked this conversation as resolved
@ -2541,1 +2543,4 @@
static int customData_get_alignment(eCustomDataType type)
{
/* Handle array types. */
Member

This comment isn't quite right, since CD_PROP_BYTE_COLOR is also an array type. Maybe "Handle float array types"?

This comment isn't quite right, since `CD_PROP_BYTE_COLOR` is also an array type. Maybe "Handle float array types"?
Author
Member

I has an if statement for that. I admit the comment isn't clear, it applies to the three if statements (for 4,2,1 alignments).

I has an if statement for that. I admit the comment isn't clear, it applies to the three if statements (for 4,2,1 alignments).
Member

Ah I see. I don't think the comment is necessary honestly

Ah I see. I don't think the comment is necessary honestly
JosephEagar marked this conversation as resolved
@ -2545,0 +2594,4 @@
int max_alignment = 1;
int aligns[] = {8, 4, 2, 1};
Member

std::array should work here, a bit nicer IMO

`std::array` should work here, a bit nicer IMO
JosephEagar marked this conversation as resolved
@ -2548,1 +2600,3 @@
typeInfo = layerType_getInfo(eCustomDataType(data->layers[i].type));
for (int i = 0; i < ARRAY_SIZE(aligns) + 1; i++) {
for (int j = 0; j < data->totlayer; j++) {
CustomDataLayer *layer = data->layers + j;
Member

Declare variables const

Declare variables const
JosephEagar marked this conversation as resolved
Hans Goudey changed title from Core: Respect data alignment for BMesh attributes to BMesh: Respect data alignment for attributes 2023-06-22 13:19:31 +02:00
Joseph Eagar added 1 commit 2023-06-22 13:47:51 +02:00
Joseph Eagar added 1 commit 2023-06-22 13:48:42 +02:00
Joseph Eagar added 1 commit 2023-06-22 13:50:51 +02:00
Joseph Eagar added 1 commit 2023-06-22 13:54:16 +02:00
Joseph Eagar added 1 commit 2023-06-22 13:56:15 +02:00
Joseph Eagar added 1 commit 2023-06-22 13:56:43 +02:00
Joseph Eagar added 1 commit 2023-06-23 08:43:30 +02:00
This pull request has changes conflicting with the target branch.
  • source/blender/blenkernel/intern/customdata.cc

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u temp-customdata-aligned-offsets:JosephEagar-temp-customdata-aligned-offsets
git checkout JosephEagar-temp-customdata-aligned-offsets
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Code Documentation
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
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 & 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
Viewport & EEVEE
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Asset Browser Project
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
Asset System
Module
Asset System
Module
Core
Module
Development Management
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Module
Viewport & EEVEE
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Severity
High
Severity
Low
Severity
Normal
Severity
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#109237
No description provided.