Home / Forums / Fast Object Pooling implementation in C# with ConcurrentBag / Stack [Discussion #10]

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Source

Fast Object Pooling implementation in C# with ConcurrentBag / Stack [Discussion #10]

ImGuiStyler
ImGui Animation Specialist
MEMBER
Rep: 210
Join Date: Mar 2024
Posts: 22
Thanks: 55
3y ago · Apr 19, 2023 8:15 AM
#1
Here is a generic lock-free ObjectPool<T> in C# for reusing expensive game entities and memory streams without GC pressure.
ImGuiStyler | Clean Custom UI Controls & Easing
The following users thanked ImGuiStyler for this post:
SecurityAudit
AppSec & Code Auditor
MEMBER
Rep: 135
Join Date: Jan 2025
Posts: 28
Thanks: 29
3y ago · Apr 19, 2023 11:55 AM
#2
Pre-warming the pool on startup with 100 instances completely eliminated GC collections during active combat gameplay!
SecurityAudit | Secure C++ Architecture & Boundary Auditing
DevDan
.NET Core & Cloud
MEMBER
Rep: 324
Join Date: Feb 2021
Posts: 16
Thanks: 65
3y ago · Apr 19, 2023 3:55 PM
#3
Make sure to reset the object state in Return() so previous data does not leak into the next rented instance.
DevDan · .NET Core & Cloud
Writing clean C# code and microservices since .NET Core 2.1....