private static void CompositeProxyExample() { // Array of structures (AoS) // Memory layout of creatures: Age X Y Age X Y Age X Y... var creatures = new Creature[100]; for (int i = 0; i < 100; i++) { creatures[i] = new Creature() { Age = 10, X = 0, Y = 0 } } ; foreach (Creature creature in creatures) { creature.X++; } // Structure of Array (SoA) // Preferred memory layout for efficient manipulation: // Age Age Age ... // X X X // Y Y Y var creatures2 = new Creatures(100); foreach (Creatures.CreatureProxy creature in creatures2) { creature.X++; } }
static void Main(string[] args) { var creatures = new Creatures(100); foreach (var c in creatures) { c.X++; } var creatures2 = new Creatures(100); foreach (Creatures.CreatureProxy c in creatures2) { c.X++; } }
public CreatureProxy(Creatures creatures, int index) { _creatures = creatures; this.index = index; }