mirror of
https://github.com/Facepunch/sbox-public.git
synced 2026-01-08 22:38:28 -05:00
This commit imports the C# engine code and game files, excluding C++ source code. [Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
44 lines
1.0 KiB
C#
44 lines
1.0 KiB
C#
|
|
/// <summary>
|
|
/// A disposable region that prevents garbage collection for the duration of the using block.
|
|
/// Useful for reducing GC pauses during performance-critical code.
|
|
/// Allocates up to 512MB with 128MB for large objects before allowing GC.
|
|
/// </summary>
|
|
internal ref struct HeavyGarbageRegion
|
|
{
|
|
bool inRegion;
|
|
|
|
/// <summary>
|
|
/// Enters a no-GC region if one isn't already active.
|
|
/// </summary>
|
|
public HeavyGarbageRegion()
|
|
{
|
|
try
|
|
{
|
|
// Try to start no-GC region: 512MB total, 128MB for large objects, discard on full GC
|
|
inRegion = GC.TryStartNoGCRegion( 1024 * 1024 * 512, 1024 * 1024 * 128, true );
|
|
}
|
|
catch ( InvalidOperationException )
|
|
{
|
|
// Already in a NoGCRegion - this is fine
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Exits the no-GC region, allowing garbage collection to resume.
|
|
/// </summary>
|
|
public void Dispose()
|
|
{
|
|
if ( !inRegion ) return;
|
|
|
|
try
|
|
{
|
|
GC.EndNoGCRegion();
|
|
}
|
|
catch ( InvalidOperationException )
|
|
{
|
|
// Can happen if allocations exceeded the budget (e.g., during hot reload)
|
|
}
|
|
}
|
|
}
|