Tencent / puerts

PUER(普洱) Typescript. Let's write your game in UE or Unity with TypeScript.
Other
5.08k stars 708 forks source link

[Unity]Why il2cpp is slower than dll When using Array? 为什么使用数组时il2cpp比dll慢? #1380

Closed danij91 closed 1 year ago

danij91 commented 1 year ago

Hello! First of all, I want to express my gratitude for everything you've done. Recently, I conducted a test on using CSharp native arrays from JavaScript, and the results were a bit peculiar. Allow me to explain the test I conducted: 你好!首先,非常感谢你所做的一切。最近,我进行了一项关于从JavaScript使用CSharp本机数组的测试,结果有些奇怪。让我解释一下我进行的测试:

  1. I created 1000 native arrays, each consisting of 1000 elements with a data type of Double. 我创建了1000个本机数组,每个数组包含1000个元素,元素的数据类型为Double。
  2. I assigned values to each element of the arrays. 我为每个数组的每个元素分配了值。

Initially, I assumed that il2cpp would outperform the DLL, but the results were quite the opposite. il2cpp took approximately 4000 milliseconds, whereas the DLL only took about 400 milliseconds. Could you please explain why this occurred? As a side note, when I performed the same task using native CSharp, it only took 40 milliseconds. 最初,我以为il2cpp会比DLL更快,但结果恰恰相反。il2cpp花费了大约4000毫秒,而DLL只需约400毫秒。请问你能解释一下为什么会发生这种情况吗?另外,值得一提的是,当我使用本机CSharp进行相同的操作时,只需要40毫秒。

I have attached the corresponding code below for your reference. 以下是相关代码的附加内容,供你参考。 ` public void RunJS() { int count = 1000; int length = 1000;

    var sw = new Stopwatch();
    var env = new JsEnv();
    env.Eval(@$"const count ={count}");
    env.Eval(@$"const length ={length}");

    env.Eval(@"const arrArr = [];
                       for(var i = 0;i<count;i++){
                         arrArr.push(CS.System.Array.CreateInstance(CS.System.Type.GetType('System.Double'), length));
                       }");
    sw.Start();
    env.Eval(@"for(var i = 0 ;  i<count; i++){
                         for(var j = 0 ;  j<length; j++){
                           arrArr[i].SetValue(1,j);
                         }
                       }");
    sw.Stop();
    env.Dispose();
    Debug.Log(sw.ElapsedMilliseconds);
}`

Thank you in advance for your assistance! 非常感谢你的帮助!

danij91 commented 1 year ago

Additionally, is it only possible to use v8 as the backend for il2cpp? 另外,il2cpp只能使用v8作为后端吗?

zombieyang commented 1 year ago

you can also use node.js as the backend, but quickjs is not supported yet. 也可以用nodejs作为后端。quickjs尚未支持。

which version are you using for test? did you generated the fully FunctionBridge.Gen.h? 你用于测试的是哪个版本?是否生成了全量的FunctionBridge.Gen.h

In the last version I made a mistake to setting the default bindingmode to slowbinding, maybe you can try to cherry-pick this line: 上个版本我有个手误将默认调用模式弄成了反射,你看看是不是要改下这里 https://github.com/Tencent/puerts/commit/5f046ec87e0a549698233bb9f156dbc4d4c78042#diff-647af41a44346c1aa008a189579d38026fa1f165a4e541822369de36a82f30fdR59

danij91 commented 1 year ago

@zombieyang Thank you for answering my question! I tested with commit 53f33017883749f3c692d05ae61de0f6aba0797b when I found this issue. and I tried again with your solution. But result is almost same. il2cpp is still 9 times slower than non il2cpp.

zombieyang commented 1 year ago

thanks for you feed back.

I wrote a test: https://github.com/zombieyang/PerformanceTesting/blob/main/Assets/CScripts/Examples/Example40.cs

I ran it with commit 53f3301 and the result is still in my expectation (tested in windows)

Could you provide more information about your testing step? If it is too much information to provide, You can contact me in Discord.

danij91 commented 1 year ago

Hello, I think I made mistake while applying IL2CPP, and I have a few further questions.

  1. I have been following the instructions provided in this link: https://puerts.github.io/en/docs/puerts/unity/performance/il2cpp/, but seem to have missed something. I'm having trouble pinpointing exactly where I went wrong. Could you possibly provide some guidance on this?
  2. My testing has been exclusively conducted in the Unity editor environment (Windows 64-bit). Could this be the source of any potential issues?

Thank you in advance for your assistance.

您好,我在应用IL2CPP时可能犯了一些错误,我还有一些其他的问题。

我一直按照这个链接中提供的说明进行操作:https://puerts.github.io/en/docs/puerts/unity/performance/il2cpp/ ,但似乎还是错过了一些东西。

我在确定我哪里出错上有些困难。你能给我一些指导吗?

我仅在Unity编辑器环境(Windows 64位)中进行了测试。这可能是潜在问题的源头吗?

希望我的翻译准确无误。

提前感谢你的帮助。

zombieyang commented 1 year ago

Il2cpp backend will not take effect in Unity Editor, It needs Unity Compilation pipeline to transfer C# code to IL and finally to C++. So you must build your project to a standalone exe to see the result.

In UnityEditor, the only available script backend is mono. And PuerTS will run in default mode.


And I know the reason that why you get a slow result: In latest master, the wrapper of default mode will not take effect when the definition EXPERIMENTAL_IL2CPP_PUERTS is setted. So your program is ran in default mode and used reflection to complete JS to C# invokation.

danij91 commented 1 year ago

我非常感谢你的帮助,它帮我解决了问题!

我还有一些问题想要问:

  1. Puerts 2.0的正式版本计划何时发布?
  2. 你们有没有计划开发一个可以在Unity编辑器模式下使用的IL2CPP版本?

再次感谢你的帮助。

Your message is already quite good, but here's a slightly more polished version:

I greatly appreciate your assistance, as it helped me resolve the issue!

I do have a few more questions I'd like to ask:

  1. When is the official release of Puerts 2.0 scheduled?
  2. Are there any plans to develop an IL2CPP version that's available in Unity editor mode?

Again, thank you very much for your help.

chexiongsheng commented 1 year ago

Unity itself does not support il2cpp in editor.

zombieyang commented 1 year ago

我非常感谢你的帮助,它帮我解决了问题!

我还有一些问题想要问:

  1. Puerts 2.0的正式版本计划何时发布?
  2. 你们有没有计划开发一个可以在Unity编辑器模式下使用的IL2CPP版本?

再次感谢你的帮助。

Your message is already quite good, but here's a slightly more polished version:

I greatly appreciate your assistance, as it helped me resolve the issue!

I do have a few more questions I'd like to ask:

  1. When is the official release of Puerts 2.0 scheduled?
  2. Are there any plans to develop an IL2CPP version that's available in Unity editor mode?

Again, thank you very much for your help.

2.0.0-RC will coming this month. RC means no more new feature will be added until official release. And RC will end if there are no bugs being reported in about two week