RNA: Speed up raw array access when the property is stored contiguously #116015

Merged
Brecht Van Lommel merged 5 commits from Mysteryem/blender:rna_raw_access_contiguous_raw_array_copy into main 2024-01-15 17:17:12 +01:00
Member

Recent and ongoing efforts have changed many properties to be stored
contiguously in memory, e.g. mesh attributes. This patch updates
rna_raw_access to make use of this and copy the entire contiguous block
of memory when the property is stored contiguously.

This is faster and scales much better with larger arrays.

In Python, using foreach_get/foreach_set with the value property of a
'FACE' domain 'INT' type mesh attribute:

Using a mesh with 98304 faces:

  • foreach_get
    • Python list
      • Before: ~1760μs
      • After: ~1530μs (1.15x faster)
    • Compatible buffer
      • Before: ~230μs
      • After: ~8μs (28.8x faster)
  • foreach_set
    • Python list
      • Before: ~970μs
      • After: ~770μs (1.26x faster)
    • Compatible buffer
      • Before: ~200μs
      • After: ~8μs (25.0x faster)

Using a mesh with 384 faces:

  • foreach_get
    • Python list
      • Before: ~7.35μs
      • After: ~6.40μs (1.15x faster)
    • Compatible buffer
      • Before: ~1.49μs
      • After: ~0.55μs (2.71x faster)
  • foreach_set
    • Python list
      • Before: ~4.43μs
      • After: ~3.57μs (1.24x faster)
    • Compatible buffer
      • Before: ~1.44μs
      • After: ~0.57μs (2.53x faster)

I chose to move the a variable into the loop in the second conditional branch because the a variable is not used by the first conditional branch.

Recent and ongoing efforts have changed many properties to be stored contiguously in memory, e.g. mesh attributes. This patch updates rna_raw_access to make use of this and copy the entire contiguous block of memory when the property is stored contiguously. This is faster and scales much better with larger arrays. In Python, using foreach_get/foreach_set with the `value` property of a `'FACE'` domain `'INT'` type mesh attribute: Using a mesh with 98304 faces: * `foreach_get` * Python list * Before: ~1760μs * After: ~1530μs (1.15x faster) * Compatible buffer * Before: ~230μs * After: ~8μs (28.8x faster) * `foreach_set` * Python list * Before: ~970μs * After: ~770μs (1.26x faster) * Compatible buffer * Before: ~200μs * After: ~8μs (25.0x faster) Using a mesh with 384 faces: * `foreach_get` * Python list * Before: ~7.35μs * After: ~6.40μs (1.15x faster) * Compatible buffer * Before: ~1.49μs * After: ~0.55μs (2.71x faster) * `foreach_set` * Python list * Before: ~4.43μs * After: ~3.57μs (1.24x faster) * Compatible buffer * Before: ~1.44μs * After: ~0.57μs (2.53x faster) --- I chose to move the `a` variable into the loop in the second conditional branch because the `a` variable is not used by the first conditional branch.
Thomas Barlow added 1 commit 2023-12-11 01:23:37 +01:00
26c59424a5 RNA: Speed up raw array access when the property is stored contiguously
Recent and ongoing efforts have changed many properties to be stored
contiguously in memory, e.g. mesh attributes. This patch updates
rna_raw_access to make use of this and copy the entire contiguous block
of memory when the property is stored contiguously.

This is faster and scales much better with larger arrays.

Using foreach_get/foreach_set with the `value` property of a `'FACE'`
domain `'INT'` attribute:

Using a mesh with 98304 faces:

foreach_get with a compatible buffer object:
- Before: ~230μs
- After: ~8μs

foreach_get with a Python list:
- Before: ~1760μs
- After: ~1530μs

foreach_set with a compatible buffer object:
- Before: ~200μs
- After: ~8μs

foreach_set with a Python list:
- Before: ~970μs
- After: ~770μs

