Regression: Creating material node links in python is VERY slow in v3.1 #97375

Closed
opened 2022-04-16 02:00:59 +02:00 by Netherby · 19 comments

System Information
Operating system: Windows-10-10.0.19043-SP0 64 Bits
Graphics card: NVIDIA GeForce GTX 1070/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 496.49

Blender Version
Broken: version: 3.1.0, branch: master, commit date: 2022-03-08 18:16, hash: c77597cd0e

Regression is caused by 7e712b2d6a

Short description of error
Node trees for materials and other can be edited and created in python by using the node_tree.nodes and node_tree.links data sets. Creating new links via node_tree.links.new() function call in python is now 7000x times slower than in previous versions of Blender. This makes programmatically creating complicated materials with many node links practically impossible as it takes far too long to ever create them (it is probably actually faster to do so by hand through the UI?!).

Exact steps for others to reproduce the error
Simply use the python interface to create a link between two nodes via node_tree.links.new() and compare time it takes with other versions.

**System Information** Operating system: Windows-10-10.0.19043-SP0 64 Bits Graphics card: NVIDIA GeForce GTX 1070/PCIe/SSE2 NVIDIA Corporation 4.5.0 NVIDIA 496.49 **Blender Version** Broken: version: 3.1.0, branch: master, commit date: 2022-03-08 18:16, hash: `c77597cd0e` Regression is caused by 7e712b2d6a **Short description of error** Node trees for materials and other can be edited and created in python by using the node_tree.nodes and node_tree.links data sets. Creating new links via node_tree.links.new() function call in python is now 7000x times slower than in previous versions of Blender. This makes programmatically creating complicated materials with many node links practically impossible as it takes far too long to ever create them (it is probably actually faster to do so by hand through the UI?!). **Exact steps for others to reproduce the error** Simply use the python interface to create a link between two nodes via node_tree.links.new() and compare time it takes with other versions.
Author

Added subscriber: @JerahmyPocott

Added subscriber: @JerahmyPocott
Member

Added subscriber: @HooglyBoogly

Added subscriber: @HooglyBoogly
Member

Changed status from 'Needs Triage' to: 'Needs User Info'

Changed status from 'Needs Triage' to: 'Needs User Info'
Member

Thanks for the report. Could you upload a simple test file with the script you described? It's helpful to have an exact file to make talking about things easier.

Thanks for the report. Could you upload a simple test file with the script you described? It's helpful to have an exact file to make talking about things easier.
Author

'Simple' is a bit of a problem, since it has to be a reasonably complex node tree for it to become noticeable.

In simple tests 3.1 actually handles larger node trees faster than 3.0 by a significant margin.

But when I use an actual production shader being run through this script which in this case will create 1015 links, Blender 3.1 takes 4 MINUTES to complete the links, while Blender 3.0 takes 15 seconds doing the exact same task.

This leads me to think it must be incorrectly recompiling the shader between linking or some similarly time consuming task.

I've written a simple test script that will trigger the problem when run with the right material (but do you really want an 800 node material that triggers the problem? because thats what I've got):

import bpy
from datetime import datetime

# set to material name
material = 'mat name'
loops = 1
num_nodes = 500

# Create a bunch of mix nodes or something
mat = bpy.data.materials[material]
mat.use_nodes = True
nodes = mat.node_tree.nodes
links = mat.node_tree.links
for iter in range(loops):
    mix_nodes = []
    link_pairs = []
    start = datetime.now()
    for i in range(num_nodes):
        mix_nodes.append(nodes.new('ShaderNodeMixRGB'))
    # Link the node pairs
    for i in range(num_nodes-2):
        src1 = mix_nodes[i].outputs['Color']
        src2 = mix_nodes[i+1].outputs['Color']
        dst1 = mix_nodes[i+2].inputs['Color1']
        dst2 = mix_nodes[i+2].inputs['Color2']
        link_pairs.append([dst1, src1])
        link_pairs.append([src2, dst2])
    for pair in link_pairs:
        links.new(pair[0], pair[1])
    print("Creating and linking %s nodes with %s links, iteration %s of %s took: %s" % (num_nodes, len(link_pairs), iter+1, loops, datetime.now() - start))
'Simple' is a bit of a problem, since it has to be a reasonably complex node tree for it to become noticeable. In simple tests 3.1 actually handles larger node trees faster than 3.0 by a significant margin. But when I use an actual production shader being run through this script which in this case will create 1015 links, Blender 3.1 takes 4 MINUTES to complete the links, while Blender 3.0 takes 15 seconds doing the exact same task. This leads me to think it must be incorrectly recompiling the shader between linking or some similarly time consuming task. I've written a simple test script that will trigger the problem when run with the right material (but do you really want an 800 node material that triggers the problem? because thats what I've got): ``` import bpy from datetime import datetime # set to material name material = 'mat name' loops = 1 num_nodes = 500 # Create a bunch of mix nodes or something mat = bpy.data.materials[material] mat.use_nodes = True nodes = mat.node_tree.nodes links = mat.node_tree.links for iter in range(loops): mix_nodes = [] link_pairs = [] start = datetime.now() for i in range(num_nodes): mix_nodes.append(nodes.new('ShaderNodeMixRGB')) # Link the node pairs for i in range(num_nodes-2): src1 = mix_nodes[i].outputs['Color'] src2 = mix_nodes[i+1].outputs['Color'] dst1 = mix_nodes[i+2].inputs['Color1'] dst2 = mix_nodes[i+2].inputs['Color2'] link_pairs.append([dst1, src1]) link_pairs.append([src2, dst2]) for pair in link_pairs: links.new(pair[0], pair[1]) print("Creating and linking %s nodes with %s links, iteration %s of %s took: %s" % (num_nodes, len(link_pairs), iter+1, loops, datetime.now() - start)) ```
Author

