@series-inc/rundot-syncplay 6.0.0-rc.113 → 6.0.0-rc.116
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/native/physics/box3d_adapter.cpp +24 -0
- package/core/native/physics/box3d_adapter.hpp +2 -0
- package/core/native/physics/box3d_world_backend.cpp +22 -4
- package/core/native/physics/checkpoint_schema.hpp +14 -2
- package/core/native/physics/geometry_memory.hpp +12 -0
- package/core/native/physics/world.cpp +103 -31
- package/core/native/physics/world.hpp +68 -6
- package/core/native/physics/world_strong_digest.hpp +271 -94
- package/core/src/physics/abi-constants.mjs +2 -1
- package/core/src/physics/physics-profile-wasm-module.generated.mjs +5 -5
- package/core/src/physics/physics-profile.wasm +0 -0
- package/core/src/physics/physics-wasm-module.generated.mjs +5 -5
- package/core/src/physics/physics.wasm +0 -0
- package/core/src/physics2d/physics2d-wasm-module.generated.mjs +1 -1
- package/dist/authority-room.d.ts +5 -0
- package/dist/authority-room.js +25 -19
- package/dist/session-snapshot.d.ts +1 -1
- package/dist/session-snapshot.js +5 -1
- package/dist/snapshot-config.d.ts +4 -2
- package/dist/snapshot-config.js +4 -2
- package/package.json +1 -1
|
@@ -15293,6 +15293,30 @@ std::int32_t syncplay_adapter_canonical_materialize_digest(void* buffer, std::ui
|
|
|
15293
15293
|
return digestCursor.ok ? kSyncplayAdapterOk : kSyncplayAdapterCheckpointCorrupt;
|
|
15294
15294
|
}
|
|
15295
15295
|
|
|
15296
|
+
std::int32_t syncplay_adapter_canonical_defer_digest(void* buffer, std::uint32_t size) {
|
|
15297
|
+
if (buffer == nullptr || size < kCanonicalPrefixSize) return kSyncplayAdapterBadArgument;
|
|
15298
|
+
auto* bytes = static_cast<unsigned char*>(buffer);
|
|
15299
|
+
if (std::memcmp(bytes, "SYNCB3C1", 8) != 0) return kSyncplayAdapterCheckpointVersionMismatch;
|
|
15300
|
+
Cursor forkCursor{bytes, kCanonicalPrefixSize, 60, true};
|
|
15301
|
+
const std::uint32_t forkSize = getU32(forkCursor);
|
|
15302
|
+
if (!forkCursor.ok || forkSize == 0 || forkSize > size - kCanonicalPrefixSize) {
|
|
15303
|
+
return kSyncplayAdapterCheckpointCorrupt;
|
|
15304
|
+
}
|
|
15305
|
+
// The supplied digest is the deferred form (zero) or the digest of the fork section. Any other value is a
|
|
15306
|
+
// fork section that does not match its digest: refuse it, as a verifying restore does, before writing zero.
|
|
15307
|
+
Cursor readCursor{bytes, kCanonicalPrefixSize, kCanonicalExtrasCountOffset - 8, true};
|
|
15308
|
+
const std::uint64_t supplied = getU64(readCursor);
|
|
15309
|
+
if (!readCursor.ok) return kSyncplayAdapterCheckpointCorrupt;
|
|
15310
|
+
if (supplied != 0
|
|
15311
|
+
&& supplied != syncplay_fnv1a64(bytes + kCanonicalPrefixSize, forkSize, SYNCPLAY_FNV1A64_OFFSET)) {
|
|
15312
|
+
g_canonicalError = "geometry digest";
|
|
15313
|
+
return kSyncplayAdapterCheckpointCorrupt;
|
|
15314
|
+
}
|
|
15315
|
+
Cursor digestCursor{bytes, kCanonicalPrefixSize, kCanonicalExtrasCountOffset - 8, true};
|
|
15316
|
+
putU64(digestCursor, 0);
|
|
15317
|
+
return digestCursor.ok ? kSyncplayAdapterOk : kSyncplayAdapterCheckpointCorrupt;
|
|
15318
|
+
}
|
|
15319
|
+
|
|
15296
15320
|
std::int32_t syncplay_adapter_sha256(const void* data, std::uint32_t size,
|
|
15297
15321
|
unsigned char out[32]) {
|
|
15298
15322
|
return strongDigest(data, size, out) ? kSyncplayAdapterOk : kSyncplayAdapterBadArgument;
|
|
@@ -1146,6 +1146,8 @@ std::int32_t syncplay_adapter_canonical_save_ex(std::uint32_t slot, void* buffer
|
|
|
1146
1146
|
std::uint32_t* outSize,
|
|
1147
1147
|
std::uint32_t computeForkDigest);
|
|
1148
1148
|
std::int32_t syncplay_adapter_canonical_materialize_digest(void* buffer, std::uint32_t size);
|
|
1149
|
+
// Writes zero to the fork digest of a canonical image: the deferred form that a live capture stores.
|
|
1150
|
+
std::int32_t syncplay_adapter_canonical_defer_digest(void* buffer, std::uint32_t size);
|
|
1149
1151
|
std::int32_t syncplay_adapter_sha256(const void* data, std::uint32_t size,
|
|
1150
1152
|
unsigned char out[32]);
|
|
1151
1153
|
std::int32_t syncplay_adapter_canonical_strong_digest(std::uint32_t slot,
|
|
@@ -4422,29 +4422,47 @@ std::uint32_t restoreCanonicalImageTrusted(World& world,
|
|
|
4422
4422
|
return restoreCanonicalImageImpl(world, bytes, false);
|
|
4423
4423
|
}
|
|
4424
4424
|
|
|
4425
|
-
|
|
4425
|
+
// The adapter image inside a backend state section (and inside a portal image), or an empty span for a
|
|
4426
|
+
// section with no image. False for a corrupt section.
|
|
4427
|
+
bool canonicalAdapterBytes(std::span<std::uint8_t> bytes, std::span<std::uint8_t>& adapterBytes) noexcept {
|
|
4426
4428
|
BackendStateSection section;
|
|
4427
4429
|
try {
|
|
4428
4430
|
if (!readBackendStateSection(bytes, section)) return false;
|
|
4429
4431
|
} catch (...) {
|
|
4430
4432
|
return false;
|
|
4431
4433
|
}
|
|
4434
|
+
adapterBytes = {};
|
|
4432
4435
|
if ((section.flags & kBackendStateNoImage) != 0) return true;
|
|
4433
4436
|
if (section.inner.empty()) return false;
|
|
4434
4437
|
const std::size_t innerOffset =
|
|
4435
4438
|
static_cast<std::size_t>(section.inner.data() - bytes.data());
|
|
4436
|
-
|
|
4439
|
+
adapterBytes = bytes.subspan(innerOffset, section.inner.size());
|
|
4437
4440
|
if (adapterBytes.size() >= 12 && readStateU32(adapterBytes, 0) == kPortalImageMagic) {
|
|
4438
4441
|
const std::uint32_t adapterSize = readStateU32(adapterBytes, 8);
|
|
4439
4442
|
if (adapterSize == 0 || adapterSize > adapterBytes.size() - 12) return false;
|
|
4440
4443
|
adapterBytes = adapterBytes.subspan(12, adapterSize);
|
|
4441
4444
|
}
|
|
4442
|
-
|
|
4445
|
+
return adapterBytes.size() <= 0xffffffffu;
|
|
4446
|
+
}
|
|
4447
|
+
|
|
4448
|
+
bool materializeCanonicalImageDigest(std::span<std::uint8_t> bytes) noexcept {
|
|
4449
|
+
std::span<std::uint8_t> adapterBytes;
|
|
4450
|
+
if (!canonicalAdapterBytes(bytes, adapterBytes)) return false;
|
|
4451
|
+
if (adapterBytes.empty()) return true;
|
|
4443
4452
|
return syncplay_adapter_canonical_materialize_digest(
|
|
4444
4453
|
adapterBytes.data(), static_cast<std::uint32_t>(adapterBytes.size()))
|
|
4445
4454
|
== kSyncplayAdapterOk;
|
|
4446
4455
|
}
|
|
4447
4456
|
|
|
4457
|
+
bool deferCanonicalImageDigest(std::span<std::uint8_t> bytes) noexcept {
|
|
4458
|
+
std::span<std::uint8_t> adapterBytes;
|
|
4459
|
+
if (!canonicalAdapterBytes(bytes, adapterBytes)) return false;
|
|
4460
|
+
if (adapterBytes.empty()) return true;
|
|
4461
|
+
return syncplay_adapter_canonical_defer_digest(
|
|
4462
|
+
adapterBytes.data(), static_cast<std::uint32_t>(adapterBytes.size()))
|
|
4463
|
+
== kSyncplayAdapterOk;
|
|
4464
|
+
}
|
|
4465
|
+
|
|
4448
4466
|
// The world accepted the restore (bridge `commitRestore`): the staged mirror becomes the mirror of the
|
|
4449
4467
|
// world, and the mirror that it replaces goes with its adapter world. Nothing
|
|
4450
4468
|
// here can fail, so the world calls it with the commit of its public state.
|
|
@@ -4929,7 +4947,7 @@ const Box3dCanonicalBridge kBox3dBridge{
|
|
|
4929
4947
|
rebaseRevision, holdStep, endHeldStep, reserveReplacedJoints, markReplacedJoints, noteVelocityWrites,
|
|
4930
4948
|
noteVelocityTransform, prepareRestore,
|
|
4931
4949
|
backendStateEpoch, restoreCanonicalImageTrusted, materializeCanonicalImageDigest,
|
|
4932
|
-
beginTransactionCounters, endTransactionCounters, effectiveAngularVelocity,
|
|
4950
|
+
beginTransactionCounters, endTransactionCounters, effectiveAngularVelocity, deferCanonicalImageDigest,
|
|
4933
4951
|
};
|
|
4934
4952
|
|
|
4935
4953
|
struct Box3dBridgeInstaller {
|
|
@@ -20,7 +20,14 @@ namespace rundot::kinetix::physics {
|
|
|
20
20
|
// The topology ids, definition signatures and body materials sections are each one leaf
|
|
21
21
|
// (kStrongSectionLeafDomain, then the section bytes of revision 5). The static slab keeps those leaves.
|
|
22
22
|
// The contact event section is a list of chunk leaves too (kStrongContactEventChunkDomain).
|
|
23
|
-
|
|
23
|
+
// Revision 7: a contact cache chunk holds the rows of one body pair (at most 8). The chunk leaves of the
|
|
24
|
+
// contact cache and the contact events, and the body tile leaves, are hashed in groups (kStrongLeafGroupDomain,
|
|
25
|
+
// group ends a function of the leaves), so a digest hashes only the groups that changed. The joints, the
|
|
26
|
+
// joint warm state and the joint reactions are section leaves. The canonical Box3D image is hashed per stored
|
|
27
|
+
// block (kStrongBox3dBlockDomain, block ends a function of the image bytes), and each block keeps its leaf,
|
|
28
|
+
// so an image with few changed blocks costs only those blocks. A stored image holds the deferred (zero) fork
|
|
29
|
+
// digest, so the blocks hash the same bytes on every client.
|
|
30
|
+
inline constexpr std::uint32_t kWorldStrongDigestAlgorithmRevision = 7;
|
|
24
31
|
inline constexpr char kStrongCheckpointDomain[] = "kinetix.world-checkpoint/1";
|
|
25
32
|
inline constexpr char kStrongRecordDomain[] = "kinetix.geometry-record/1";
|
|
26
33
|
inline constexpr char kStrongRootDomain[] = "kinetix.geometry-root/1";
|
|
@@ -29,6 +36,8 @@ inline constexpr char kStrongTileDomain[] = "kinetix.body-tile/1";
|
|
|
29
36
|
inline constexpr char kStrongContactCacheChunkDomain[] = "kinetix.contact-cache-chunk/1";
|
|
30
37
|
inline constexpr char kStrongSectionLeafDomain[] = "kinetix.strong-section/1";
|
|
31
38
|
inline constexpr char kStrongContactEventChunkDomain[] = "kinetix.contact-event-chunk/1";
|
|
39
|
+
inline constexpr char kStrongLeafGroupDomain[] = "kinetix.leaf-group/1";
|
|
40
|
+
inline constexpr char kStrongBox3dBlockDomain[] = "kinetix.soft-step-block/1";
|
|
32
41
|
|
|
33
42
|
// The canonical Box3D section of a portable snapshot (wire versions 30 to 35). One fixed-width header and
|
|
34
43
|
// then the payload, so a corrupt length field fails before any read of the payload:
|
|
@@ -125,6 +134,8 @@ static_assert(sizeof(kStrongTileDomain) - 1 == 19);
|
|
|
125
134
|
static_assert(sizeof(kStrongContactCacheChunkDomain) - 1 == 29);
|
|
126
135
|
static_assert(sizeof(kStrongSectionLeafDomain) - 1 == 24);
|
|
127
136
|
static_assert(sizeof(kStrongContactEventChunkDomain) - 1 == 29);
|
|
137
|
+
static_assert(sizeof(kStrongLeafGroupDomain) - 1 == 20);
|
|
138
|
+
static_assert(sizeof(kStrongBox3dBlockDomain) - 1 == 25);
|
|
128
139
|
|
|
129
140
|
// One field list drives the FNV visitor and the strong visitor.
|
|
130
141
|
#define RUNDOT_CHECKPOINT_FIELDS(X) \
|
|
@@ -352,7 +363,8 @@ inline void checkpointRecordSchemaGuard(const CheckpointProfile& value) noexcept
|
|
|
352
363
|
[[maybe_unused]] const auto& [abiVersion, numericProfile, idLength, id, geometryDigest] = value;
|
|
353
364
|
}
|
|
354
365
|
inline void checkpointRecordSchemaGuard(const BodyTile& value) noexcept {
|
|
355
|
-
|
|
366
|
+
// serial names the tile for the strong digest cache: derived identity, never serialized.
|
|
367
|
+
[[maybe_unused]] const auto& [memoState, logicalCount, physicalCount, memoLength, memo, values, serial] = value;
|
|
356
368
|
}
|
|
357
369
|
inline void checkpointRecordSchemaGuard(const CheckpointBodyTable& value) noexcept {
|
|
358
370
|
[[maybe_unused]] const auto& [tileValues, valueCount, tiles, tileAllocationBytes,
|
|
@@ -464,6 +464,18 @@ class DomainAllocator {
|
|
|
464
464
|
bool operator!=(const DomainAllocator<U, Domain>&) const noexcept { return false; }
|
|
465
465
|
};
|
|
466
466
|
|
|
467
|
+
// Raw checkpoint storage of `bytes` bytes at `alignment`, charged to the checkpoint ledger and to an armed
|
|
468
|
+
// checkpoint stamp. The storage that an allocation function returns is an implicitly created array of
|
|
469
|
+
// std::byte, so a caller places objects in it at byte offsets from the returned pointer.
|
|
470
|
+
inline std::byte* allocateCheckpointBytes(std::size_t bytes, std::size_t alignment) {
|
|
471
|
+
if (checkpointAllocationStampArmed) checkpointAllocationStamp += bytes;
|
|
472
|
+
return static_cast<std::byte*>(InstanceMemoryLedger::instance().allocate(LedgerDomain::Checkpoint, bytes, alignment));
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
inline void deallocateCheckpointBytes(std::byte* pointer, std::size_t bytes, std::size_t alignment) noexcept {
|
|
476
|
+
InstanceMemoryLedger::instance().deallocate(LedgerDomain::Checkpoint, pointer, bytes, alignment);
|
|
477
|
+
}
|
|
478
|
+
|
|
467
479
|
template <class T>
|
|
468
480
|
using MetadataAllocator = DomainAllocator<T, LedgerDomain::Metadata>;
|
|
469
481
|
|
|
@@ -239,6 +239,40 @@ struct Box3dUninitByte {
|
|
|
239
239
|
};
|
|
240
240
|
static_assert(sizeof(Box3dUninitByte) == 1 && alignof(Box3dUninitByte) == 1);
|
|
241
241
|
|
|
242
|
+
// The header of a record allocation that keeps a strong leaf memo next to its values (a Box3D image block, a
|
|
243
|
+
// contact chunk). The values follow the header in the same allocation.
|
|
244
|
+
struct alignas(16) StrongMemoHeader {
|
|
245
|
+
StrongLeafMemo memo;
|
|
246
|
+
};
|
|
247
|
+
static_assert(sizeof(StrongMemoHeader) % alignof(StrongMemoHeader) == 0);
|
|
248
|
+
|
|
249
|
+
// Allocates one record: a StrongMemoHeader object, then `count` values copied from `source`, in one raw byte
|
|
250
|
+
// allocation of the checkpoint ledger. The returned pointer shares ownership of the allocation, and `memo`
|
|
251
|
+
// points to the header memo, which lives as long as the values. The memo receives a fresh serial.
|
|
252
|
+
template <typename T>
|
|
253
|
+
std::shared_ptr<const T[]> makeStrongMemoRecord(const T* source, std::size_t count, const StrongLeafMemo*& memo) {
|
|
254
|
+
static_assert(std::is_trivially_copyable_v<T>);
|
|
255
|
+
static_assert(alignof(T) <= alignof(StrongMemoHeader));
|
|
256
|
+
constexpr std::size_t alignment = alignof(StrongMemoHeader);
|
|
257
|
+
const std::size_t bytes = sizeof(StrongMemoHeader) + count * sizeof(T);
|
|
258
|
+
// Byte storage: the header object at offset 0, the values from sizeof(StrongMemoHeader), a multiple of
|
|
259
|
+
// alignof(T). Both offsets are pointer arithmetic on the byte array of the allocation.
|
|
260
|
+
std::byte* storage = geometry_private::allocateCheckpointBytes(bytes, alignment);
|
|
261
|
+
StrongMemoHeader* header = ::new (static_cast<void*>(storage)) StrongMemoHeader{};
|
|
262
|
+
header->memo.serial = nextStrongRecordSerial();
|
|
263
|
+
std::byte* valueStorage = storage + sizeof(StrongMemoHeader);
|
|
264
|
+
// T is trivially copyable: the copy creates the T objects in the byte storage, and launder points to them.
|
|
265
|
+
if (count > 0) std::memcpy(static_cast<void*>(valueStorage), source, count * sizeof(T));
|
|
266
|
+
const T* values = std::launder(reinterpret_cast<const T*>(valueStorage));
|
|
267
|
+
// When the control block allocation fails, the shared_ptr constructor calls the deleter, so no path leaks.
|
|
268
|
+
std::shared_ptr<StrongMemoHeader> owner(header, [storage, bytes](StrongMemoHeader* pointer) noexcept {
|
|
269
|
+
pointer->~StrongMemoHeader();
|
|
270
|
+
geometry_private::deallocateCheckpointBytes(storage, bytes, alignment);
|
|
271
|
+
}, geometry_private::CheckpointAllocator<StrongMemoHeader>{});
|
|
272
|
+
memo = &owner->memo;
|
|
273
|
+
return std::shared_ptr<const T[]>(std::move(owner), values);
|
|
274
|
+
}
|
|
275
|
+
|
|
242
276
|
std::uint32_t box3dLz4ReadU32(const std::uint8_t* bytes) noexcept {
|
|
243
277
|
std::uint32_t value = 0;
|
|
244
278
|
std::memcpy(&value, bytes, sizeof(value));
|
|
@@ -738,11 +772,11 @@ std::shared_ptr<const Box3dCanonicalImage> packBox3dCanonicalImage(
|
|
|
738
772
|
{
|
|
739
773
|
geometry_private::CheckpointAllocationStampScope chunkStamp;
|
|
740
774
|
noteImportCheckpointAllocation();
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
fresh.
|
|
775
|
+
// One allocation holds the strong leaf memo and then the stored bytes, so a shared block shares its
|
|
776
|
+
// leaf. Offsets are taken on the cell array, so every pointer stays inside the allocation.
|
|
777
|
+
const StrongLeafMemo* memo = nullptr;
|
|
778
|
+
fresh.bytes = makeStrongMemoRecord(storage.data(), storage.size(), memo);
|
|
779
|
+
fresh.memo = memo;
|
|
746
780
|
fresh.allocationBytes = static_cast<std::uint32_t>(chunkStamp.bytes());
|
|
747
781
|
}
|
|
748
782
|
fresh.count = static_cast<std::uint32_t>(storage.size());
|
|
@@ -809,6 +843,7 @@ constexpr char kDetailPlanBusy[] = "plan busy";
|
|
|
809
843
|
constexpr char kDetailNoPlan[] = "no plan";
|
|
810
844
|
constexpr char kDetailPlanClash[] = "plan clash";
|
|
811
845
|
constexpr char kDetailNoSerial[] = "no serial";
|
|
846
|
+
constexpr char kDetailDigestMemory[] = "no memory";
|
|
812
847
|
constexpr char kDetailGeometryCapacity[] = "geometry record capacity exceeded";
|
|
813
848
|
constexpr char kDetailGeometryAllocation[] = "geometry record resource limit exceeded";
|
|
814
849
|
constexpr std::uint64_t kUntrackedChunkHeaderBytes = 16;
|
|
@@ -826,6 +861,7 @@ static_assert(sizeof(kDetailPlanBusy) - 1 == 9);
|
|
|
826
861
|
static_assert(sizeof(kDetailNoPlan) - 1 == 7);
|
|
827
862
|
static_assert(sizeof(kDetailPlanClash) - 1 == 10);
|
|
828
863
|
static_assert(sizeof(kDetailNoSerial) - 1 == 9);
|
|
864
|
+
static_assert(sizeof(kDetailDigestMemory) - 1 <= 10);
|
|
829
865
|
static_assert(sizeof(kDetailPlanBusy) - 1 <= 10);
|
|
830
866
|
static_assert(sizeof(kDetailNoPlan) - 1 <= 10);
|
|
831
867
|
static_assert(sizeof(kDetailPlanClash) - 1 <= 10);
|
|
@@ -2717,13 +2753,30 @@ WorldError readBox3dCanonicalSection(
|
|
|
2717
2753
|
return error(WorldErrorCode::SnapshotMalformed, "canonical soft-step image digest does not match");
|
|
2718
2754
|
}
|
|
2719
2755
|
if (uncompressed) {
|
|
2756
|
+
// A donor transfer carries the staged image with the deferred fork digest. The same check as a full
|
|
2757
|
+
// snapshot keeps the stored bytes canonical: zero or the correct digest, then zero.
|
|
2758
|
+
const Box3dCanonicalBridge* bridge = g_box3dCanonicalBridge;
|
|
2759
|
+
if (bridge == nullptr || bridge->deferImageDigest == nullptr
|
|
2760
|
+
|| !bridge->deferImageDigest(std::span<std::uint8_t>(compressed.data(), compressed.size()))) {
|
|
2761
|
+
return error(WorldErrorCode::InvalidState, "the backend rejected the canonical state of the image");
|
|
2762
|
+
}
|
|
2720
2763
|
image = packBox3dCanonicalImage(compressed, nullptr, /*trustedInMemory=*/false, /*hasDeferredForkDigest=*/true);
|
|
2721
2764
|
} else {
|
|
2722
2765
|
Box3dCanonicalBytes payload(rawLength);
|
|
2723
2766
|
if (!decodeLzma(compressed, properties, payload)) {
|
|
2724
2767
|
return error(WorldErrorCode::SnapshotMalformed, "canonical soft-step image compression is invalid");
|
|
2725
2768
|
}
|
|
2726
|
-
image
|
|
2769
|
+
// A stored image holds the deferred fork digest, as a live capture does, so equal states store equal
|
|
2770
|
+
// bytes and equal blocks (the strong digest hashes the stored blocks). A restore materializes it again.
|
|
2771
|
+
// deferImageDigest reads the state section with the parser that the restore uses to materialize the
|
|
2772
|
+
// digest, and it checks a supplied fork digest as a verifying restore does, so an image that it refuses
|
|
2773
|
+
// is an image that a verifying restore refuses: the import refuses it with the error of that restore.
|
|
2774
|
+
const Box3dCanonicalBridge* bridge = g_box3dCanonicalBridge;
|
|
2775
|
+
if (bridge == nullptr || bridge->deferImageDigest == nullptr
|
|
2776
|
+
|| !bridge->deferImageDigest(std::span<std::uint8_t>(payload.data(), payload.size()))) {
|
|
2777
|
+
return error(WorldErrorCode::InvalidState, "the backend rejected the canonical state of the image");
|
|
2778
|
+
}
|
|
2779
|
+
image = packBox3dCanonicalImage(payload, nullptr, /*trustedInMemory=*/false, /*hasDeferredForkDigest=*/true);
|
|
2727
2780
|
}
|
|
2728
2781
|
} catch (const geometry_private::AllocationCapacity&) {
|
|
2729
2782
|
return error(WorldErrorCode::AllocationFailure, "canonical soft-step image exceeds the retained memory ceiling");
|
|
@@ -3016,6 +3069,15 @@ Box3dCanonicalBytes decodeBox3dCanonicalPrefix(const Box3dCanonicalImage& image,
|
|
|
3016
3069
|
return raw;
|
|
3017
3070
|
}
|
|
3018
3071
|
|
|
3072
|
+
bool decodeBox3dCanonicalBlock(const Box3dCanonicalChunk& chunk, std::span<std::uint8_t> out) noexcept {
|
|
3073
|
+
if (chunk.count == 0 || chunk.bytes == nullptr || out.size() != chunk.rawCount) return false;
|
|
3074
|
+
const std::span<const std::uint8_t> stored(chunk.bytes.get(), chunk.count);
|
|
3075
|
+
if (chunk.isCompressed()) return decodeBox3dLz4Block(stored, out);
|
|
3076
|
+
if (chunk.rawCount != chunk.count) return false;
|
|
3077
|
+
std::memcpy(out.data(), stored.data(), chunk.count);
|
|
3078
|
+
return true;
|
|
3079
|
+
}
|
|
3080
|
+
|
|
3019
3081
|
Box3dCanonicalBytes decodeBox3dCanonicalImage(const Box3dCanonicalImage& image,
|
|
3020
3082
|
bool materializeForkDigest) {
|
|
3021
3083
|
// Each block decodes on its own and is appended in share order, so the output is the exact raw
|
|
@@ -3791,8 +3853,38 @@ WorldError World::strongDigestCheckpoint(
|
|
|
3791
3853
|
return error(WorldErrorCode::InvalidState, "checkpoint profile id is invalid");
|
|
3792
3854
|
}
|
|
3793
3855
|
std::array<std::uint8_t, 32> local{};
|
|
3794
|
-
|
|
3795
|
-
|
|
3856
|
+
// The cache only skips work: each entry is checked exactly, so a digest that cannot take the lock (another
|
|
3857
|
+
// digest of this world is running) computes without it and gives the same bytes.
|
|
3858
|
+
const bool ownsCache = !strongDigestCacheBusy_.test_and_set(std::memory_order_acquire);
|
|
3859
|
+
struct ReleaseCache {
|
|
3860
|
+
std::atomic_flag* busy;
|
|
3861
|
+
~ReleaseCache() {
|
|
3862
|
+
if (busy != nullptr) busy->clear(std::memory_order_release);
|
|
3863
|
+
}
|
|
3864
|
+
} release{ownsCache ? &strongDigestCacheBusy_ : nullptr};
|
|
3865
|
+
StrongDigestCache* cache = ownsCache ? &strongDigestCache_ : nullptr;
|
|
3866
|
+
if (cache != nullptr) ++cache->epoch;
|
|
3867
|
+
// On every exit (a digest, an error, a refused allocation) of a call that visited groups, the cache keeps only
|
|
3868
|
+
// the groups of this call, so failed calls never accumulate entries. A call that read the checkpoint memo
|
|
3869
|
+
// visits no group and leaves the cache as it was. Erasing allocates nothing.
|
|
3870
|
+
struct PruneCache {
|
|
3871
|
+
StrongDigestCache* cache;
|
|
3872
|
+
~PruneCache() {
|
|
3873
|
+
if (cache == nullptr || cache->visitedEpoch != cache->epoch) return;
|
|
3874
|
+
std::erase_if(cache->groups, [this](const auto& entry) { return entry.second.epoch != cache->epoch; });
|
|
3875
|
+
}
|
|
3876
|
+
} prune{cache};
|
|
3877
|
+
WorldError status{};
|
|
3878
|
+
try {
|
|
3879
|
+
status = strongDigestView(
|
|
3880
|
+
CheckpointStateView{*data, table, checkpoint.box3dCanonical().get()}, limits_, local, data.get(), cache);
|
|
3881
|
+
} catch (const std::bad_alloc&) {
|
|
3882
|
+
// The leaf lists and the block scratch of the digest are required work: without them there is no digest.
|
|
3883
|
+
// The detail fits the short string buffer, so the refusal allocates nothing.
|
|
3884
|
+
return error(WorldErrorCode::AllocationFailure, kDetailDigestMemory);
|
|
3885
|
+
} catch (const geometry_private::AllocationCapacity&) {
|
|
3886
|
+
return error(WorldErrorCode::AllocationFailure, kDetailDigestMemory);
|
|
3887
|
+
}
|
|
3796
3888
|
if (!status.ok()) return status;
|
|
3797
3889
|
output = local;
|
|
3798
3890
|
return {};
|
|
@@ -4431,16 +4523,6 @@ bool sameChunkValues(const WorldContactEvent* left, const WorldContactEvent* rig
|
|
|
4431
4523
|
}
|
|
4432
4524
|
|
|
4433
4525
|
|
|
4434
|
-
// One 16-byte cell of a chunk allocation: the leaf memo, then the values.
|
|
4435
|
-
struct alignas(16) CheckpointChunkCell {
|
|
4436
|
-
std::byte bytes[16];
|
|
4437
|
-
};
|
|
4438
|
-
// The memo cells at the start of a chunk allocation. The values start on a cell boundary.
|
|
4439
|
-
inline constexpr std::size_t kCheckpointChunkMemoBytes = 48;
|
|
4440
|
-
static_assert(sizeof(StrongLeafMemo) <= kCheckpointChunkMemoBytes);
|
|
4441
|
-
static_assert(alignof(StrongLeafMemo) <= alignof(CheckpointChunkCell));
|
|
4442
|
-
static_assert(kCheckpointChunkMemoBytes % sizeof(CheckpointChunkCell) == 0);
|
|
4443
|
-
static_assert(std::is_trivially_destructible_v<StrongLeafMemo>);
|
|
4444
4526
|
|
|
4445
4527
|
// Splits values into chunks. The rows themselves select the chunk ends, so an
|
|
4446
4528
|
// added or removed row changes only the chunk that holds it. A chunk whose
|
|
@@ -4455,7 +4537,6 @@ std::shared_ptr<const CheckpointChunkTable<T>> packCheckpointChunks(
|
|
|
4455
4537
|
const CheckpointChunkTable<T>* prev) {
|
|
4456
4538
|
static_assert(std::is_trivially_copyable_v<T>);
|
|
4457
4539
|
if (values.empty()) return nullptr;
|
|
4458
|
-
static_assert(alignof(T) <= alignof(CheckpointChunkCell));
|
|
4459
4540
|
const std::size_t count = values.size();
|
|
4460
4541
|
std::size_t chunkCount = 0;
|
|
4461
4542
|
forEachCheckpointChunkRange(values, shape, keyOf, [&](std::size_t, std::size_t) { ++chunkCount; });
|
|
@@ -4513,18 +4594,8 @@ std::shared_ptr<const CheckpointChunkTable<T>> packCheckpointChunks(
|
|
|
4513
4594
|
geometry_private::CheckpointAllocationStampScope stamp;
|
|
4514
4595
|
noteImportCheckpointAllocation();
|
|
4515
4596
|
// One allocation holds the leaf memo and then the values, so a shared chunk shares its leaf.
|
|
4516
|
-
|
|
4517
|
-
|
|
4518
|
-
const std::size_t valueCells = (length * sizeof(T) + sizeof(CheckpointChunkCell) - 1)
|
|
4519
|
-
/ sizeof(CheckpointChunkCell);
|
|
4520
|
-
auto owned = std::allocate_shared<CheckpointChunkCell[]>(
|
|
4521
|
-
geometry_private::CheckpointAllocator<CheckpointChunkCell>{}, memoCells + valueCells);
|
|
4522
|
-
const StrongLeafMemo* memo = ::new (static_cast<void*>(owned.get())) StrongLeafMemo{};
|
|
4523
|
-
void* valueStorage = static_cast<void*>(owned.get() + memoCells);
|
|
4524
|
-
// T is trivially copyable: the copy creates the T objects, and launder gives a pointer to them.
|
|
4525
|
-
std::memcpy(valueStorage, data, length * sizeof(T));
|
|
4526
|
-
const T* payload = std::launder(reinterpret_cast<const T*>(valueStorage));
|
|
4527
|
-
fresh.values = std::shared_ptr<const T[]>(std::move(owned), payload);
|
|
4597
|
+
const StrongLeafMemo* memo = nullptr;
|
|
4598
|
+
fresh.values = makeStrongMemoRecord(data, length, memo);
|
|
4528
4599
|
fresh.memo = memo;
|
|
4529
4600
|
fresh.allocationBytes = stamp.bytes();
|
|
4530
4601
|
}
|
|
@@ -4620,6 +4691,7 @@ WorldError World::packCheckpointData(
|
|
|
4620
4691
|
fresh->logicalCount = static_cast<std::uint32_t>(length);
|
|
4621
4692
|
fresh->physicalCount = tileValues;
|
|
4622
4693
|
fresh->values = std::move(values);
|
|
4694
|
+
fresh->serial = nextStrongRecordSerial();
|
|
4623
4695
|
builtTable->tiles.push_back(std::shared_ptr<const BodyTile>(std::move(fresh)));
|
|
4624
4696
|
builtTable->tileAllocationBytes.push_back(tileBytes);
|
|
4625
4697
|
}
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
#include <array>
|
|
12
12
|
#include <cstring>
|
|
13
13
|
#include <deque>
|
|
14
|
+
#include <unordered_map>
|
|
14
15
|
#include <atomic>
|
|
15
16
|
#include <cstddef>
|
|
16
17
|
#include <cstdint>
|
|
@@ -763,6 +764,8 @@ struct BodyTile {
|
|
|
763
764
|
mutable std::uint32_t memoLength = 0;
|
|
764
765
|
mutable std::array<std::uint8_t, 32> memo{};
|
|
765
766
|
std::shared_ptr<const double[]> values;
|
|
767
|
+
// The process-unique serial of this tile (nextStrongRecordSerial); 0 for a tile with no serial.
|
|
768
|
+
std::uint64_t serial = 0;
|
|
766
769
|
};
|
|
767
770
|
|
|
768
771
|
inline const double* bodyTileDoubles(const BodyTile& tile) noexcept {
|
|
@@ -798,6 +801,41 @@ inline constexpr std::size_t kCheckpointSharedFromSlabOffset = ~std::size_t{0};
|
|
|
798
801
|
struct StrongLeafMemo {
|
|
799
802
|
mutable std::atomic<std::uint8_t> state{0};
|
|
800
803
|
mutable std::array<std::uint8_t, 32> leaf{};
|
|
804
|
+
// The process-unique serial of the record that holds the memo (nextStrongRecordSerial), or 0 for a record
|
|
805
|
+
// with no serial. A serial is never reused, so equal serials name the same immutable bytes.
|
|
806
|
+
std::uint64_t serial = 0;
|
|
807
|
+
};
|
|
808
|
+
|
|
809
|
+
inline std::atomic<std::uint64_t> gStrongRecordSerial{1};
|
|
810
|
+
inline std::uint64_t nextStrongRecordSerial() noexcept {
|
|
811
|
+
return gStrongRecordSerial.fetch_add(1, std::memory_order_relaxed);
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
// Strong digest work that one digest leaves for the next: the group nodes of chunked sections, keyed by
|
|
815
|
+
// the serial of the first record of the group, and the leaf of small raw sections with a copy of their
|
|
816
|
+
// bytes. Every entry is checked exactly before use (all serials equal, or all bytes equal), so a digest
|
|
817
|
+
// with the cache equals a digest without it. After a digest that visited groups, failed or not, the cache keeps
|
|
818
|
+
// only the groups that this digest used; a digest that read the checkpoint memo visits none and keeps them all.
|
|
819
|
+
// Its memory is charged to the metadata ledger of the instance.
|
|
820
|
+
struct StrongDigestCache {
|
|
821
|
+
template <typename T>
|
|
822
|
+
using Allocator = geometry_private::MetadataAllocator<T>;
|
|
823
|
+
struct Group {
|
|
824
|
+
std::vector<std::uint64_t, Allocator<std::uint64_t>> serials;
|
|
825
|
+
std::array<std::uint8_t, 32> node{};
|
|
826
|
+
std::uint64_t epoch = 0;
|
|
827
|
+
};
|
|
828
|
+
struct Raw {
|
|
829
|
+
bool valid = false;
|
|
830
|
+
std::vector<std::uint8_t, Allocator<std::uint8_t>> bytes;
|
|
831
|
+
std::array<std::uint8_t, 32> leaf{};
|
|
832
|
+
};
|
|
833
|
+
std::unordered_map<std::uint64_t, Group, std::hash<std::uint64_t>, std::equal_to<std::uint64_t>,
|
|
834
|
+
Allocator<std::pair<const std::uint64_t, Group>>> groups;
|
|
835
|
+
std::array<Raw, 32> raw{};
|
|
836
|
+
std::uint64_t epoch = 0;
|
|
837
|
+
// The epoch of the last digest that visited groups (writeStrongLeafGroups).
|
|
838
|
+
std::uint64_t visitedEpoch = 0;
|
|
801
839
|
};
|
|
802
840
|
|
|
803
841
|
struct CheckpointStaticSlab {
|
|
@@ -930,6 +968,8 @@ struct CheckpointChunkShape {
|
|
|
930
968
|
std::uint64_t endMask = 0;
|
|
931
969
|
// A chunk ends at this number of rows.
|
|
932
970
|
std::size_t maxRows = 1;
|
|
971
|
+
// True: a chunk ends only where the next row has another key, so the rows of one key stay together.
|
|
972
|
+
bool keyRuns = false;
|
|
933
973
|
};
|
|
934
974
|
|
|
935
975
|
// Calls visit(start, length) once per chunk of values, in order. The chunk ends are a function of the
|
|
@@ -944,9 +984,14 @@ void forEachCheckpointChunkRange(
|
|
|
944
984
|
std::size_t chunkStart = 0;
|
|
945
985
|
for (std::size_t row = 0, rowsInChunk = 1; row < rows; ++row, ++rowsInChunk) {
|
|
946
986
|
const std::size_t offset = row * shape.rowValues;
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
987
|
+
bool ends = row + 1 == rows || rowsInChunk == shape.maxRows;
|
|
988
|
+
if (!ends) {
|
|
989
|
+
const CheckpointChunkKey key = keyOf(values.data() + offset, std::min(shape.rowValues, count - offset));
|
|
990
|
+
const std::size_t next = offset + shape.rowValues;
|
|
991
|
+
const bool keyChanges = !shape.keyRuns
|
|
992
|
+
|| !(keyOf(values.data() + next, std::min(shape.rowValues, count - next)) == key);
|
|
993
|
+
ends = keyChanges && (checkpointChunkKeyHash(key) & shape.endMask) == shape.endMask;
|
|
994
|
+
}
|
|
950
995
|
if (!ends) continue;
|
|
951
996
|
rowsInChunk = 0;
|
|
952
997
|
const std::size_t chunkEnd = std::min(count, (row + 1) * shape.rowValues);
|
|
@@ -955,10 +1000,12 @@ void forEachCheckpointChunkRange(
|
|
|
955
1000
|
}
|
|
956
1001
|
}
|
|
957
1002
|
|
|
958
|
-
// The 8 events (3 KB)
|
|
959
|
-
//
|
|
1003
|
+
// The 8 events (3 KB) of a typical event chunk keep the copy for a changed row small and the chunk list
|
|
1004
|
+
// short. A contact cache chunk ends at 8 rows (2.4 KB), or earlier between two body pairs (on about 1 in 8 pair
|
|
1005
|
+
// keys), so a contact that changes copies and hashes at most 8 rows. The 8-row end can fall inside the rows of
|
|
1006
|
+
// one pair; the ends stay a function of the rows.
|
|
960
1007
|
inline constexpr CheckpointChunkShape kContactEventChunkShape{1, 7, 32};
|
|
961
|
-
inline constexpr CheckpointChunkShape kContactCacheChunkShape{kContactCacheStride,
|
|
1008
|
+
inline constexpr CheckpointChunkShape kContactCacheChunkShape{kContactCacheStride, 7, 8, true};
|
|
962
1009
|
|
|
963
1010
|
// A contact event row is one event. Its key is the body pair.
|
|
964
1011
|
inline CheckpointChunkKey contactEventChunkKey(const WorldContactEvent* row, std::size_t) noexcept {
|
|
@@ -1102,6 +1149,9 @@ struct Box3dCanonicalChunk {
|
|
|
1102
1149
|
std::uint32_t flags = 0;
|
|
1103
1150
|
// 128-bit 2-lane digest of the raw bytes for zero-decompression block sharing.
|
|
1104
1151
|
std::array<std::uint64_t, 2> rawDigest{};
|
|
1152
|
+
// The strong digest leaf of the raw bytes, in the allocation that bytes owns, so every image that shares
|
|
1153
|
+
// the block shares the leaf. Null for a block made without a memo.
|
|
1154
|
+
const StrongLeafMemo* memo = nullptr;
|
|
1105
1155
|
|
|
1106
1156
|
bool isCompressed() const noexcept { return (flags & 1u) != 0u; }
|
|
1107
1157
|
};
|
|
@@ -1125,6 +1175,9 @@ struct Box3dCanonicalImage {
|
|
|
1125
1175
|
// Return the raw canonical image for restore, export, and strong-digest hashing.
|
|
1126
1176
|
Box3dCanonicalBytes decodeBox3dCanonicalImage(const Box3dCanonicalImage& image,
|
|
1127
1177
|
bool materializeForkDigest = true);
|
|
1178
|
+
// Decode one stored block into `out`, which must hold exactly chunk.rawCount bytes. False for an empty or
|
|
1179
|
+
// undecodable block.
|
|
1180
|
+
bool decodeBox3dCanonicalBlock(const Box3dCanonicalChunk& chunk, std::span<std::uint8_t> out) noexcept;
|
|
1128
1181
|
// Return the first `size` raw bytes of the canonical image (fewer when the image is shorter). It
|
|
1129
1182
|
// decodes only the blocks that hold them. Empty on a block that does not decode.
|
|
1130
1183
|
Box3dCanonicalBytes decodeBox3dCanonicalPrefix(const Box3dCanonicalImage& image, std::size_t size);
|
|
@@ -1267,6 +1320,9 @@ struct Box3dCanonicalBridge {
|
|
|
1267
1320
|
// that no native body takes, and the next sync would publish it for a group that is not held.
|
|
1268
1321
|
bool (*effectiveAngularVelocity)(World& world, BodyId id, const std::array<double, 3>& publicAngular,
|
|
1269
1322
|
std::array<double, 3>& out) noexcept = nullptr;
|
|
1323
|
+
// The inverse of materializeImageDigest: writes zero to the fork digest of a canonical image, so a stored
|
|
1324
|
+
// image always holds the deferred form (hasDeferredForkDigest) and equal images store equal bytes.
|
|
1325
|
+
bool (*deferImageDigest)(std::span<std::uint8_t> bytes) noexcept = nullptr;
|
|
1270
1326
|
};
|
|
1271
1327
|
|
|
1272
1328
|
// The Box3D backend state of one public world: its mirror, the adapter world id that it owns, its
|
|
@@ -2358,6 +2414,12 @@ class World {
|
|
|
2358
2414
|
CheckpointBodyTableOwner lastCaptureBodies_;
|
|
2359
2415
|
CheckpointStaticSlabOwner lastCaptureStaticSlab_;
|
|
2360
2416
|
CheckpointChunkTableOwner<double> lastCaptureContactCache_;
|
|
2417
|
+
// Strong digest work kept for the next digest (StrongDigestCache). It is not part of the world state: a
|
|
2418
|
+
// copy or a move starts with an empty cache, and every entry is checked exactly before use.
|
|
2419
|
+
mutable StrongDigestCache strongDigestCache_;
|
|
2420
|
+
// Held by the strong digest that uses the cache. test_and_set and clear are noexcept, so a digest that is
|
|
2421
|
+
// reached from a noexcept entry point takes and returns it without a throwing path.
|
|
2422
|
+
mutable std::atomic_flag strongDigestCacheBusy_ = ATOMIC_FLAG_INIT;
|
|
2361
2423
|
CheckpointChunkTableOwner<WorldContactEvent> lastCaptureContactEvents_;
|
|
2362
2424
|
std::shared_ptr<const Box3dCanonicalImage> lastCaptureBox3dImage_;
|
|
2363
2425
|
CaptureStateStamp lastCaptureStamp_{};
|