Using a mesh with 384 faces:

foreach_get with a compatible buffer object:
- Before: ~1.49μs
- After: ~0.55μs

foreach_get with a Python list:
- Before: ~7.35μs
- After: ~6.40μs

foreach_set with a compatible buffer object:
- Before: ~1.44μs
- After: ~0.57μs

foreach_set with a Python list:
- Before: ~4.43μs
- After: ~3.57μs
Thomas Barlow reviewed 2023-12-23 18:08:48 +01:00
@ -4608,2 +4608,3 @@
/* The property is stored contiguously so the entire array can be copied at once. */
if (set) {
memcpy(outp, inp, size);
memcpy(outp, inp, size * out.len);
Author
Member

Might there be concerns of integer overflow when doing size * out.len? Both are int, so I guess passing size_t(size) * out.len to memcpy or declaring size_t size; instead of int size; would be better.

Might there be concerns of integer overflow when doing `size * out.len`? Both are `int`, so I guess passing `size_t(size) * out.len` to `memcpy` or declaring `size_t size;` instead of `int size;` would be better.
Member

size_t size sounds like a nice simple solution

`size_t size` sounds like a nice simple solution
Mysteryem marked this conversation as resolved
Hans Goudey reviewed 2023-12-30 02:42:31 +01:00
Hans Goudey left a comment
Member

@blender-bot build

@blender-bot build
Thomas Barlow added 2 commits 2023-12-30 05:41:20 +01:00
43b0b8bf43 Avoid numeric overflow in the number of bytes to copy
Both `size` and `out.len` were `int` type. By making `size` `size_t`
type, this should avoid the possibility of numeric overflow when
multiplying both together.
Brecht Van Lommel requested changes 2024-01-15 11:28:51 +01:00
@ -4650,3 +4650,3 @@
void *inp = in.array;
void *outp = out.array;
int a, size;
size_t size;

I don't think this is enough to avoid integer overflow?

RNA_raw_type_sizeof and arraylen are still int, at least one of those needs to be cast to size_t before multiplying.

I don't think this is enough to avoid integer overflow? `RNA_raw_type_sizeof` and `arraylen` are still `int`, at least one of those needs to be cast to `size_t` before multiplying.
Author
Member

I've updated the existing code so that RNA_raw_type_sizeof returns a size_t. This should also fix possible numeric overflow in the PyMem_Malloc calls in bpy_rna.cc#foreach_getset.

I've updated the existing code so that `RNA_raw_type_sizeof` returns a `size_t`. This should also fix possible numeric overflow in the `PyMem_Malloc` calls in `bpy_rna.cc#foreach_getset`.
Thomas Barlow added 2 commits 2024-01-15 15:51:15 +01:00
buildbot/vexp-code-patch-lint Build done. Details
buildbot/vexp-code-patch-linux-x86_64 Build done. Details
buildbot/vexp-code-patch-darwin-arm64 Build done. Details
buildbot/vexp-code-patch-windows-amd64 Build done. Details
buildbot/vexp-code-patch-darwin-x86_64 Build done. Details
buildbot/vexp-code-patch-coordinator Build done. Details
97cf9c7924
Change RNA_raw_type_sizeof to return size_t
Avoids numeric overflow in some of the existing code.

bpy_rna.cc#foreach_parse_args has been updated to take a `size_t*`
argument since it was returning the result of #RNA_raw_type_sizeof. Its
use in bpy_rna.cc#foreach_getset has been updated to store the result in
a `size_t`.
Brecht Van Lommel approved these changes 2024-01-15 16:03:55 +01:00

@blender-bot build

@blender-bot build
Brecht Van Lommel merged commit fdb56c4e8d into main 2024-01-15 17:17:12 +01:00
Brecht Van Lommel deleted branch rna_raw_access_contiguous_raw_array_copy 2024-01-15 17:17:14 +01: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
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#116015
No description provided.