Calling FindObjectsOfType and iterating through the results in the Update() method causes a drop in frame rate.

#14 · closed · 2 comments

View on GitHub ↗

2822867502

The English issue is after the Chinese 2. 问题描述 但是我注意到,在使用该插件进行去码后,帧率出现了明显的下降,经核实,游戏资源越多,帧率下降越明显,我查看了源代码,发现以下问题 在`DumbRendererDemosaic`类中 ``` public class DumbRendererDemosaic : MonoBehaviour { public DumbRendererDemosaic(System.IntPtr handle) : base(handle) { } private void Update() { var array = FindObjectsOfType(Il2CppType.Of<Renderer>()); var ofType = array.Select(x => x.Cast<Renderer>()); foreach (var renderer in ofType) { ..... } } } ``` 您在Update()函数中使用`Object.FindObjectsOfType`并对结果进行遍历,我认为这是严重拖慢性能的 2.原因分析 我了解到,`Update()`在每一帧都会调用,而`Object.FindObjectsOfType`和遍历都是消耗性能的行为,因此这是导致帧率降低的原因 3.解决方案 - **在`Start()`中调用**:考虑到大多数游戏不会把`Enabled = false`的资源自动启用,因此我认为在游戏启动时调用即可 - **使用缓存**:在每次去码后,记录所操作对象的特征并写入缓存,在下次去码操作时可以避免再次大范围查找 考虑到我并非专业Unity开发者,故未通过Pull Request提交代码,并且以上方案仅供参考,希望您能有所启发 4.致谢 由衷感谢作者所创作的插件使去码过程得以简化 --- ### 1. Problem Description However, I noticed that after using this plugin for demosaicing, the frame rate dropped significantly. Upon verification, the more game resources there were, the more noticeable the frame rate drop became. I examined the source code and found the following issue: In the `DumbRendererDemosaic` class: ``` public class DumbRendererDemosaic : MonoBehaviour { public DumbRendererDemosaic(System.IntPtr handle) : base(handle) { } private void Update() { var array = FindObjectsOfType(Il2CppType.Of<Renderer>()); var ofType = array.Select(x => x.Cast<Renderer>()); foreach (var renderer in ofType) { ..... } } } ``` I believe that calling `Object.FindObjectsOfType` inside the `Update()` method and subsequently iterating over the results is severely impacting performance. 2. Root Cause Analysis I understand that `Update()` is called every frame, and both `Object.FindObjectsOfType` and the subsequent iteration are computationally expensive operations. Therefore, this is the primary cause of the reduced frame rate. 3. Proposed Solutions - **Move to `Start()` Method**: Since most games won't automatically re-enable objects that have been set to `Enabled = false`, I suggest calling this operation only once during initialization, such as in the `Start()` method. - **Implement Caching**: After each demosaic operation, record and store the characteristics of the affected objects. This cache can then be used in future operations to avoid repeatedly searching through all objects. Since I am not a professional Unity developer, I did not submit a Pull Request with proposed code changes. Additionally, the above suggestions are only references and may require further refinement before implementation. 4. Acknowledgment My sincere gratitude goes to the author for creating this plugin, which greatly simplifies the demosaicing process. *Translated by Qwen (Tongyi Qianwen). My English is not very good, so if there are any translation errors, please forgive me.*

Comments

ManlyMarco

This is a known issue. I did this intentionally because it is fairly common for games to create and destroy relevant objects during gameplay, which makes checking in Start useless.

2822867502

For others experiencing the same issue: This problem was fixed in PR #15 . Please update to version 1.7 to improve performance.