@series-inc/rundot-syncplay 6.0.0-rc.110 → 6.0.0-rc.112

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.
@@ -14,12 +14,21 @@ namespace rundot::kinetix::physics {
14
14
  // Revision 5 (T15): the canonical Box3D image is a strong field after every public field, so a checkpoint
15
15
  // that carries solver state commits to it. Revision 4 is rejected: a strong digest of revision 4 does not
16
16
  // cover the image and cannot be compared with a digest of revision 5.
17
- inline constexpr std::uint32_t kWorldStrongDigestAlgorithmRevision = 5;
17
+ // Revision 6: the contact cache section is a list of chunk leaves (kStrongContactCacheChunkDomain), not the
18
+ // raw values. The chunk ends are a function of the rows (forEachCheckpointChunkRange), and a checkpoint keeps
19
+ // the leaf of a chunk that it shares, so a strong digest hashes only the cache rows that changed.
20
+ // The topology ids, definition signatures and body materials sections are each one leaf
21
+ // (kStrongSectionLeafDomain, then the section bytes of revision 5). The static slab keeps those leaves.
22
+ // The contact event section is a list of chunk leaves too (kStrongContactEventChunkDomain).
23
+ inline constexpr std::uint32_t kWorldStrongDigestAlgorithmRevision = 6;
18
24
  inline constexpr char kStrongCheckpointDomain[] = "kinetix.world-checkpoint/1";
19
25
  inline constexpr char kStrongRecordDomain[] = "kinetix.geometry-record/1";
20
26
  inline constexpr char kStrongRootDomain[] = "kinetix.geometry-root/1";
21
27
  inline constexpr char kStrongMaterialDomain[] = "kinetix.material-root/1";
22
28
  inline constexpr char kStrongTileDomain[] = "kinetix.body-tile/1";
29
+ inline constexpr char kStrongContactCacheChunkDomain[] = "kinetix.contact-cache-chunk/1";
30
+ inline constexpr char kStrongSectionLeafDomain[] = "kinetix.strong-section/1";
31
+ inline constexpr char kStrongContactEventChunkDomain[] = "kinetix.contact-event-chunk/1";
23
32
 
24
33
  // The canonical Box3D section of a portable snapshot (wire versions 30 to 35). One fixed-width header and
25
34
  // then the payload, so a corrupt length field fails before any read of the payload:
@@ -113,6 +122,9 @@ static_assert(sizeof(kStrongRecordDomain) - 1 == 25);
113
122
  static_assert(sizeof(kStrongRootDomain) - 1 == 23);
114
123
  static_assert(sizeof(kStrongMaterialDomain) - 1 == 23);
115
124
  static_assert(sizeof(kStrongTileDomain) - 1 == 19);
125
+ static_assert(sizeof(kStrongContactCacheChunkDomain) - 1 == 29);
126
+ static_assert(sizeof(kStrongSectionLeafDomain) - 1 == 24);
127
+ static_assert(sizeof(kStrongContactEventChunkDomain) - 1 == 29);
116
128
 
117
129
  // One field list drives the FNV visitor and the strong visitor.
118
130
  #define RUNDOT_CHECKPOINT_FIELDS(X) \
@@ -354,7 +366,8 @@ inline void checkpointRecordSchemaGuard(const CheckpointDeformableTable& value)
354
366
  }
355
367
  template <typename T>
356
368
  inline void checkpointRecordSchemaGuard(const CheckpointChunk<T>& value) noexcept {
357
- [[maybe_unused]] const auto& [values, count, allocationBytes] = value;
369
+ // memo is the strong digest leaf of values, derived and never serialized.
370
+ [[maybe_unused]] const auto& [values, count, allocationBytes, memo] = value;
358
371
  }
359
372
  template <typename T>
360
373
  inline void checkpointRecordSchemaGuard(const CheckpointChunkTable<T>& value) noexcept {
@@ -395,7 +408,11 @@ inline void checkpointRecordSchemaGuard(const CheckpointStaticSlab& value) noexc
395
408
  surfaceVelocitiesOffset,
396
409
  surfaceVelocitiesCount,
397
410
  bodyMaterialFramesOffset,
398
- bodyMaterialFramesCount
411
+ bodyMaterialFramesCount,
412
+ // Strong digest leaves of slab sections: derived from the slab bytes, never serialized.
413
+ topologyIdsLeaf,
414
+ definitionSignaturesLeaf,
415
+ bodyMaterialsLeaf
399
416
  ] = value;
