Skip to content

Commit add69f3

Browse files
committed
CopyTo fn
1 parent 3bfd208 commit add69f3

File tree

1 file changed

+11
-59
lines changed

1 file changed

+11
-59
lines changed

src/Archetype.cs

Lines changed: 11 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ internal Column(ref readonly ComponentInfo component, int chunkSize)
1515
Data = Lookup.GetArray(component.ID, chunkSize)!;
1616
// Changed = new uint[chunkSize];
1717
}
18+
19+
20+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
21+
public void CopyTo(int srcIdx, ref readonly Column dest, int dstIdx)
22+
{
23+
Array.Copy(Data, srcIdx, dest.Data, dstIdx, 1);
24+
}
1825
}
1926

2027
[SkipLocalsInit]
@@ -272,13 +279,9 @@ private EcsID RemoveByRow(ref ArchetypeChunk chunk, int row)
272279

273280
var srcIdx = _count & CHUNK_THRESHOLD;
274281
var dstIdx = row & CHUNK_THRESHOLD;
275-
var items = Components;
276-
for (var i = 0; i < items.Length; ++i)
282+
for (var i = 0; i < Components.Length; ++i)
277283
{
278-
var arrayToBeRemoved = chunk.Columns![i].Data;
279-
var lastValidArray = lastChunk.Columns![i].Data;
280-
281-
CopyData(lastValidArray, srcIdx, arrayToBeRemoved, dstIdx, 1, items[i].Size, items[i].IsManaged);
284+
lastChunk.Columns![i].CopyTo(srcIdx, in chunk.Columns![i], dstIdx);
282285
}
283286

284287
ref var rec = ref _world.GetRecord(chunk.EntityAt(row).ID);
@@ -308,11 +311,7 @@ private EcsID RemoveByRow(ref ArchetypeChunk chunk, int row)
308311
internal EcsID Remove(ref EcsRecord record)
309312
=> RemoveByRow(ref record.Chunk, record.Row);
310313

311-
internal Archetype InsertVertex(
312-
Archetype left,
313-
ComponentInfo[] sign,
314-
EcsID id
315-
)
314+
internal Archetype InsertVertex(Archetype left, ComponentInfo[] sign, EcsID id)
316315
{
317316
var vertex = new Archetype(left._world, sign, _comparer);
318317
var a = left.All.Length < vertex.All.Length ? left : vertex;
@@ -344,11 +343,7 @@ internal ref ArchetypeChunk MoveEntity(Archetype newArch, ref ArchetypeChunk fro
344343
++y;
345344
}
346345

347-
var fromArray = fromChunk.Columns![i].Data;
348-
var toArray = toChunk.Columns![j].Data;
349-
350-
// copy the moved entity to the target archetype
351-
CopyData(fromArray, srcIdx, toArray, dstIdx, 1, items[i].Size, items[i].IsManaged);
346+
fromChunk.Columns![i].CopyTo(srcIdx, in toChunk.Columns![j], dstIdx);
352347
}
353348

354349
_ = RemoveByRow(ref fromChunk, oldRow);
@@ -514,49 +509,6 @@ public void Print(int depth)
514509
}
515510
}
516511

517-
internal sealed class RawArrayData
518-
{
519-
public uint Length;
520-
public uint Padding;
521-
public byte Data;
522-
}
523-
524-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
525-
private static void CopyData(Array src, int srcIdx, Array dst, int dstIdx, int count, int elementSize, bool isManaged)
526-
{
527-
Array.Copy(src, srcIdx, dst, dstIdx, count);
528-
}
529-
530-
private static unsafe void CopySimd(ref byte src, ref byte dst, int totalBytes)
531-
{
532-
int vectorSize = Vector<byte>.Count; // SIMD chunk size
533-
int offset = 0;
534-
535-
// Perform vectorized copy
536-
while (offset + vectorSize <= totalBytes)
537-
{
538-
var vector = Unsafe.ReadUnaligned<Vector<byte>>(ref Unsafe.Add(ref src, offset));
539-
Unsafe.WriteUnaligned(ref Unsafe.Add(ref dst, offset), vector);
540-
offset += vectorSize;
541-
}
542-
543-
// Process remaining bytes in chunks of 8 (long)
544-
const int wordSize = sizeof(long); // 8 bytes
545-
while (offset + wordSize <= totalBytes)
546-
{
547-
long word = Unsafe.ReadUnaligned<long>(ref Unsafe.Add(ref src, offset));
548-
Unsafe.WriteUnaligned(ref Unsafe.Add(ref dst, offset), word);
549-
offset += wordSize;
550-
}
551-
552-
// Process remaining bytes one by one
553-
while (offset < totalBytes)
554-
{
555-
Unsafe.Add(ref dst, offset) = Unsafe.Add(ref src, offset);
556-
offset++;
557-
}
558-
}
559-
560512
public int CompareTo(Archetype? other)
561513
{
562514
return Id.CompareTo(other?.Id);

0 commit comments

Comments
 (0)