Repository: godot
Issue: Poor multithreading performance when calling functions within a thread
Labels: bug,topic:core,topic:gdscript,confirmed,performance

### Godot version

3.4.2-stable

### System information

Windows 10, Intel Core i7 8700-K (6 cores, 12 threads)

### Issue description

### I've created a scene that tests the performance of Godot's multithreading:

![ezgif-5-dcb88393d0](https://user-images.githubusercontent.com/80287313/154687836-34c12be6-d0a5-4afc-aa4f-088147b5ccd9.gif)
Blue squares show the processing speeds of each thread.
The red square shows the combined processing speed of all threads.
Keys 1-6 start/stop threads. Use the mouse wheel to adjust the scale.

Speed is measured by looping some logic, and finding the number of iterations processed per microsecond.
(Exact speed values are not shown.)

### Standard GDScript Operations Work Fine

If iteration logic of each thread uses standard GDScript operations:
```
while(true):
    some_val += 1
```
Then the performance seems fine.
Adding a thread does not slow down other threads, and the total processing speed increases.
(Shown above)

### Calling Functions Works Poorly

If iteration logic of each thread uses some kind of function:
```
while(true):
    _increment(some_val)
```
Then the performance suffers:

![ezgif-5-9d2cfac093](https://user-images.githubusercontent.com/80287313/154689231-f65a66f6-4bbe-4cd2-90eb-28849d893f22.gif)

Adding a thread slows down other threads, and the total processing speed does not increase.
This happens for:
+ GDScript functions
+ Standard Module C++ functions (_such as OpenSimplexNoise.get_noise_2d()_)
+ Custom Module C++ functions (_such as Summator.add() from the tutorial Module_)

### Calling a Unique Function Per Thread:

If the iteration logic of each thread calls a unique function:
```
while(true):
    call("_increment" + str(thread_index), some_val)
```
Then the performance does not suffer as much:

![ezgif-5-7697ed75af](https://user-images.githubusercontent.com/80287313/154690575-6b0921ae-99d8-4e94-a652-4caaa15f1bfd.gif)

Adding a thread slows down other threads, but the total processing speed increases.

### Steps to reproduce

Here are the game builds I used for testing:
(Use keys 1-6 and mouse scroll wheel)

[Threading_Test_No_Function.zip](https://github.com/godotengine/godot/files/8097550/Threading_Test_No_Function.zip)
[Threading_Test_GDScript_Function.zip](https://github.com/godotengine/godot/files/8097547/Threading_Test_GDScript_Function.zip)
[Threading_Test_Module_Function.zip](https://github.com/godotengine/godot/files/8097546/Threading_Test_Module_Function.zip)
[Threading_Test_Custom_Module_Function.zip](https://github.com/godotengine/godot/files/8097545/Threading_Test_Custom_Module_Function.zip)
[Threading_Test_Unique_GDScript_Functions.zip](https://github.com/godotengine/godot/files/8097551/Threading_Test_Unique_GDScript_Functions.zip)


Download the minimal reproduction project and comment/uncomment lines from the _thread_function() function.
Make sure to export the game as an .exe before testing.

If you want to test the Summator.add() function, you'll need to build the engine from the 3.4.2-stable source code with the Summator module included:
[summator.zip](https://github.com/godotengine/godot/files/8097581/summator.zip)



### Minimal reproduction project

[Multithreading_Test.zip](https://github.com/godotengine/godot/files/8097640/Multithreading_Test.zip)