linknodetest.blend
I've packaged the script along with a material into a blend file. The material is just some nodes I culled from a much larger shader, but it demonstrates the performance issue even though it doesn't do anything. When testing this on my machine the scirpt takes around 5 seconds to complete on Blender 3.1, vs a few ms to complete on Blender 3.0.

The performance difference quickly scales up if you add more nodes, the full shader takes over 4 minutes, vs 15 seconds.

[linknodetest.blend](https://archive.blender.org/developer/F13004565/linknodetest.blend) I've packaged the script along with a material into a blend file. The material is just some nodes I culled from a much larger shader, but it demonstrates the performance issue even though it doesn't do anything. When testing this on my machine the scirpt takes around 5 seconds to complete on Blender 3.1, vs a few ms to complete on Blender 3.0. The performance difference quickly scales up if you add more nodes, the full shader takes over 4 minutes, vs 15 seconds.
Member

Changed status from 'Needs User Info' to: 'Confirmed'

Changed status from 'Needs User Info' to: 'Confirmed'
Member

Thanks for the details. I can confirm the subpar performance.

Thanks for the details. I can confirm the subpar performance.
Member

Added subscribers: @JacquesLucke, @lichtwerk

Added subscribers: @JacquesLucke, @lichtwerk
Member

Regression is caused by 7e712b2d6a

Will raise prio since it is a recent regression, feel free to lower if this does not feel appropriate.

CC @JacquesLucke

Regression is caused by 7e712b2d6a Will raise prio since it is a recent regression, feel free to lower if this does not feel appropriate. CC @JacquesLucke
Philipp Oeser changed title from Creating material node links in python is VERY slow in v3.1 to Regression: Creating material node links in python is VERY slow in v3.1 2022-04-27 14:26:35 +02:00
Member

While there are certainly ways to reduce the constant overhead a bit (by e.g. computing create_identifier_map lazily), the real problem here is that the entire node tree update function has to be run after each added link. This is an algorithmic complexity problem. The code would probably run pretty much in an instant if we had a method in python that would allow adding multiple links in one call.

While there are certainly ways to reduce the constant overhead a bit (by e.g. computing `create_identifier_map` lazily), the real problem here is that the entire node tree update function has to be run after each added link. This is an algorithmic complexity problem. The code would probably run pretty much in an instant if we had a method in python that would allow adding multiple links in one call.
Member

Added subscriber: @LazyDodo

Added subscriber: @LazyDodo
Member

This issue existed in different forms for a long long time, I tried adding a batch update that just suppressed updates in D2691 which brought one of my scripts down from 20 minutes to a few seconds, but that didn't make it through review, I would expect a single call to have even better performance since it's not tripping in between python and C for every node you add.

This issue existed in different forms for a long long time, I tried adding a batch update that just suppressed updates in [D2691](https://archive.blender.org/developer/D2691) which brought one of my scripts down from 20 minutes to a few seconds, but that didn't make it through review, I would expect a single call to have even better performance since it's not tripping in between python and C for every node you add.
Member

OK, OK, so this was never particularly fast, but it got 7000x times slower in 7e712b2d6a...

OK, OK, so this was never particularly fast, but it got 7000x times slower in 7e712b2d6a...
Member

no-ones arguing that, the point I was trying to make was : the proposed solution will likely have satisfactory performance

no-ones arguing that, the point I was trying to make was : the proposed solution will likely have satisfactory performance
Author

While previous performance wasn't great, it was mostly acceptable. So, in lieu of a better solution returning to previous performance levels would be satisfactory. Yes it still takes 30s to do what should really take 30ms, but that is still a lot better than taking 30 minutes.

While previous performance wasn't great, it was mostly acceptable. So, in lieu of a better solution returning to previous performance levels would be satisfactory. Yes it still takes 30s to do what should really take 30ms, but that is still a lot better than taking 30 minutes.
Jacques Lucke self-assigned this 2022-05-04 14:20:42 +02:00

This issue was referenced by 54b293237e

This issue was referenced by 54b293237ea92f29aef05268836cab54c420d7ad
Member

Changed status from 'Confirmed' to: 'Resolved'

Changed status from 'Confirmed' to: 'Resolved'
Member

54b293237e makes it quite a bit faster for me. Might still be slower than before but maybe it's usable enough for your use case. If it's still wayyyy to slow, please make a new report with a new test file that allows us to reproduce the issue easily.

54b293237e makes it quite a bit faster for me. Might still be slower than before but maybe it's usable enough for your use case. If it's still wayyyy to slow, please make a new report with a new test file that allows us to reproduce the issue easily.
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
6 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#97375
No description provided.