BMesh data copy with many layers is extremelly slow (100x slower) compared to blender 3.6 #115776

Closed
opened 2023-12-05 00:53:02 +01:00 by Mykyta-Petrenko · 3 comments

System Information
Operating system: Windows-10-10.0.19045-SP0 64 Bits
Graphics card: Radeon RX 580 Series ATI Technologies Inc. 4.6.0 Core Profile Context 23.11.1.231017

Blender Version
Broken: version: 4.0.1, branch: blender-v4.0-release, commit date: 2023-11-16 16:40, hash: d0dd92834a08
Worked: (newest version of Blender that worked as expected)

Short description of error
Accessing bmesh.verts for meshes with huge amount of shape keys becomes extremelly slow in all blender 4.0 that I have tested.

Exact steps for others to reproduce the error
Here is a simple script to demonstrate the issue

import bpy
import time
import bmesh


# params to get a sphere with the poly count equivalent to high-res character
segments = 256
rign_count = 128
n_keys = 500


def benchmark():
    t1 = time.time()
    # Create a sphere with 
    bpy.ops.mesh.primitive_uv_sphere_add(segments=segments, ring_count=rign_count)

    # Set the created sphere as the active object
    obj = bpy.context.active_object
    
    # Add shape keys to the sphere
    for i in range(n_keys):
        obj.shape_key_add(name=f'Key{i}', from_mix=False)

    # Create a bmesh for the target object
    bm = bmesh.new()
    bm.from_mesh(obj.data)

    t2 = time.time()
    # ISSUE START
    # dummy loop accessing bmesh.verts
    for i, v in enumerate(bm.verts):
        v.co = v.co * 1.0
    # ISSUE END
    
    loop_time = time.time() - t2

    # Write back to the mesh
    bm.to_mesh(obj.data)
    bm.free()

    # Update the mesh
    obj.data.update()
    
    total_time = time.time() - t1
    
    print(f"Total time: {total_time}")
    print(f"Bmesh loop time: {loop_time}")


if __name__ == "__main__":
    benchmark()
**System Information** Operating system: Windows-10-10.0.19045-SP0 64 Bits Graphics card: Radeon RX 580 Series ATI Technologies Inc. 4.6.0 Core Profile Context 23.11.1.231017 **Blender Version** Broken: version: 4.0.1, branch: blender-v4.0-release, commit date: 2023-11-16 16:40, hash: `d0dd92834a08` Worked: (newest version of Blender that worked as expected) **Short description of error** Accessing bmesh.verts for meshes with huge amount of shape keys becomes extremelly slow in all blender 4.0 that I have tested. **Exact steps for others to reproduce the error** Here is a simple script to demonstrate the issue ```Py import bpy import time import bmesh # params to get a sphere with the poly count equivalent to high-res character segments = 256 rign_count = 128 n_keys = 500 def benchmark(): t1 = time.time() # Create a sphere with bpy.ops.mesh.primitive_uv_sphere_add(segments=segments, ring_count=rign_count) # Set the created sphere as the active object obj = bpy.context.active_object # Add shape keys to the sphere for i in range(n_keys): obj.shape_key_add(name=f'Key{i}', from_mix=False) # Create a bmesh for the target object bm = bmesh.new() bm.from_mesh(obj.data) t2 = time.time() # ISSUE START # dummy loop accessing bmesh.verts for i, v in enumerate(bm.verts): v.co = v.co * 1.0 # ISSUE END loop_time = time.time() - t2 # Write back to the mesh bm.to_mesh(obj.data) bm.free() # Update the mesh obj.data.update() total_time = time.time() - t1 print(f"Total time: {total_time}") print(f"Bmesh loop time: {loop_time}") if __name__ == "__main__": benchmark() ```
Mykyta-Petrenko added the
Priority
Normal
Type
Report
Status
Needs Triage
labels 2023-12-05 00:53:03 +01:00
Jesse Yurkovich added
Module
Modeling
Status
Confirmed
and removed
Status
Needs Triage
labels 2023-12-05 01:02:39 +01:00

Confirmed with a build as far back as August 15th (I have no older builds immediately available). I'll set to high due to the severity of the regression so we can investigate.

Blender 3.6.5
Bmesh loop time: 0.14097070693969727

4.0 Alpha 2023-08-15 5741a5d433
Bmesh loop time: 15.914452314376831

Confirmed with a build as far back as August 15th (I have no older builds immediately available). I'll set to high due to the severity of the regression so we can investigate. Blender 3.6.5 `Bmesh loop time: 0.14097070693969727` 4.0 Alpha 2023-08-15 5741a5d433cd `Bmesh loop time: 15.914452314376831`
Jesse Yurkovich added
Priority
High
and removed
Priority
Normal
labels 2023-12-05 01:03:38 +01:00
Member

Looks like CustomData_bmesh_copy_data_exclude_by_type has quadratic complexity after 9175d9b7c2.

Looks like `CustomData_bmesh_copy_data_exclude_by_type` has quadratic complexity after 9175d9b7c27b191ea94a41871a7049b068f77bd7.
Member

Think it's best to first build a struct describing how the layer data should be copied, like MeshToBMeshLayerInfo, then iterate over all the BMesh elements and actually do the copy. That's a significant refactor, but otherwise everything involving CustomData_bmesh_copy_data_exclude_by_type will be slow.

Think it's best to first build a struct describing how the layer data should be copied, like `MeshToBMeshLayerInfo`, then iterate over all the BMesh elements and actually do the copy. That's a significant refactor, but otherwise everything involving `CustomData_bmesh_copy_data_exclude_by_type` will be slow.
Hans Goudey self-assigned this 2023-12-05 15:51:20 +01:00
Hans Goudey changed title from bmesh becomes extremelly slow (x100) compared to blender 3.6 to BMesh data copy with many layers is extremelly slow (100x slower) compared to blender 3.6 2023-12-05 17:58:55 +01:00
Blender Bot added
Status
Resolved
and removed
Status
Confirmed
labels 2023-12-09 05:38:54 +01:00
Brecht Van Lommel added
Type
Bug
and removed
Type
Report
labels 2023-12-11 16:06:21 +01:00
Sign in to join this conversation.
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#115776
No description provided.