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

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Source

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

OffsetHunter
RTTI & NetVar Dumper
MEMBER
Rep: 175
Join Date: Jul 2024
Posts: 20
Thanks: 44
3y ago · Apr 10, 2023 3:39 PM
#1
Here is a generic lock-free ObjectPool<T> in C# for reusing expensive game entities and memory streams without GC pressure.
OffsetHunter · RTTI TypeDescriptors & NetVars
The following users thanked OffsetHunter for this post:
GhostHunter_x
Unity & Phasmo Modder
VIP
Rep: 275
Join Date: Apr 2023
Posts: 17
Thanks: 71
3y ago · Apr 10, 2023 6:55 PM
#2
Pre-warming the pool on startup with 100 instances completely eliminated GC collections during active combat gameplay!
GhostHunter_x · Phasmophobia & Unity Reverser
GhostAI, Sanity, EMF & Evidence inspection
PixelCraft
ImGui UI Hacker
MEMBER
Rep: 344
Join Date: Mar 2022
Posts: 7
Thanks: 92
3y ago · Apr 10, 2023 11:55 PM
#3
Make sure to reset the object state in Return() so previous data does not leak into the next rented instance.
PixelCraft · ImGui UI Hacker
Creating custom Dear ImGui themes, graphs, docking layouts, ...