mobile - 在 CLR/Mono 上经常调用的函数中进行分配是否效率低下?

标签 mobile memory-management mono clr monogame

我现在正在玩 MonoGame,并且正在研究 SpriteBatch.Draw() 方法。

在每秒可以调用数万次的方法内部,我看到以下内容:

public void Draw (Texture2D texture,
        Rectangle destinationRectangle,
        Rectangle? sourceRectangle,
        Color color,
        float rotation,
        Vector2 origin,
        SpriteEffects effect,
        float depth)
    {
        CheckValid(texture);

        DrawInternal(texture,
              new Vector4(destinationRectangle.X, //<-------------- Oh Noes! :O
                          destinationRectangle.Y,
                          destinationRectangle.Width,
                          destinationRectangle.Height),
              sourceRectangle,
              color,
              rotation,
              new Vector2(origin.X * ((float)destinationRectangle.Width (float)texture.Width), <-- Oh Noes!
                          origin.Y * ((float)destinationRectangle.Height / (float)texture.Height)),
              effect,
              depth);
    }

似乎每隔 60 秒左右就会分配数千个新向量。

MonoGame 是一个用于(除其他外)移动开发的平台。我做了一些在 Dalvik 上运行的移动开发,以这种方式分配从来都不是一个好主意。在慢速移动处理器上,需要收集的对象的积累最终会导致 GC 运行并导致性能明显下降(在某些设备上最多 200 毫秒)。这引出了我的问题:

CLR/Mono GC 的运行方式或其他一些因素是否会阻止这种性能影响的发生,或者 MonoGame 是否在其最常调用的函数之一中执行了不应执行的操作?

最佳答案

Vector4 type不是一个类,它是一个结构。创建它的新实例不会在堆上创建对象,而是创建一个结构值。在这种情况下,值被推送到方法调用的堆栈上,因此它与将成员作为单独的参数推送基本上是相同的工作。

关于mobile - 在 CLR/Mono 上经常调用的函数中进行分配是否效率低下?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13446275/

相关文章:

css - 媒体查询适用于桌面浏览器,但不适用于移动设备

html - 垂直显示折叠的菜单项,导航栏居中, Logo 居中( Bootstrap )

javascript - Google AdWords 通过电话进行转化?

mobile - Kendo ui 移动分屏导航问题

c - 在 C 中读取执行程序标准输出的更好方法

c# - 我可以在 MS.Net Framework 4.0 中使用 SqlClient 的 Mono 实现吗?

java - 如何反转放置 2/3 堆的字符串?

c++ - GCC 是如何实现 C++ 标准分配器的?

c# - 未找到单声道 GAC DLL

c# - 在 Xamarin.Forms 中使用 android 支持库 API 级别 26 (MonoAndroid 8.0 Oreo)?