Skip to content

Avoid allocation caused by the ForEachEntity() delegate #48

Answered by friflo
MaansenV asked this question in Q&A
Discussion options

You must be logged in to vote

To avoid the allocation caused by the delegate you can use the approach explained in the Wiki - Enumerate Query Chunks

This approach replaces Query.ForEachEntity(...) by two loops. This approach is allocation free.

foreach (var (positions, sprites, entities) in Query.Chunks)
{
    for (int n = 0; n < positions.Length; n++) {
        var position = positions[n];
        var sprite   = sprites[n];
        var texture = _textureManager.GetTexture(sprite.TextureId);
        _spriteBatch.Draw(...)
    }
}

If you like you can extract the inner block to a method.

foreach (var (positions, sprites, entities) in Query.Chunks)
{
    for (int n = 0; n < positions.Length; n++) {
        Draw(position[n],

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by friflo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants