-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Engine/MemoryCache: Stage 1 improvements (RFC #2647) #2649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zalza13
wants to merge
1
commit into
litedb-org:dev
Choose a base branch
from
zalza13:feature/memorycache-pr1
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,27 @@ internal class MemoryCache : IDisposable | |
/// Get memory segment sizes | ||
/// </summary> | ||
private readonly int[] _segmentSizes; | ||
|
||
// On-demand cleanup system to prevent memory leaks | ||
private readonly SemaphoreSlim _cleanLock = new SemaphoreSlim(1, 1); // Non-reentrant cleanup lock | ||
private DateTime _lastCleanupRunUtc = DateTime.MinValue; // Last cleanup execution time | ||
private volatile bool _cleanupSignal; // Signal to trigger cleanup | ||
|
||
// Conservative library defaults | ||
private const int DEFAULT_MAX_FREE_PAGES = 200; | ||
private static readonly TimeSpan DEFAULT_IDLE = TimeSpan.FromSeconds(60); | ||
private static readonly TimeSpan DEFAULT_INT = TimeSpan.FromSeconds(30); | ||
private const int DEFAULT_BATCH = 128; | ||
private const int OPS_CLEANUP_STEP = 256; // Trigger cleanup every N operations | ||
|
||
// Instance-level overrides for tuning | ||
private volatile int _maxFreePages = DEFAULT_MAX_FREE_PAGES; | ||
private TimeSpan _idleBeforeEvict = DEFAULT_IDLE; | ||
private TimeSpan _minCleanupInterval = DEFAULT_INT; | ||
private volatile int _cleanupBatchSize = DEFAULT_BATCH; | ||
private int _opsSinceLastCleanup; // Operations counter for periodic cleanup | ||
|
||
public enum CacheProfile { Mobile, Desktop, Server } | ||
|
||
public MemoryCache(int[] memorySegmentSizes) | ||
{ | ||
|
@@ -79,6 +100,12 @@ public PageBuffer GetReadablePage(long position, FileOrigin origin, Action<long, | |
// increment share counter | ||
Interlocked.Increment(ref page.ShareCounter); | ||
|
||
// Periodic cleanup trigger | ||
if ((Interlocked.Increment(ref _opsSinceLastCleanup) & (OPS_CLEANUP_STEP - 1)) == 0) | ||
{ | ||
TryCleanupOnDemand(); | ||
} | ||
|
||
return page; | ||
} | ||
|
||
|
@@ -134,7 +161,9 @@ public PageBuffer GetWritablePage(long position, FileOrigin origin, Action<long, | |
/// </summary> | ||
public PageBuffer NewPage() | ||
{ | ||
return this.NewPage(long.MaxValue, FileOrigin.None); | ||
var page = this.NewPage(long.MaxValue, FileOrigin.None); | ||
|
||
return page; | ||
} | ||
|
||
/// <summary> | ||
|
@@ -256,7 +285,10 @@ public void DiscardPage(PageBuffer page) | |
// or will be overwritten by ReadPage | ||
|
||
// added into free list | ||
page.Timestamp = DateTime.UtcNow.Ticks; // PR-1 mark freed time | ||
_free.Enqueue(page); | ||
if (_free.Count > _maxFreePages) _cleanupSignal = true; | ||
TryCleanupOnDemand(force: _free.Count > _maxFreePages); | ||
} | ||
|
||
#endregion | ||
|
@@ -339,7 +371,9 @@ private void Extend() | |
page.Position = long.MaxValue; | ||
page.Origin = FileOrigin.None; | ||
|
||
page.Timestamp = DateTime.UtcNow.Ticks; | ||
_free.Enqueue(page); | ||
if (_free.Count > _maxFreePages) _cleanupSignal = true; | ||
} | ||
} | ||
|
||
|
@@ -401,28 +435,186 @@ private void Extend() | |
public int Clear() | ||
{ | ||
var counter = 0; | ||
var now = DateTime.UtcNow; | ||
var overCap = false; | ||
|
||
ENSURE(this.PagesInUse == 0, "must have no pages in use when call Clear() cache"); | ||
|
||
foreach (var page in _readable.Values) | ||
// Thread-safe enumeration of ConcurrentDictionary | ||
foreach (var kv in _readable) | ||
{ | ||
page.Position = long.MaxValue; | ||
page.Origin = FileOrigin.None; | ||
if (_readable.TryRemove(kv.Key, out var page)) | ||
{ | ||
// Reset page controls | ||
page.Position = long.MaxValue; | ||
page.Origin = FileOrigin.None; | ||
|
||
// Mark as "freed at" for idle policy | ||
page.Timestamp = now.Ticks; | ||
|
||
_free.Enqueue(page); | ||
_free.Enqueue(page); | ||
counter++; | ||
|
||
counter++; | ||
// Check if we exceed free pages limit | ||
if (_free.Count > _maxFreePages) | ||
overCap = true; | ||
} | ||
} | ||
|
||
_readable.Clear(); | ||
// Trigger cleanup if needed (outside any locks) | ||
if (overCap) | ||
{ | ||
_cleanupSignal = true; | ||
TryCleanupOnDemand(force: true); | ||
} | ||
|
||
return counter; | ||
} | ||
|
||
#endregion | ||
|
||
#region Memory Cleanup & Tuning | ||
|
||
/// <summary> | ||
/// Apply predefined cache tuning profiles for different environments | ||
/// </summary> | ||
public void ApplyProfile(CacheProfile profile) | ||
{ | ||
switch (profile) | ||
{ | ||
case CacheProfile.Mobile: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like some more "Dynamic" way:
|
||
// Low-RAM / mobile devices - aggressive cleanup | ||
_maxFreePages = 100; | ||
_idleBeforeEvict = TimeSpan.FromSeconds(25); | ||
_minCleanupInterval = TimeSpan.FromSeconds(2); | ||
_cleanupBatchSize = 96; | ||
break; | ||
|
||
case CacheProfile.Desktop: | ||
// Balanced defaults | ||
_maxFreePages = DEFAULT_MAX_FREE_PAGES; | ||
_idleBeforeEvict = DEFAULT_IDLE; | ||
_minCleanupInterval = DEFAULT_INT; | ||
_cleanupBatchSize = DEFAULT_BATCH; | ||
break; | ||
|
||
case CacheProfile.Server: | ||
// High throughput - less frequent but larger cleanups | ||
_maxFreePages = 360; | ||
_idleBeforeEvict = TimeSpan.FromSeconds(90); | ||
_minCleanupInterval = TimeSpan.FromSeconds(45); | ||
_cleanupBatchSize = 192; | ||
break; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Triggers cleanup if conditions are met: forced, cleanup signal set, | ||
/// free pages exceed limit, or operation threshold reached | ||
/// </summary> | ||
private void TryCleanupOnDemand(bool force = false) | ||
{ | ||
if (_disposed) | ||
return; | ||
|
||
var now = DateTime.UtcNow; | ||
var since = now - _lastCleanupRunUtc; | ||
|
||
if (!force && | ||
since < _minCleanupInterval && | ||
_free.Count <= _maxFreePages && | ||
!_cleanupSignal && | ||
_opsSinceLastCleanup < OPS_CLEANUP_STEP) | ||
{ | ||
return; | ||
} | ||
|
||
_cleanupSignal = false; | ||
Interlocked.Exchange(ref _opsSinceLastCleanup, 0); | ||
|
||
Cleanup(); | ||
} | ||
|
||
/// <summary> | ||
/// Performs bounded, non-reentrant cleanup over the free-list. | ||
/// Processes up to _cleanupBatchSize pages, evicting those idle longer than _idleBeforeEvict. | ||
/// Uses atomic eviction flags to prevent race conditions during cleanup. | ||
/// </summary> | ||
private void Cleanup() | ||
{ | ||
if (!_cleanLock.Wait(0)) return; | ||
try | ||
{ | ||
if (_free.IsEmpty) return; | ||
|
||
var now = DateTime.UtcNow; | ||
_lastCleanupRunUtc = now; | ||
|
||
var keep = new List<PageBuffer>(_cleanupBatchSize); | ||
int processed = 0; | ||
|
||
while (processed < _cleanupBatchSize && _free.TryDequeue(out var page)) | ||
{ | ||
if (!page.TryBeginEvict()) | ||
{ | ||
keep.Add(page); | ||
processed++; | ||
continue; | ||
} | ||
|
||
var freedAtTicks = page.Timestamp > 0 ? page.Timestamp : now.Ticks; | ||
var idle = now - new DateTime(freedAtTicks, DateTimeKind.Utc); | ||
|
||
if (idle < _idleBeforeEvict) | ||
{ | ||
page.MarkReusable(); | ||
keep.Add(page); | ||
} | ||
else | ||
{ | ||
try | ||
{ | ||
// Try to clear the page buffer to free memory | ||
// If Clear() fails (e.g., corrupted buffer, access violation), | ||
// we gracefully handle it by keeping the page for reuse instead of crashing | ||
page.Clear(); | ||
} | ||
catch | ||
{ | ||
page.MarkReusable(); | ||
keep.Add(page); | ||
} | ||
} | ||
|
||
processed++; | ||
} | ||
|
||
foreach (var p in keep) | ||
_free.Enqueue(p); | ||
} | ||
finally | ||
{ | ||
_cleanLock.Release(); | ||
} | ||
} | ||
|
||
#endregion | ||
|
||
private volatile bool _disposed; | ||
|
||
public void Dispose() | ||
{ | ||
_disposed = true; | ||
_cleanupSignal = false; | ||
|
||
try | ||
{ | ||
_cleanLock.Wait(); | ||
} | ||
finally | ||
{ | ||
_cleanLock.Dispose(); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_free.Count
enumerates the whole collection. Try to use a cheaper volatile int andInterlocked.
on it.