Interactive 2D wave optics simulator. Turn any image into a refractive scene, add sources, watch waves refract, diffract and interfere. Probes, intensity maps, video export.
C#
A new run's store allocates its memory only once the old store's is released
Alexander Graefd1cd1b790cd2
diff --git a/CLAUDE.md b/CLAUDE.mdindex adb2b5f..4069b44 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -930,7 +930,13 @@ FrameStore (temporary file in %LOCALAPPDATA%\WaveDudeNext\cache, DeleteOnClose,sides and flushed everything on unmap (a minute-long freeze). Size: a 1920×1080 run of 2500 steps at`VisualizeEvery` 2 with both planes is **10.4 GB**, in RAM here. The file is deleted ondispose/exit/crash (`DeleteOnClose`); `FrameCache.Sweep` at start-up removes leftovers; the window- disposes a store on the thread pool, never on the UI thread. When a run is replaced the+ disposes a store on the thread pool, never on the UI thread; `DiscardRun` returns the task of that disposal+ and the next store takes it as `memoryReleased` (`FrameStore.Create`, 2026-10-16): its block preparer+ allocates nothing before the old store's memory is given back, so the reclaimed bytes the budget counts on+ are really free when the first block is made (before, the two overlapped for the tens of milliseconds the old+ run needed to stop, which a machine at its commit limit lost to the disk; the harness writes to a store+ whose release is pending and sees the write wait, and disposes one whose release never comes at once).+ When a run is replaced thewindow drops `Run` before clearing the stale flag, so the preview never falls back to the old storefor a moment (that showed the old frame for a second after every edit, 2026-10-09).- **Numbers (RTX 2080, 64 GB RAM, SATA SSD RAID, Debug build, 2026-10-09)**, fromdiff --git a/WaveDudeNext.WPF/MainWindow.Simulation.cs b/WaveDudeNext.WPF/MainWindow.Simulation.csindex 4979309..1367d64 100644--- a/WaveDudeNext.WPF/MainWindow.Simulation.cs+++ b/WaveDudeNext.WPF/MainWindow.Simulation.cs@@ -92,15 +92,15 @@ public partial class MainWindowRunSpec spec = RunSpec.Create(Project, LoadSceneImage(), Settings.Default.UseGpu, trackIntensity, quality);// the frames stay in memory up to the budget (the user's share of the RAM, capped by what is free now); only// the rest needs room on the cache drive (2026-10-14). The outgoing run's frames count as free: DiscardRun- // below gives them back before the new store's blocks are touched (2026-10-16)+ // below gives them back, and the new store allocates its memory tier only once they are gone (2026-10-16)long budget = FrameMemory.Budget(Settings.Default.FrameMemoryPercent, Run?.Store.RamBytes ?? 0);int ramFrames = Math.Min(spec.StoredFrameCount, FrameStore.FramesInBudget(spec.Shape, spec.Layers, budget));if (ramFrames < spec.StoredFrameCount){FrameCache.CheckSpace(FrameStore.SizeOf(spec.Shape, spec.Layers, spec.StoredFrameCount - ramFrames));}- DiscardRun();- FrameStore store = FrameStore.Create(FrameCache.NewPath(), spec.Shape, spec.Layers, spec.StoredFrameCount, spec.CaptureEvery, budget);+ Task memoryReleased = DiscardRun();+ FrameStore store = FrameStore.Create(FrameCache.NewPath(), spec.Shape, spec.Layers, spec.StoredFrameCount, spec.CaptureEvery, budget, memoryReleased);// the run's progress lands on the UI thread below input priority (2026-10-16): posted at normal priority, the// frame counts kept the input queue starved for the whole run, since a new one was posted the moment the last// was handled, and every click and key was delivered after the run (the watchdog's input-priority ticks were@@ -184,7 +184,8 @@ public partial class MainWindow/// Drops the current run: stops it and disposes its store (deleting the cache file) once the worker has/// let go of it. Called before a new run, when the project is replaced and on closing./// </summary>- private void DiscardRun()+ /// <returns>Completes when the old store is disposed and its memory given back (at once when there was no run); the next run's store waits for it before allocating its memory tier.</returns>+ private Task DiscardRun(){if (Export?.IsActive == true){@@ -204,11 +205,11 @@ public partial class MainWindowIsProbesStale = false;if (old == null){- return;+ return Task.CompletedTask;}old.Stop();// on the thread pool: unmapping a run's cache file is quick, but never the UI thread's wait- old.Completion.ContinueWith(_ =>+ return old.Completion.ContinueWith(_ =>{old.Store.Dispose();old.Dispose();diff --git a/WaveDudeNext/Runs/FrameStore.cs b/WaveDudeNext/Runs/FrameStore.csindex 392cb8c..027bd55 100644--- a/WaveDudeNext/Runs/FrameStore.cs+++ b/WaveDudeNext/Runs/FrameStore.cs@@ -77,6 +77,7 @@ public sealed unsafe class FrameStore : IFrameSource, IDisposableprivate readonly BlockingCollection<IntPtr> _prepared = new(PrepareDepth);private readonly CancellationTokenSource _prepareCancel = new();private readonly Task _preparer;+ private readonly Task? _memoryReleased;private readonly byte[] _staging;private byte[]? _staging2;private FileStream? _file;@@ -85,8 +86,9 @@ public sealed unsafe class FrameStore : IFrameSource, IDisposableprivate int _count;private bool _disposed;- private FrameStore(string path, FrameShape shape, FrameLayers layers, int capacity, int visualizeEvery, long ramBudget)+ private FrameStore(string path, FrameShape shape, FrameLayers layers, int capacity, int visualizeEvery, long ramBudget, Task? memoryReleased){+ _memoryReleased = memoryReleased;shape.Validate();ArgumentOutOfRangeException.ThrowIfLessThan(capacity, 1);ArgumentOutOfRangeException.ThrowIfLessThan(visualizeEvery, 1);@@ -114,15 +116,32 @@ public sealed unsafe class FrameStore : IFrameSource, IDisposable/// <summary>/// The background producer of memory blocks: allocates one per frame of the memory tier, touches every page- /// so the writer never takes a page fault, and queues it, at most <see cref="PrepareDepth"/> ahead. Ends when- /// every block is made, the store is disposed, or an allocation fails (the commit limit), after which the- /// writer spills the remaining frames to the file.+ /// so the writer never takes a page fault, and queues it, at most <see cref="PrepareDepth"/> ahead. Allocates+ /// nothing before the memory the budget counts on is released (<see cref="_memoryReleased"/>, the outgoing run's+ /// store; 2026-10-16: the budget had counted it as free while the old store still held it, a race of tens of+ /// milliseconds that a machine at its commit limit lost). Ends when every block is made, the store is disposed,+ /// or an allocation fails (the commit limit), after which the writer spills the remaining frames to the file./// </summary>private void PrepareBlocks(){CancellationToken token = _prepareCancel.Token;try{+ if (_memoryReleased != null)+ {+ try+ {+ _memoryReleased.Wait(token);+ }+ catch (OperationCanceledException)+ {+ return;+ }+ catch (AggregateException)+ {+ // the release failed; the memory is in whatever state it is, the allocations below find out+ }+ }for (int i = 0; i < RamFrames && !token.IsCancellationRequested; i++){void* block;@@ -178,13 +197,14 @@ public sealed unsafe class FrameStore : IFrameSource, IDisposable/// <param name="capacity">Number of frames the run produces (<see cref="RunSpec.FrameCount"/>).</param>/// <param name="visualizeEvery">Steps per frame, kept for <see cref="StepOf"/>.</param>/// <param name="ramBudget">Bytes of memory the frames may take; null takes <see cref="FrameMemory.Budget(int, long)"/> at the default share.</param>+ /// <param name="memoryReleased">Completes when memory the budget counts on is released (the outgoing run's store being disposed); the memory tier is not allocated before. Null: nothing to wait for.</param>/// <exception cref="IOException">The file cannot be created or sized (for instance no disk space).</exception>- public static FrameStore Create(string path, FrameShape shape, FrameLayers layers, int capacity, int visualizeEvery, long? ramBudget = null) =>- new(path, shape, layers, capacity, visualizeEvery, ramBudget ?? FrameMemory.Budget(FrameMemory.DefaultPercent));+ public static FrameStore Create(string path, FrameShape shape, FrameLayers layers, int capacity, int visualizeEvery, long? ramBudget = null, Task? memoryReleased = null) =>+ new(path, shape, layers, capacity, visualizeEvery, ramBudget ?? FrameMemory.Budget(FrameMemory.DefaultPercent), memoryReleased);- /// <summary>Creates a full-quality store (every cell as a <see cref="Half"/>) for a canvas of <paramref name="width"/> × <paramref name="height"/>; see <see cref="Create(string, FrameShape, FrameLayers, int, int, long?)"/>.</summary>- public static FrameStore Create(string path, int width, int height, FrameLayers layers, int capacity, int visualizeEvery, long? ramBudget = null) =>- Create(path, FrameShape.Full(width, height), layers, capacity, visualizeEvery, ramBudget);+ /// <summary>Creates a full-quality store (every cell as a <see cref="Half"/>) for a canvas of <paramref name="width"/> × <paramref name="height"/>; see <see cref="Create(string, FrameShape, FrameLayers, int, int, long?, Task?)"/>.</summary>+ public static FrameStore Create(string path, int width, int height, FrameLayers layers, int capacity, int visualizeEvery, long? ramBudget = null, Task? memoryReleased = null) =>+ Create(path, FrameShape.Full(width, height), layers, capacity, visualizeEvery, ramBudget, memoryReleased);/// <summary>Bytes a file holding <paramref name="capacity"/> frames of this shape takes, for <see cref="FrameCache.CheckSpace"/> (the spilled part of a run).</summary>public static long SizeOf(FrameShape shape, FrameLayers layers, int capacity) => HeaderSize + capacity * FrameBytesOf(shape, layers);diff --git a/_tools/library_check/Program.cs b/_tools/library_check/Program.csindex 407f8a7..0b8f545 100644--- a/_tools/library_check/Program.cs+++ b/_tools/library_check/Program.cs@@ -733,6 +733,24 @@ internal static class Program&& FrameMemory.Budget(1 * gb, 16 * gb, FrameMemory.Automatic, 0) == 0&& FrameMemory.Budget(1 * gb, 16 * gb, 100, 512L << 20) == 0,"budget: reclaimed memory of the outgoing run counts as available, the share still caps it");+ // the memory tier waits for that reclaimed memory (2026-10-16): a store made with a release task allocates+ // nothing and a write to it waits until the task completes, then the frame lands in memory, not in a file+ path = Path.Combine(Path.GetTempPath(), "library_check-wait-" + Guid.NewGuid().ToString("N") + FrameCache.Extension);+ var release = new TaskCompletionSource();+ using (var store = FrameStore.Create(path, scene.Width, scene.Height, FrameLayers.Field, 3, 1, ramBudget: 1L << 30, memoryReleased: release.Task))+ {+ Task write = Task.Run(() => store.Write(0, cpu.FieldData, null, cpu.Stride));+ bool waited = !write.Wait(300) && store.Count == 0;+ release.SetResult();+ bool landed = write.Wait(5000) && store.Count == 1 && store.FramesInMemory == 1 && !store.HasFile;+ Check(waited && landed, "the memory tier waits for the outgoing run's memory before its first allocation, then the frame lands in memory");+ }+ // a store whose release never comes is still disposed at once (the preparer's wait is cancelled)+ path = Path.Combine(Path.GetTempPath(), "library_check-never-" + Guid.NewGuid().ToString("N") + FrameCache.Extension);+ var never = FrameStore.Create(path, scene.Width, scene.Height, FrameLayers.Field, 3, 1, ramBudget: 1L << 30, memoryReleased: new TaskCompletionSource().Task);+ var disposeClock = Stopwatch.StartNew();+ never.Dispose();+ Check(disposeClock.ElapsedMilliseconds < 1000, "a store still waiting for the released memory disposes at once (" + disposeClock.ElapsedMilliseconds + " ms)");// a full-HD store of 1 GB: the writes must run at memory speed and the dispose must not flush the// dirty pages to disk (the file is deleted anyway; this used to take a minute for a 10 GB run)