400
417
  }
401
418
  inline void checkpointRecordSchemaGuard(const BodyId& value) noexcept {
@@ -3588,6 +3588,8 @@ struct LiveStateView {
3588
3588
  // A live world has no checkpoint image. A live strong digest therefore commits to the public state only,
3589
3589
  // with the image field empty.
3590
3590
  const Box3dCanonicalImage* strongBox3dCanonical() const noexcept { return nullptr; }
3591
+ // Live state keeps no leaf memo: the strong digest computes each section leaf.
3592
+ const StrongLeafMemo* strongSectionLeafMemo(CheckpointField) const noexcept { return nullptr; }
3591
3593
  // The live canonical images of the registered deformable worlds. Empty when there is no registration.
3592
3594
  std::span<const CheckpointDeformableImage> strongDeformableImages() const noexcept { return deformables; }
3593
3595
  std::span<const CheckpointDeformableImage> deformables{};
@@ -3646,6 +3648,17 @@ struct CheckpointStateView {
3646
3648
  std::span<const BodyContactMaterialAssignment> bodyMaterials() const noexcept { return data.bodyMaterials(); }
3647
3649
  std::span<const BodyMaterialFrameRow> bodyMaterialFrames() const noexcept { return data.bodyMaterialFrames(); }
3648
3650
  const Box3dCanonicalImage* strongBox3dCanonical() const noexcept { return box3d; }
3651
+ // The leaf memo of a section that the static slab holds, so checkpoints that share the slab share it.
3652
+ const StrongLeafMemo* strongSectionLeafMemo(CheckpointField field) const noexcept {
3653
+ const CheckpointStaticSlab* slab = data.staticSlab.get();
3654
+ if (slab == nullptr) return nullptr;
3655
+ switch (field) {
3656
+ case CheckpointField::TopologyIds: return &slab->topologyIdsLeaf;
3657
+ case CheckpointField::DefinitionSignatures: return &slab->definitionSignaturesLeaf;
3658
+ case CheckpointField::BodyMaterials: return &slab->bodyMaterialsLeaf;
3659
+ default: return nullptr;
3660
+ }
3661
+ }
3649
3662
  std::span<const CheckpointDeformableImage> strongDeformableImages() const noexcept {
3650
3663
  return data.deformableImages();
3651
3664
  }
@@ -4344,37 +4357,6 @@ WorldError World::publishCheckpoint(PreparedCheckpoint&& prepared, WorldCheckpoi
4344
4357
  }
4345
4358
 
4346
4359
  namespace {
4347
- // The key of the first row of a chunk. Chunk ends and chunk matches use it.
4348
- struct CheckpointChunkKey {
4349
- std::uint64_t first = 0;
4350
- std::uint64_t second = 0;
4351
- bool operator==(const CheckpointChunkKey&) const = default;
4352
- };
4353
-
4354
- std::uint64_t checkpointChunkKeyHash(const CheckpointChunkKey& key) noexcept {
4355
- std::uint64_t value = key.first * 0x9E3779B97F4A7C15ull;
4356
- value ^= key.second + 0x632BE59BD9B4E019ull + (value << 6) + (value >> 2);
4357
- value ^= value >> 30;
4358
- value *= 0xBF58476D1CE4E5B9ull;
4359
- value ^= value >> 27;
4360
- value *= 0x94D049BB133111EBull;
4361
- value ^= value >> 31;
4362
- return value;
4363
- }
4364
-
4365
- // A contact event row is one event. Its key is the body pair.
4366
- CheckpointChunkKey contactEventChunkKey(const WorldContactEvent* row, std::size_t) noexcept {
4367
- return {row->bodyA.value, row->bodyB.value};
4368
- }
4369
-
4370
- // A contact cache row is kContactCacheStride values. Its key is the bits of
4371
- // the two body indices. A short last row uses the values that it has.
4372
- CheckpointChunkKey contactCacheChunkKey(const double* row, std::size_t available) noexcept {
4373
- CheckpointChunkKey key;
4374
- if (available > 0) std::memcpy(&key.first, row, sizeof(double));
4375
- if (available > 1) std::memcpy(&key.second, row + 1, sizeof(double));
4376
- return key;
4377
- }
4378
4360
 
4379
4361
  // Equal bits in every field. Padding bytes are not compared: no read of a
4380
4362
  // contact event uses them, and they can differ between equal events.
@@ -4448,19 +4430,17 @@ bool sameChunkValues(const WorldContactEvent* left, const WorldContactEvent* rig
4448
4430
  return true;
4449
4431
  }
4450
4432
 
4451
- struct CheckpointChunkShape {
4452
- // Values per row. A chunk ends only at the end of a row.
4453
- std::size_t rowValues = 1;
4454
- // A row ends its chunk when its key hash has all of these bits.
4455
- std::uint64_t endMask = 0;
4456
- // A chunk ends at this number of rows.
4457
- std::size_t maxRows = 1;
4458
- };
4459
4433
 
4460
- // The 8 events (3 KB) or 16 cache rows (1.2 KB) of a typical chunk keep the
4461
- // copy for a changed row small and the chunk list short.
4462
- constexpr CheckpointChunkShape kContactEventChunkShape{1, 7, 32};
4463
- constexpr CheckpointChunkShape kContactCacheChunkShape{kContactCacheStride, 15, 64};
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>);
4464
4444
 
4465
4445
  // Splits values into chunks. The rows themselves select the chunk ends, so an
4466
4446
  // added or removed row changes only the chunk that holds it. A chunk whose
@@ -4475,24 +4455,10 @@ std::shared_ptr<const CheckpointChunkTable<T>> packCheckpointChunks(
4475
4455
  const CheckpointChunkTable<T>* prev) {
4476
4456
  static_assert(std::is_trivially_copyable_v<T>);
4477
4457
  if (values.empty()) return nullptr;
4458
+ static_assert(alignof(T) <= alignof(CheckpointChunkCell));
4478
4459
  const std::size_t count = values.size();
4479
- const std::size_t rows = (count + shape.rowValues - 1) / shape.rowValues;
4480
- auto rowKey = [&](std::size_t row) {
4481
- const std::size_t offset = row * shape.rowValues;
4482
- return keyOf(values.data() + offset, std::min(shape.rowValues, count - offset));
4483
- };
4484
- auto endsChunk = [&](std::size_t row, std::size_t rowsInChunk) {
4485
- return row + 1 == rows || rowsInChunk == shape.maxRows
4486
- || (checkpointChunkKeyHash(rowKey(row)) & shape.endMask) == shape.endMask;
4487
- };
4488
-
4489
4460
  std::size_t chunkCount = 0;
4490
- for (std::size_t row = 0, rowsInChunk = 1; row < rows; ++row, ++rowsInChunk) {
4491
- if (endsChunk(row, rowsInChunk)) {
4492
- ++chunkCount;
4493
- rowsInChunk = 0;
4494
- }
4495
- }
4461
+ forEachCheckpointChunkRange(values, shape, keyOf, [&](std::size_t, std::size_t) { ++chunkCount; });
4496
4462
 
4497
4463
  // The chunks of prev by the key of their first row. Open addressing.
4498
4464
  std::vector<std::uint32_t> prevIndex;
@@ -4537,12 +4503,7 @@ std::shared_ptr<const CheckpointChunkTable<T>> packCheckpointChunks(
4537
4503
  }
4538
4504
  table->valueCount = count;
4539
4505
  std::uint64_t totalBytes = table->selfAllocationBytes;
4540
- std::size_t chunkStart = 0;
4541
- for (std::size_t row = 0, rowsInChunk = 1; row < rows; ++row, ++rowsInChunk) {
4542
- if (!endsChunk(row, rowsInChunk)) continue;
4543
- rowsInChunk = 0;
4544
- const std::size_t chunkEnd = std::min(count, (row + 1) * shape.rowValues);
4545
- const std::size_t length = chunkEnd - chunkStart;
4506
+ forEachCheckpointChunkRange(values, shape, keyOf, [&](std::size_t chunkStart, std::size_t length) {
4546
4507
  const T* data = values.data() + chunkStart;
4547
4508
  if (const CheckpointChunk<T>* shared = findShared(data, length)) {
4548
4509
  table->chunks.push_back(*shared);
@@ -4551,17 +4512,27 @@ std::shared_ptr<const CheckpointChunkTable<T>> packCheckpointChunks(
4551
4512
  {
4552
4513
  geometry_private::CheckpointAllocationStampScope stamp;
4553
4514
  noteImportCheckpointAllocation();
4554
- auto owned = std::allocate_shared<T[]>(geometry_private::CheckpointAllocator<T>{}, length);
4555
- std::memcpy(owned.get(), data, length * sizeof(T));
4556
- fresh.values = std::move(owned);
4515
+ // One allocation holds the leaf memo and then the values, so a shared chunk shares its leaf.
4516
+ // Offsets are taken on the cell array, so every pointer stays inside the allocation.
4517
+ constexpr std::size_t memoCells = kCheckpointChunkMemoBytes / sizeof(CheckpointChunkCell);
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);
4528
+ fresh.memo = memo;
4557
4529
  fresh.allocationBytes = stamp.bytes();
4558
4530
  }
4559
4531
  fresh.count = static_cast<std::uint32_t>(length);
4560
4532
  table->chunks.push_back(std::move(fresh));
4561
4533
  }
4562
4534
  totalBytes += table->chunks.back().allocationBytes;
4563
- chunkStart = chunkEnd;
4564
- }
4535
+ });
4565
4536
  table->totalAllocatedBytes = totalBytes;
4566
4537
  return table;
4567
4538
  }
@@ -7,7 +7,9 @@
7
7
  #include "contact_material_store.hpp"
8
8
  #include "deformable_world_bridge.hpp"
9
9
 
10
+ #include <algorithm>
10
11
  #include <array>
12
+ #include <cstring>
11
13
  #include <deque>
12
14
  #include <atomic>
13
15
  #include <cstddef>
@@ -789,6 +791,15 @@ struct CheckpointProfile {
789
791
 
790
792
  inline constexpr std::size_t kCheckpointSharedFromSlabOffset = ~std::size_t{0};
791
793
 
794
+ // The strong digest leaf of an immutable record that checkpoints share (a contact chunk, a static slab
795
+ // section). The first strong digest fills it, and every later digest of a checkpoint that shares the
796
+ // record reads it. Derived from the record bytes, never serialized. The state follows
797
+ // rundot::sha256::tryPublishMemo.
798
+ struct StrongLeafMemo {
799
+ mutable std::atomic<std::uint8_t> state{0};
800
+ mutable std::array<std::uint8_t, 32> leaf{};
801
+ };
802
+
792
803
  struct CheckpointStaticSlab {
793
804
  std::size_t totalAllocatedBytes = 0;
794
805
  std::uint64_t stampedAllocatedBytes = 0;
@@ -829,6 +840,11 @@ struct CheckpointStaticSlab {
829
840
  std::size_t bodyMaterialFramesOffset = 0;
830
841
  std::size_t bodyMaterialFramesCount = 0;
831
842
 
843
+ // Strong digest leaves of the slab sections that the strong digest hashes as one leaf each.
844
+ StrongLeafMemo topologyIdsLeaf;
845
+ StrongLeafMemo definitionSignaturesLeaf;
846
+ StrongLeafMemo bodyMaterialsLeaf;
847
+
832
848
  const std::byte* dataBytes() const noexcept {
833
849
  return reinterpret_cast<const std::byte*>(this);
834
850
  }
@@ -879,13 +895,85 @@ using CheckpointStaticSlabOwner = std::shared_ptr<const CheckpointStaticSlab>;
879
895
  // A run of consecutive values that checkpoints share while its bytes stay
880
896
  // equal, as they share body tiles. A chunk always holds one or more values.
881
897
  // allocationBytes is the ledger charge of the chunk when it was made.
898
+ // memo is the strong digest leaf of the chunk. It points into the allocation that values owns, so it
899
+ // lives as long as values, and every checkpoint that shares the chunk shares the leaf.
882
900
  template <typename T>
883
901
  struct CheckpointChunk {
884
902
  std::shared_ptr<const T[]> values;
885
903
  std::uint32_t count = 0;
886
904
  std::uint64_t allocationBytes = 0;
905
+ const StrongLeafMemo* memo = nullptr;
906
+ };
907
+
908
+ // The key of the first row of a chunk. Chunk ends and chunk matches use it.
909
+ struct CheckpointChunkKey {
910
+ std::uint64_t first = 0;
911
+ std::uint64_t second = 0;
912
+ bool operator==(const CheckpointChunkKey&) const = default;
887
913
  };
888
914
 
915
+ inline std::uint64_t checkpointChunkKeyHash(const CheckpointChunkKey& key) noexcept {
916
+ std::uint64_t value = key.first * 0x9E3779B97F4A7C15ull;
917
+ value ^= key.second + 0x632BE59BD9B4E019ull + (value << 6) + (value >> 2);
918
+ value ^= value >> 30;
919
+ value *= 0xBF58476D1CE4E5B9ull;
920
+ value ^= value >> 27;
921
+ value *= 0x94D049BB133111EBull;
922
+ value ^= value >> 31;
923
+ return value;
924
+ }
925
+
926
+ struct CheckpointChunkShape {
927
+ // Values per row. A chunk ends only at the end of a row.
928
+ std::size_t rowValues = 1;
929
+ // A row ends its chunk when its key hash has all of these bits.
930
+ std::uint64_t endMask = 0;
931
+ // A chunk ends at this number of rows.
932
+ std::size_t maxRows = 1;
933
+ };
934
+
935
+ // Calls visit(start, length) once per chunk of values, in order. The chunk ends are a function of the
936
+ // rows only: no previous checkpoint and no storage history changes them. Two worlds with equal values
937
+ // get equal chunks, so a digest over chunk leaves is a digest of the values.
938
+ template <typename T, typename KeyOf, typename Visit>
939
+ void forEachCheckpointChunkRange(
940
+ std::span<const T> values, const CheckpointChunkShape& shape, KeyOf&& keyOf, Visit&& visit) {
941
+ if (values.empty()) return;
942
+ const std::size_t count = values.size();
943
+ const std::size_t rows = (count + shape.rowValues - 1) / shape.rowValues;
944
+ std::size_t chunkStart = 0;
945
+ for (std::size_t row = 0, rowsInChunk = 1; row < rows; ++row, ++rowsInChunk) {
946
+ const std::size_t offset = row * shape.rowValues;
947
+ const bool ends = row + 1 == rows || rowsInChunk == shape.maxRows
948
+ || (checkpointChunkKeyHash(keyOf(values.data() + offset, std::min(shape.rowValues, count - offset)))
949
+ & shape.endMask) == shape.endMask;
950
+ if (!ends) continue;
951
+ rowsInChunk = 0;
952
+ const std::size_t chunkEnd = std::min(count, (row + 1) * shape.rowValues);
953
+ visit(chunkStart, chunkEnd - chunkStart);
954
+ chunkStart = chunkEnd;
955
+ }
956
+ }
957
+
958
+ // The 8 events (3 KB) or 16 cache rows (1.2 KB) of a typical chunk keep the copy for a changed row
959
+ // small and the chunk list short.
960
+ inline constexpr CheckpointChunkShape kContactEventChunkShape{1, 7, 32};
961
+ inline constexpr CheckpointChunkShape kContactCacheChunkShape{kContactCacheStride, 15, 64};
962
+
963
+ // A contact event row is one event. Its key is the body pair.
964
+ inline CheckpointChunkKey contactEventChunkKey(const WorldContactEvent* row, std::size_t) noexcept {
965
+ return {row->bodyA.value, row->bodyB.value};
966
+ }
967
+
968
+ // A contact cache row is kContactCacheStride values. Its key is the bits of
969
+ // the two body indices. A short last row uses the values that it has.
970
+ inline CheckpointChunkKey contactCacheChunkKey(const double* row, std::size_t available) noexcept {
971
+ CheckpointChunkKey key;
972
+ if (available > 0) std::memcpy(&key.first, row, sizeof(double));
973
+ if (available > 1) std::memcpy(&key.second, row + 1, sizeof(double));
974
+ return key;
975
+ }
976
+
889
977
  // The logical sequence is the concatenation of the chunks in order.
890
978
  // selfAllocationBytes is the table and its chunk list. totalAllocatedBytes
891
979
  // adds every chunk, also the chunks that other checkpoints share.
@@ -954,6 +1042,15 @@ class CheckpointChunkView {
954
1042
  iterator end() const noexcept {
955
1043
  return table_ ? iterator(table_->chunks.data() + table_->chunks.size(), 0) : iterator();
956
1044
  }
1045
+ std::size_t chunkCount() const noexcept {
1046
+ return table_ ? table_->chunks.size() : 0;
1047
+ }
1048
+ // Calls visit once per stored chunk, in order.
1049
+ template <typename Visit>
1050
+ void forEachChunk(Visit&& visit) const {
1051
+ if (!table_) return;
1052
+ for (const CheckpointChunk<T>& chunk : table_->chunks) visit(chunk);
1053
+ }
957
1054
  // Calls visit once per chunk, in order, with a span that is not empty.
958
1055
  template <typename Visit>
959
1056
  void forEachSpan(Visit&& visit) const {