Compact sessions within Box3D world capacity

This commit is contained in:
Meta Vens 2026-07-22 00:01:47 +01:00
commit ebd74df319
2 changed files with 17 additions and 10 deletions

View file

@ -164,11 +164,13 @@ public sealed class YmirSession : IDisposable
}
/// <summary>
/// Creates a fresh persistence generation from the current Box3D projection when
/// no contact or unstepped mutation state would be lost. The caller owns the
/// returned session and remains responsible for disposing this session after swap.
/// Replaces this session with a fresh persistence generation from the current
/// Box3D projection when no contact or unstepped mutation state would be lost.
/// The native world owned by this session is released before the replacement is
/// allocated so compaction cannot exceed Box3D's fixed world capacity. A successful
/// call leaves this session disposed and transfers ownership to the caller.
/// </summary>
public bool TryCreateCompactedPersistenceBaseline(out YmirSession? compacted)
public bool TryReplaceWithCompactedPersistenceBaseline(out YmirSession? compacted)
{
lock (_gate)
{
@ -182,10 +184,13 @@ public sealed class YmirSession : IDisposable
return false;
}
compacted = new YmirSession(new YmirSessionCreateRequest(
var request = new YmirSessionCreateRequest(
_sessionId,
_bodies.Values.OrderBy(body => body.Id, StringComparer.Ordinal).ToArray(),
_time));
_time);
_native.Dispose();
_disposed = true;
compacted = new YmirSession(request);
return true;
}
}

View file

@ -205,15 +205,17 @@ public sealed class YmirSessionTests
"compact-quiescent", [Body("ship", velocityX: 2.0f)]));
original.Step(Step("step-1", 0, 0.1f));
original.Step(Step("step-2", 1, 0.1f));
var expectedWorld = original.Snapshot();
Assert.True(original.TryCreateCompactedPersistenceBaseline(out var compacted));
Assert.True(original.TryReplaceWithCompactedPersistenceBaseline(out var compacted));
using (compacted!)
{
var baseline = compacted!;
Assert.NotEqual(original.Info.SessionGeneration, baseline.Info.SessionGeneration);
Assert.True(original.Info.IsDisposed);
Assert.Equal(0, baseline.Info.Revision);
Assert.Equal(0, baseline.PersistenceJournalEntryCount);
AssertWorldEqual(original.Snapshot(), baseline.Snapshot());
AssertWorldEqual(expectedWorld, baseline.Snapshot());
var capture = baseline.CapturePersistence(0);
Assert.Null(capture.JournalChunk);
@ -230,14 +232,14 @@ public sealed class YmirSessionTests
"compact-contact",
[Body("ship", positionX: 0.0f, isStatic: true), Body("pickup", positionX: 1.5f)]));
touching.Step(Step("begin", 0, 0.01f));
Assert.False(touching.TryCreateCompactedPersistenceBaseline(out var contactBaseline));
Assert.False(touching.TryReplaceWithCompactedPersistenceBaseline(out var contactBaseline));
Assert.Null(contactBaseline);
using var pendingForce = YmirSession.Create(new YmirSessionCreateRequest(
"compact-force", [Body("ship")]));
pendingForce.ApplyForce(new YmirApplyForceCommand(
Header("force", 0), "ship", new Vec2(10.0f, 0.0f)));
Assert.False(pendingForce.TryCreateCompactedPersistenceBaseline(out var forceBaseline));
Assert.False(pendingForce.TryReplaceWithCompactedPersistenceBaseline(out var forceBaseline));
Assert.Null(forceBaseline);
}