我现在正在玩 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/