Skip to content

Instantly share code, notes, and snippets.

@koster
Last active June 19, 2024 15:18
Show Gist options
  • Save koster/dccd926f8beb177b4be80f65c567b7d3 to your computer and use it in GitHub Desktop.
Save koster/dccd926f8beb177b4be80f65c567b7d3 to your computer and use it in GitHub Desktop.
Use this to stop allocating lists, stop allocs now❌❌❌❌❌❌❌ (Unity)
using System.Collections.Generic;
public static class ListPool<T>
{
const int InitialPoolSize = 32;
static List<List<T>> pool = new List<List<T>>(InitialPoolSize);
public static List<T> Request(int minSize = 32)
{
if (pool.Count > 0)
{
var request = pool[0];
request.Clear();
pool.RemoveAt(0);
return request;
}
return new List<T>(minSize);
}
public static void Return(List<T> l)
{
pool.Add(l);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment