Repository: godot
Issue: Lag caused by adding mesh to MeshInstance3D (possibly due to the use of threads) in Godot 4.4 dev4,5,6,7 and beta1
Labels: bug,topic:rendering,confirmed,regression,topic:3d,performance

### Tested versions

Reproducible in: Godot 4.4 beta1, dev4,5,6,7, ~Godot 4.4 dev1~
Not reproducible in: Godot 4.3 stable

### System information

Godot v4.4.beta1 - Windows 10 (build 19045) - Multi-window, 2 monitors - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 3070 (NVIDIA; 32.0.15.6636) - AMD Ryzen 7 5800X 8-Core Processor (16 threads)

### Issue description

In ~Godot 4.4 dev1~ **dev4,5,6,7 and 4.4 beta1**, a noticeable lag occurs when assigning a new not empty mesh to a MeshInstance3D. This happens specifically on the line:
```gdscript
# sb.gd
func changeMesh() -> void:
	# The lag issue is caused by this line in Godot 4.4 beta1 and dev4,5,6,7.
	# The issue persists from Godot 4.4 dev4,5,6,7 up to and including Godot 4.4 beta1.
	# If the same project is run in Godot 4.3 release, there are no issues.
	# Commenting out this line in Godot 4.4 removes the lag, suggesting the issue might be related to CSG/Mesh handling.
	# Replacing the line with add_child(CSGBox3D.new()) also causes lag in 4.4.
	# However, using add_child(Node3D.new()) works without any lag in 4.4.
	mi.mesh = BoxMesh.new()
	pass
```

The issue does not appear in Godot 4.3, where the same project works without any performance drops. When running the project in Godot 4.4 with this line active, there is significant lag, which can be resolved by commenting out the line.

Additionally, replacing `mi.mesh = BoxMesh.new()` with `add_child(CSGBox3D.new())` triggers the same lag. However, using `add_child(Node3D.new())` works without any lag, suggesting the problem is related to mesh handling or CSG node processing in Godot 4.4.

**In my project, there are parallel tasks (threads) being executed through a task manager, similar to what was used in my main project. I’m unsure whether the presence of these tasks is contributing to this issue. I added the same multithreading setup here, but I haven't been able to determine if the lag is caused by the mesh handling or if the task manager's threads are influencing it.**

https://youtu.be/Uw34gAoN7HE

### Steps to reproduce

Create a new Godot project and copy the provided scripts from MRP into your project or use MRP.

- main.gd: Attach this script to a Node3D node.
- sb.gd: Used in main.gd for creating instances of SB.
- taskManager.gd: This is a helper script to manage tasks using WorkerThreadPool and should be Global.
- Open the project in the Godot editor and run the scene.

Observe the performance issue:

The project creates 255 instances of the SB node.
Each SB runs a heavy computation (simulated in the `somethingProcess()` method).
After computation completes, the `changeMesh()` method is called to change the mesh of the SB instance.
Check the lag that occurs when the `changeMesh()` method sets `mi.mesh = BoxMesh.new()`. This will introduce a noticeable lag in ~Godot 4.4 dev1~ dev4,5,6,7 and 4.4 beta1. The lag does not occur in Godot 4.3 stable.

Test different changes:

- Comment out the line `mi.mesh = BoxMesh.new()`. This removes the lag.
- Replace the line with `add_child(CSGBox3D.new())`. This also causes the lag to appear in ~Godot 4.4 dev1~ dev4,5,6,7 and 4.4 beta1.
- Replace it with `add_child(Node3D.new())`. The lag does not occur when using a simple Node3D instead of a meshes.

### Minimal reproduction project (MRP)

[mrp-meshIssue4.4.zip](https://github.com/user-attachments/files/18480836/mrp-meshIssue4.4.zip)