Chunk Generation Performance in C# I'm working on a game in C#, this game generate new chunk (infinite) when you move around. If I have > 1000 chunks in my list then it become laggy even though I only load the necessary chunks on my screen. Tried all things to improve the code but it did not improve.

Stopwatch ChunkDetect = new Stopwatch();
List&lt;Chunks&gt; allChunks = new List&lt;Chunks&gt;();
public readonly int sizeChunk = 800;

private void loadChunks()
{
    ChunkDetect.Restart();
    ChunkDetect.Start();
    int CamXCh = Convert.ToInt32(Math.Ceiling(-Convert.ToDouble(CameraX) / sizeChunk)),
        CamYCh = Convert.ToInt32(Math.Ceiling(-Convert.ToDouble(CameraY) / sizeChunk));

    int sizeX = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(gameBox.Width) / sizeChunk)) + 4,
        sizeY = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(gameBox.Height) / sizeChunk)) + 4;

    Random generation = new Random();
    // Load Chunks or Create
    for (int chunkX = 0; chunkX &lt; sizeX; chunkX++)
    {
        for (int chunkY = 0; chunkY &lt; sizeY; chunkY++)
        {
            int xChunk = chunkX + CamXCh - 2,
                yChunk = chunkY + CamYCh - 2;

            Chunks currentChunk = allChunks.Where(i =&gt; i.positionX == xChunk &amp;&amp; i.positionY == yChunk).FirstOrDefault();

            bool isLoad = true;
            if (chunkX == 0 || chunkX == (sizeX - 1) || chunkY == 0 || chunkY == (sizeY - 1))
                isLoad = false;

            if (currentChunk == null)
            {
                Color ColorBiome = Color.FromArgb(64, 233, 56);
                string BiomeName = "Grass";
                allChunks.Add(new Chunks(xChunk, yChunk, sizeChunk, ColorBiome, BiomeName, isLoad, 40));
            }
            else
            {
                bool isCurrentLoaded = currentChunk.isLoaded;
                if (!isLoad)
                    currentChunk.isLoaded = false;
                else if (!isCurrentLoaded)
                    currentChunk.isLoaded = true;
            }
        }
    }
    ChunkDetect.Stop();
}

class Chunks
{
    public Color biomeType;
    public Guid uid;
    public bool isLoaded;
    public int positionX, positionY, Size;
    public string biomeName;
    public List&lt;CeldsChunk&gt; MapChunk;

    public Chunks(int x, int y, int size, Color biome, string BiomeName, bool loaded, int sizeCeld)
    {

        // Identificador
        this.uid = new Guid();

        // Posición y tamaño
        this.positionX = x;         // Position
        this.positionY = y;         // Position
        int cSize = this.Size = size;         // Size

        // Bioma
        this.biomeType = biome;      // Biome Color
        this.biomeName = BiomeName;  // Name of Biome

        // Propiedades
        this.isLoaded = loaded;      // If chunk is loaded

        int sizeChunkInCeld = (cSize / sizeCeld);

        List&lt;CeldsChunk&gt; celdsToChunk = new List&lt;CeldsChunk&gt;();

        Random rd = new Random();
        for (int CeldX = 0; CeldX &lt; sizeChunkInCeld; CeldX++)
        {
            for (int CeldY = 0; CeldY &lt; sizeChunkInCeld; CeldY++)
            {
                celdsToChunk.Add(new CeldsChunk(new Point(CeldX, CeldY, sizeCeld), this.biomeType));
            }
        }

        this.MapChunk = celdsToChunk;
    }

    public void changeLoaded(bool g)
    {
        this.isLoaded = g;
    }

}
Your allChunks is a List. The Linq query to get the chunk with the right coordinates is going to scan the entire list, and check each chunk. This is a linear process, and the performance depends on the number of chunks in the list. As the list grows, the performance decreases.

The solution is to index the chunks in a way that makes the lookup performance a constant-time operation (not dependant on the number of chunks).

The logical data structure for this is to use a Dictionary, and key the Dictionary on a value that is the coordinate. Thus, given a coordinate, you can easily find the Chunk.

Your chunks are indexed using int values for the X and Y coordinate. I would recommend using a long-value as the key, and to simply shift the X and Y coordinates in to the Dictionary... for example:

Dictionary&lt;long, Chunks&gt; indexChunks = ....

// merge both coordinates in to one unique key
// the y-coordinate is in the high-32-bits, the x-coord in the low 32 bits.
// this should probably be in a function.
long key = xChunk + ((long)yChunk &lt;&lt; 32);

if (!indexChunks.containsKey(key)) {
    indexChunks.Add(key, new Chunks(.....));
}

Chunks currentChunk = indexChunks[key];


The Dictionary will convert the lookup process to a faster, scalable mechanism.

If you want to, you can create a more complicated key mechanism, but I would recommend that you keep the key as simple as possible.