PyAPI: Speed up Mesh.shade_flat() using foreach_set #114591

Open
Thomas Barlow wants to merge 1 commits from Mysteryem/blender:speed_up_mesh_shade_flat into main

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

Replace the slower iteration in Python with using foreach_set and a
compatible buffer object filled with True.

For meshes under 24 faces, the new code is slightly slower due to higher
overhead, but the increase in performance as the number of faces
increases should more than make up for this.

NumPy is only used to create a buffer filled with True that is
compatible with foreach_set. The array module does not support the
bool/'?' type, and ctypes arrays are not currently considered
compatible buffers by foreach_set.

No changes, other than performance, are expected with this patch.

Time per call for me for meshes with:

  • 24 faces:
    • Before: ~0.0053ms
    • After: ~0.0042ms
    • ~1.3 times faster
  • 1536 faces:
    • Before: ~0.24ms
    • After: ~0.0044ms
    • ~52 times faster
  • 24576 faces:
    • Before: ~3.79ms
    • After: ~0.0053ms
    • ~719 times faster
  • 393216 faces:
    • Before: ~58.5ms
    • After: ~0.018ms
    • ~3327 times faster

ctypes arrays use byte-order/size/alignment prefixes which foreach_set doesn't support, which would fall back to foreach_set's slower iteration.


For comparison, using a Python list with foreach_get is also many times faster and has less overhead, but is not as fast when the number of faces increases:
Time per call using sharp_faces_data.foreach_set("value", [True] * len(sharp_faces_data)):

  • 24 faces:
    • ~0.0023ms
    • ~2.3 times faster
  • 1536 faces:
    • ~0.018ms
    • ~13.3 times faster
  • 24576 faces:
    • ~0.25ms
    • ~15.2 times faster
  • 393216 faces:
    • ~3.97ms
    • ~14.7 times faster

Graph of speedup for meshes starting at 6 faces and doubling each time up to 1,572,864 faces:
image

Replace the slower iteration in Python with using `foreach_set` and a compatible buffer object filled with `True`. For meshes under 24 faces, the new code is slightly slower due to higher overhead, but the increase in performance as the number of faces increases should more than make up for this. NumPy is only used to create a buffer filled with `True` that is compatible with `foreach_set`. The `array` module does not support the `bool`/`'?'` type, and ctypes arrays are not currently considered compatible buffers by `foreach_set`. No changes, other than performance, are expected with this patch. Time per call for me for meshes with: - 24 faces: - Before: ~0.0053ms - After: ~0.0042ms - ~1.3 times faster - 1536 faces: - Before: ~0.24ms - After: ~0.0044ms - ~52 times faster - 24576 faces: - Before: ~3.79ms - After: ~0.0053ms - ~719 times faster - 393216 faces: - Before: ~58.5ms - After: ~0.018ms - ~3327 times faster --- ctypes arrays use byte-order/size/alignment prefixes which `foreach_set` doesn't support, which would fall back to `foreach_set`'s slower iteration. --- For comparison, using a Python list with `foreach_get` is also many times faster and has less overhead, but is not as fast when the number of faces increases: Time per call using `sharp_faces_data.foreach_set("value", [True] * len(sharp_faces_data))`: - 24 faces: - ~0.0023ms - ~2.3 times faster - 1536 faces: - ~0.018ms - ~13.3 times faster - 24576 faces: - ~0.25ms - ~15.2 times faster - 393216 faces: - ~3.97ms - ~14.7 times faster --- Graph of speedup for meshes starting at 6 faces and doubling each time up to 1,572,864 faces: ![image](/attachments/d6aeedc9-f274-401b-9744-d6e9bea0b79d)
Iliya Katushenock added the
Module
Python API
Interest
Modeling
labels 2023-11-07 18:45:40 +01:00
Thomas Barlow force-pushed speed_up_mesh_shade_flat from 0af98fa117 to b109c5f668 2024-01-27 02:56:39 +01:00 Compare
Author
Member

I've updated the performance comparison following recent performance improvements to foreach_set, and I've rebased on main.

I've updated the performance comparison following recent performance improvements to `foreach_set`, and I've rebased on main.
Author
Member

An alternative that uses struct.pack rather than numpy could be:

import struct
sharp_faces_data = _name_convention_attribute_ensure(self.attributes, "sharp_face", 'FACE', 'BOOLEAN').data
true_buffer = memoryview(struct.pack("?", True) * len(sharp_faces_data)).cast("?")
sharp_faces_data.foreach_set("value", true_buffer)

This is also slightly faster than using numpy, but it doesn't amount to much when the code was already so fast. Notably though, it does have lower overhead, so is faster than the original code for even meshes as small as a default cube.

An alternative that uses `struct.pack` rather than `numpy` could be: ```py import struct sharp_faces_data = _name_convention_attribute_ensure(self.attributes, "sharp_face", 'FACE', 'BOOLEAN').data true_buffer = memoryview(struct.pack("?", True) * len(sharp_faces_data)).cast("?") sharp_faces_data.foreach_set("value", true_buffer) ``` This is also slightly faster than using `numpy`, but it doesn't amount to much when the code was already so fast. Notably though, it does have lower overhead, so is faster than the original code for even meshes as small as a default cube.
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_shade_flat:Mysteryem-speed_up_mesh_shade_flat
git checkout Mysteryem-speed_up_mesh_shade_flat
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
1 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#114591
No description provided.