プロキシキャッシュのメモリ解放通知移譲とストリーム競合回避を含む管理責務対応
MfProxyEncoderでProxyCacheEntry生成時にメモリ解放通知コールバックを渡すよう変更しました。 一時ファイルサイズ取得をFileInfoからFile.OpenHandleとRandomAccess.GetLengthへ変更しました。 ProxyCacheEntryに解放通知用フィールドと追跡バイト数を追加しメモリ返却時に解放を通知するようにしました。 ProxyCacheEntryのIsInMemory判定をディスク未配置条件付きに変更しました。 ProxyCacheEntryのOpenReadでメモリ参照の競合を検知して再試行するループを導入しました。 ディスクパス設定時とメモリ書き出し完了時に遅延解放フラグを立ててTryReturnBuffer()を実行するようにしました。 TryReturnBuffer()をストリーム利用中は解放しない条件に改め必要時のみバッファ返却と解放通知を行うようにしました。 ProxyCacheManagerからMemoryBudget依存と重複したRecordDeallocation(long)呼び出しを削除しました。 PluginHostのProxyCacheManager生成引数を新しいコンストラクタに合わせて整理しました。
このコミットが含まれているのは:
4ファイル変更
+47
-22
検索条件に一致するファイルはありません
@@ -110,13 +110,14 @@ internal sealed class MfProxyEncoder : IProxyEncoder
|
||||
await transcodeOp;
|
||||
|
||||
float effectiveScale = (float)proxyWidth / origWidth;
|
||||
entry = new ProxyCacheEntry(inputPath, effectiveScale)
|
||||
entry = new ProxyCacheEntry(inputPath, effectiveScale, _memoryBudget.RecordDeallocation)
|
||||
{
|
||||
ProxyWidth = proxyWidth,
|
||||
ProxyHeight = proxyHeight
|
||||
};
|
||||
|
||||
var fileSize = new FileInfo(tempPath).Length;
|
||||
using var tempHandle = File.OpenHandle(tempPath, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
var fileSize = RandomAccess.GetLength(tempHandle);
|
||||
if (_memoryBudget.TryAllocate(fileSize))
|
||||
{
|
||||
byte[]? rented = null;
|
||||
|
||||
@@ -9,33 +9,39 @@ internal sealed class ProxyCacheEntry : IDisposable
|
||||
private byte[]? _memoryData;
|
||||
private int _memoryDataLength;
|
||||
private string? _diskPath;
|
||||
private readonly Action<long>? _onMemoryReleased;
|
||||
private long _cachedSize;
|
||||
private long _trackedMemoryBytes;
|
||||
private long _lastAccessedTicks = DateTime.UtcNow.Ticks;
|
||||
private readonly object _diskWriteLock = new();
|
||||
private int _disposed;
|
||||
private int _streamCount;
|
||||
private int _releaseMemoryWhenPossible;
|
||||
|
||||
internal string OriginalPath { get; }
|
||||
internal float Scale { get; }
|
||||
internal uint ProxyWidth { get; init; }
|
||||
internal uint ProxyHeight { get; init; }
|
||||
internal bool IsInMemory => Volatile.Read(ref _memoryData) is not null;
|
||||
internal bool IsInMemory => Volatile.Read(ref _memoryData) is not null && Volatile.Read(ref _diskPath) is null;
|
||||
internal bool IsValid => Volatile.Read(ref _disposed) == 0 && (Volatile.Read(ref _memoryData) is not null || Volatile.Read(ref _diskPath) is not null);
|
||||
internal long DataSize => Interlocked.Read(ref _cachedSize);
|
||||
internal DateTime CreatedAt { get; } = DateTime.UtcNow;
|
||||
internal DateTime LastAccessedAt => new(Interlocked.Read(ref _lastAccessedTicks));
|
||||
|
||||
internal ProxyCacheEntry(string originalPath, float scale)
|
||||
internal ProxyCacheEntry(string originalPath, float scale, Action<long>? onMemoryReleased = null)
|
||||
{
|
||||
OriginalPath = originalPath;
|
||||
Scale = scale;
|
||||
_onMemoryReleased = onMemoryReleased;
|
||||
}
|
||||
|
||||
internal void SetMemoryData(byte[] pooledBuffer, int length)
|
||||
{
|
||||
Interlocked.Exchange(ref _trackedMemoryBytes, length);
|
||||
Interlocked.Exchange(ref _cachedSize, (long)length);
|
||||
Volatile.Write(ref _memoryDataLength, length);
|
||||
Volatile.Write(ref _diskPath, null);
|
||||
Volatile.Write(ref _releaseMemoryWhenPossible, 0);
|
||||
Volatile.Write(ref _memoryData, pooledBuffer);
|
||||
UpdateLastAccess();
|
||||
}
|
||||
@@ -43,8 +49,9 @@ internal sealed class ProxyCacheEntry : IDisposable
|
||||
internal void SetDiskPath(string path, long size)
|
||||
{
|
||||
Interlocked.Exchange(ref _cachedSize, size);
|
||||
Volatile.Write(ref _memoryData, null);
|
||||
Volatile.Write(ref _diskPath, path);
|
||||
Volatile.Write(ref _releaseMemoryWhenPossible, 1);
|
||||
TryReturnBuffer();
|
||||
UpdateLastAccess();
|
||||
}
|
||||
|
||||
@@ -53,15 +60,26 @@ internal sealed class ProxyCacheEntry : IDisposable
|
||||
ObjectDisposedException.ThrowIf(Volatile.Read(ref _disposed) != 0, this);
|
||||
UpdateLastAccess();
|
||||
|
||||
var mem = Volatile.Read(ref _memoryData);
|
||||
if (mem is not null)
|
||||
while (true)
|
||||
{
|
||||
var mem = Volatile.Read(ref _memoryData);
|
||||
if (mem is null)
|
||||
break;
|
||||
|
||||
Interlocked.Increment(ref _streamCount);
|
||||
|
||||
if (Volatile.Read(ref _disposed) != 0)
|
||||
{
|
||||
OnStreamClosed();
|
||||
throw new ObjectDisposedException(nameof(ProxyCacheEntry));
|
||||
}
|
||||
|
||||
if (!ReferenceEquals(mem, Volatile.Read(ref _memoryData)))
|
||||
{
|
||||
OnStreamClosed();
|
||||
continue;
|
||||
}
|
||||
|
||||
var len = Volatile.Read(ref _memoryDataLength);
|
||||
return new PooledMemoryStream(mem, len, this);
|
||||
}
|
||||
@@ -104,6 +122,8 @@ internal sealed class ProxyCacheEntry : IDisposable
|
||||
fs.Write(mem, 0, Volatile.Read(ref _memoryDataLength));
|
||||
|
||||
Volatile.Write(ref _diskPath, tempPath);
|
||||
Volatile.Write(ref _releaseMemoryWhenPossible, 1);
|
||||
TryReturnBuffer();
|
||||
return tempPath;
|
||||
}
|
||||
}
|
||||
@@ -127,12 +147,23 @@ internal sealed class ProxyCacheEntry : IDisposable
|
||||
|
||||
private void TryReturnBuffer()
|
||||
{
|
||||
if (Volatile.Read(ref _disposed) != 0 && Volatile.Read(ref _streamCount) == 0)
|
||||
{
|
||||
var buf = Interlocked.Exchange(ref _memoryData, null);
|
||||
if (buf is not null)
|
||||
BufferPool.Return(buf);
|
||||
}
|
||||
if (Volatile.Read(ref _streamCount) != 0)
|
||||
return;
|
||||
|
||||
if (Volatile.Read(ref _disposed) == 0 && Volatile.Read(ref _releaseMemoryWhenPossible) == 0)
|
||||
return;
|
||||
|
||||
var buf = Interlocked.Exchange(ref _memoryData, null);
|
||||
if (buf is null)
|
||||
return;
|
||||
|
||||
Volatile.Write(ref _memoryDataLength, 0);
|
||||
BufferPool.Return(buf);
|
||||
|
||||
var released = Interlocked.Exchange(ref _trackedMemoryBytes, 0);
|
||||
if (released > 0)
|
||||
_onMemoryReleased?.Invoke(released);
|
||||
|
||||
}
|
||||
|
||||
private void UpdateLastAccess() =>
|
||||
|
||||
@@ -5,7 +5,6 @@ using System.IO;
|
||||
using System.Windows;
|
||||
using ZeroDiskProxy.Interfaces;
|
||||
using ZeroDiskProxy.Localization;
|
||||
using ZeroDiskProxy.Memory;
|
||||
using ZeroDiskProxy.Progress;
|
||||
using ZeroDiskProxy.Settings;
|
||||
|
||||
@@ -26,16 +25,14 @@ internal sealed class ProxyCacheManager : IDisposable
|
||||
private readonly ConcurrentDictionary<CacheKey, ProxyCacheEntry> _cache = new();
|
||||
private readonly ConcurrentDictionary<CacheKey, CancellationTokenSource> _pendingGenerations = new();
|
||||
private readonly IProxyEncoderFactory _encoderFactory;
|
||||
private readonly MemoryBudget _memoryBudget;
|
||||
private int _disposed;
|
||||
|
||||
internal ObservableCollection<ProxyGenerationItem> ActiveGenerations { get; } = [];
|
||||
internal event Action<string, ProxyCacheEntry>? ProxyCompleted;
|
||||
|
||||
internal ProxyCacheManager(IProxyEncoderFactory encoderFactory, MemoryBudget memoryBudget)
|
||||
internal ProxyCacheManager(IProxyEncoderFactory encoderFactory)
|
||||
{
|
||||
_encoderFactory = encoderFactory;
|
||||
_memoryBudget = memoryBudget;
|
||||
}
|
||||
|
||||
private static CacheKey MakeKey(string path, float scale) =>
|
||||
@@ -98,7 +95,6 @@ internal sealed class ProxyCacheManager : IDisposable
|
||||
var stored = _cache.GetOrAdd(key, entry);
|
||||
if (!ReferenceEquals(stored, entry))
|
||||
{
|
||||
_memoryBudget.RecordDeallocation(entry.DataSize);
|
||||
entry.Dispose();
|
||||
}
|
||||
|
||||
@@ -161,9 +157,7 @@ internal sealed class ProxyCacheManager : IDisposable
|
||||
if (!_cache.TryRemove(key, out var entry))
|
||||
return;
|
||||
|
||||
var size = entry.DataSize;
|
||||
entry.Dispose();
|
||||
_memoryBudget.RecordDeallocation(size);
|
||||
}
|
||||
|
||||
internal (int count, long totalSize) ClearAll()
|
||||
@@ -189,7 +183,6 @@ internal sealed class ProxyCacheManager : IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
_memoryBudget.RecordDeallocation(totalSize);
|
||||
return (count, totalSize);
|
||||
}
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ internal sealed class PluginHost : IDisposable
|
||||
Budget = budget;
|
||||
ExportDetector = exportDetector;
|
||||
FallbackDirectory = fallbackDirectory;
|
||||
CacheManager = new ProxyCacheManager(encoderFactory, Budget);
|
||||
CacheManager = new ProxyCacheManager(encoderFactory);
|
||||
|
||||
CacheManager.ActiveGenerations.CollectionChanged += OnGenerationsChanged;
|
||||
}
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする