PyAPI: Speed up Mesh.edge_keys using foreach_get and NumPy #114549

Open
Thomas Barlow wants to merge 2 commits from Mysteryem/blender:speed_up_mesh_edge_keys into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
Member
  1. Get edge vertex indices into a compatible buffer with foreach_get.
    1. The .edge_verts attribute has raw array access, so data can be
      memcpy-ed out of the internal array instead of iterated.
    2. A compatible buffer object is used so the data can be memcpy-ed
      into the buffer instead of iterated.
  2. Use NumPy to sort the vertex indices of each edge.
  3. Convert to a list of 2-element tuples to maintain API compatibility.

Time per call for me for meshes with:

  • 12 edges:
    • Before: ~0.0080ms
    • After: ~0.0071ms
    • ~1.1 times faster
  • 768 edges:
    • Before: ~0.48ms
    • After: ~0.037ms
    • ~12.9 times faster
  • 49152 edges:
    • Before: ~34.0ms
    • After: ~4.2ms
    • ~8.2 times faster
  • 786432 edges:
    • Before: ~572ms
    • After: ~90ms
    • ~6.4 times faster

Initially I tried converting to a list of tuples by viewing as a tuple-like structured dtype and then calling np.ndarray.tolist(), but surprisingly that was slower than using struct.iter_unpack.
Using return edge_verts_flat.view(dtype=[('', edge_verts_flat.dtype)] * 2).tolist() took ~110ms for 786432 edges (~20ms difference).

The choice of sorting using 'heapsort' over the default of 'quicksort' only saves about 2.5ms in the 786432 edges case, so is not very important.

Results from a second profiling with more meshes:
image

1. Get edge vertex indices into a compatible buffer with foreach_get. 1. The .edge_verts attribute has raw array access, so data can be memcpy-ed out of the internal array instead of iterated. 2. A compatible buffer object is used so the data can be memcpy-ed into the buffer instead of iterated. 2. Use NumPy to sort the vertex indices of each edge. 3. Convert to a list of 2-element tuples to maintain API compatibility. Time per call for me for meshes with: - 12 edges: - Before: ~0.0080ms - After: ~0.0071ms - ~1.1 times faster - 768 edges: - Before: ~0.48ms - After: ~0.037ms - ~12.9 times faster - 49152 edges: - Before: ~34.0ms - After: ~4.2ms - ~8.2 times faster - 786432 edges: - Before: ~572ms - After: ~90ms - ~6.4 times faster --- Initially I tried converting to a list of tuples by viewing as a tuple-like structured dtype and then calling `np.ndarray.tolist()`, but surprisingly that was slower than using `struct.iter_unpack`. Using `return edge_verts_flat.view(dtype=[('', edge_verts_flat.dtype)] * 2).tolist()` took ~110ms for 786432 edges (~20ms difference). The choice of sorting using `'heapsort'` over the default of `'quicksort'` only saves about 2.5ms in the 786432 edges case, so is not very important. Results from a second profiling with more meshes: ![image](/attachments/31acb478-8179-4405-bd2f-d446d7d8c82d)
Thomas Barlow added 1 commit 2023-11-06 19:10:26 +01:00
1. Get the edge vertex indices into a compatible buffer with foreach_get.
  a. The .edge_verts attribute has raw array access, so data can be
  memcpy-ed out of the internal array instead of iterated.
  b. A compatible buffer object is used so the data can be memcpy-ed into
  the buffer instead of iterated.
2. Use NumPy to sort the vertex indices of each edge and then convert to
a list of 2-element tuples to maintain API compatibility.

Time per call for me for meshes with:
- 12 edges:
  - Before: ~0.0080ms
  - After: ~0.0071ms
  - After/Before: ~89%
- 768 edges:
  - Before: ~0.48ms
  - After: ~0.037ms
  - After/Before: ~8%
- 49152 edges:
  - Before: ~34.0ms
  - After: ~4.2ms
  - After/Before: ~12%
- 786432 edges:
  - Before: ~572ms
  - After: ~90ms
  - After/Before: ~16%
Hans Goudey reviewed 2023-11-06 22:50:35 +01:00
Hans Goudey left a comment
Member

I have no experience with the Python side of things. Just one small comment. Very nice speedup though!

Meta note: I think it's usually better to give speedups in terms of X times faster. Dediding after/before and giving the result as a percentage is a bit confusing. At first it seems like "8% faster" or something.

I have no experience with the Python side of things. Just one small comment. Very nice speedup though! Meta note: I think it's usually better to give speedups in terms of X times faster. Dediding after/before and giving the result as a percentage is a bit confusing. At first it seems like "8% faster" or something.
@ -645,0 +644,4 @@
import numpy as np
from struct import iter_unpack
# Get the vertex indices of each edge from the ".edge_verts" attribute.
edge_verts_data = _name_convention_attribute_ensure(self.attributes, ".edge_verts", 'EDGE', 'INT32_2D').data
Member

I think _name_convention_attribute_get is probably better, this function shouldn't modify the mesh.

I think `_name_convention_attribute_get` is probably better, this function shouldn't modify the mesh.
Author
Member

Thanks. I realised the functional change to new meshes after submitting the PR, but wasn't sure if it mattered in this case due to ".edge_verts" being an attribute that can't be removed.

Thanks. I realised the functional change to new meshes after submitting the PR, but wasn't sure if it mattered in this case due to ".edge_verts" being an attribute that can't be removed.
Thomas Barlow added 1 commit 2023-11-07 04:07:06 +01:00
This prevents creating the ".edge_verts" attribute on meshes that don't
already have it.

An empty list is returned when the ".edge_verts" attribute doesn't
exist.
Hans Goudey added the
Module
Python API
label 2023-11-13 10:50:50 +01:00
Merge conflict checking is in progress. Try again in few moments.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u speed_up_mesh_edge_keys:Mysteryem-speed_up_mesh_edge_keys
git checkout Mysteryem-speed_up_mesh_edge_keys
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
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
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
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#114549
No description provided.