Skip to content

Commit 7647c7a

Browse files
author
LoneWandererProductions
committed
Improve memory config and arena
1 parent 563906c commit 7647c7a

3 files changed

Lines changed: 274 additions & 246 deletions

File tree

MemoryManager.Core/MemoryManagerConfig.cs

Lines changed: 90 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,28 @@
22
* COPYRIGHT: See COPYING in the top level directory
33
* PROJECT: MemoryArenaPrototype.Core
44
* FILE: MemoryManagerConfig.cs
5-
* PURPOSE: Your file purpose here
5+
* PURPOSE: Configuration class for the MemoryManager, encapsulating all tunable parameters and thresholds for memory management policies.
66
* PROGRAMMER: Peter Geinitz (Wayfarer)
77
*/
88

99
namespace MemoryManager.Core
1010
{
1111
/// <summary>
12-
/// The config for the mm
12+
/// The config for the mm
1313
/// </summary>
1414
public sealed class MemoryManagerConfig
1515
{
16-
// Add more knobs here as you identify other tuning parameters
1716

1817
/// <summary>
19-
/// Parameter-less constructor with defaults
18+
/// Parameter-less constructor with defaults
2019
/// </summary>
2120
public MemoryManagerConfig()
2221
{
2322
}
2423

2524
/// <summary>
26-
/// Constructs config based on a given slow lane size.
27-
/// Other parameters are set with "rule of thumb" proportions.
25+
/// Constructs config based on a given slow lane size.
26+
/// Other parameters are set with "rule of thumb" proportions.
2827
/// </summary>
2928
/// <param name="slowLaneSize">Total size of the slow lane (bytes)</param>
3029
public MemoryManagerConfig(int slowLaneSize)
@@ -112,7 +111,6 @@ public MemoryManagerConfig(int slowLaneSize)
112111
/// </value>
113112
public double CompactionThreshold { get; init; } = 0.80;
114113

115-
116114
/// <summary>
117115
/// Gets the policy check interval.
118116
/// How often to check allocation policies like compaction and reclamation
@@ -123,16 +121,33 @@ public MemoryManagerConfig(int slowLaneSize)
123121
/// </value>
124122
public TimeSpan PolicyCheckInterval { get; init; } = TimeSpan.FromSeconds(1);
125123

126-
// Whether automatic compaction is enabled
127-
// Useful to turn off during profiling or debugging
124+
/// <summary>
125+
/// Gets a value indicating whether [enable automatic compaction].
126+
/// Useful to turn off during profiling or debugging
127+
/// </summary>
128+
/// <value>
129+
/// <c>true</c> if [enable automatic compaction]; otherwise, <c>false</c>.
130+
/// </value>
128131
public bool EnableAutoCompaction { get; init; } = true;
129132

130-
// Generic threshold for some operation (adjust as needed)
131-
// Example: 256KB, might represent chunk size or fragmentation tolerance
133+
/// <summary>
134+
/// Gets the threshold.
135+
/// Generic threshold for some operation (adjust as needed)
136+
/// Example: 256KB, might represent chunk size or fragmentation tolerance
137+
/// </summary>
138+
/// <value>
139+
/// The threshold.
140+
/// </value>
132141
public int Threshold { get; init; } = 1024 * 1024 / 4; //256 KB
133142

134-
// Usage fraction of fast lane to trigger compaction (e.g., 90%)
135-
// Higher means less frequent compactions but higher risk of fragmentation
143+
/// <summary>
144+
/// Gets the fast lane usage threshold.
145+
/// Usage fraction of fast lane to trigger compaction (e.g., 90%)
146+
/// Higher means less frequent compactions but higher risk of fragmentation
147+
/// </summary>
148+
/// <value>
149+
/// The fast lane usage threshold.
150+
/// </value>
136151
public double FastLaneUsageThreshold { get; init; } = 0.9;
137152

138153
/// <summary>
@@ -192,5 +207,67 @@ public double GetEstimatedReservedMegabytes()
192207
// BufferSize is gone! Just the raw unmanaged lane allocations.
193208
return (FastLaneSize + SlowLaneSize) / (1024.0 * 1024.0);
194209
}
210+
211+
/*
212+
* Preset factory methods for common scenarios. These provide convenient starting points for typical use cases,
213+
*/
214+
215+
/// <summary>
216+
/// Creates a configuration tuned for real-time game loops.
217+
/// Employs an ultra-fast Bump/Linear allocator for short-lived transient frames.
218+
/// </summary>
219+
public static MemoryManagerConfig CreateForGameLoop(int totalBudget = 16 * 1024 * 1024)
220+
{
221+
return new MemoryManagerConfig
222+
{
223+
SlowLaneSize = (int)(totalBudget * 0.75),
224+
FastLaneSize = (int)(totalBudget * 0.25),
225+
FastLaneStrategy = AllocatorStrategy.LinearBump, // Maximum speed
226+
MaxFastLaneAgeFrames = 300, // Evict to SlowLane faster
227+
FastLaneUsageThreshold = 0.85,
228+
CompactionThreshold = 0.75,
229+
EnableAutoCompaction = true,
230+
PolicyCheckInterval = TimeSpan.FromMilliseconds(16) // Check roughly every frame at 60fps
231+
};
232+
}
233+
234+
/// <summary>
235+
/// Creates a configuration tuned for heavy I/O, networking, or asset streaming.
236+
/// Uses a FreeList allocator to handle random out-of-order allocations.
237+
/// </summary>
238+
public static MemoryManagerConfig CreateForBulkProcessing(int totalBudget = 64 * 1024 * 1024)
239+
{
240+
return new MemoryManagerConfig
241+
{
242+
SlowLaneSize = (int)(totalBudget * 0.85),
243+
FastLaneSize = (int)(totalBudget * 0.15),
244+
FastLaneStrategy = AllocatorStrategy.FreeList, // Out-of-order safety
245+
FastLaneLargeEntryThreshold = 16384, // Allow up to 16KB in the hot lane
246+
MaxFastLaneAgeFrames = 1200, // Let data sit longer before moving
247+
SlowLaneUsageThreshold = 0.80,
248+
SlowLaneBlobThreshold = 512, // Route larger fragments to blobs
249+
PolicyCheckInterval = TimeSpan.FromSeconds(2) // Low maintenance overhead
250+
};
251+
}
252+
253+
/// <summary>
254+
/// Creates a configuration tuned for tightly constrained or embedded footprints.
255+
/// Minimizes unmanaged allocation limits and compacts aggressively.
256+
/// </summary>
257+
public static MemoryManagerConfig CreateForLowMemory()
258+
{
259+
return new MemoryManagerConfig
260+
{
261+
FastLaneSize = 256 * 1024, // 256 KB
262+
SlowLaneSize = 1024 * 1024, // 1 MB
263+
FastLaneStrategy = AllocatorStrategy.FreeList,
264+
EnableAutoCompaction = true,
265+
CompactionThreshold = 0.60, // Compact very early
266+
FastLaneUsageThreshold = 0.70,
267+
SlowLaneUsageThreshold = 0.70,
268+
SlowLaneBlobCapacityFraction = 0.35, // Allocate more space for tiny fragments
269+
PolicyCheckInterval = TimeSpan.FromMilliseconds(500)
270+
};
271+
}
195272
}
196273
}

0 commit comments

Comments
 (0)