Last active
June 19, 2024 15:18
-
-
Save koster/dccd926f8beb177b4be80f65c567b7d3 to your computer and use it in GitHub Desktop.
Use this to stop allocating lists, stop allocs now❌❌❌❌❌❌❌ (Unity)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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