Member-only story

Data-Oriented-Design — A Swift Introduction

Marcel Kulina
Better Programming
Published in
9 min readMar 21, 2023

Photo by Conny Schneider on Unsplash

The problem

Think about a game where we have hundreds or even thousands of units moving at the same time. All these units would need a way to know when they should move, where they can move, and much more. All these units have some behaviour in common: movement.

Note: The code in this article is just for explanation. It lacks many things and is untested. It is mainly intended to showcase what an implementation can look like.

Our first step here is to define a structure that holds the movement. We call it Vector3:

struct Vector3 {
// Convenience zero vector
static let zero = Vector3(x: 0, y: 0, z: 0)

var x: Float
var y: Float
var z: Float

init(x: Float, y: Float, z: Float) {
self.x = x
self.y = y
self.z = z
}
}

As a developer, this would be the perfect fit for a simple composition. So we define an interface.

protocol MoveUnit {
func move()
}

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Write a response

Interesting--but highly specialized--design, complicated by the fact that almost any object complex enough to be useful also consists of other dynamically allocated objects (Strings, Arrays, Dictionaries, etc.), which in turn implies that looping…

if you need to consider cpu performance, it’s way beyond usual app development.
you are comparing special purpose design with general purpose.
not to mention how do you know the memory layout?
and cpu architecture isn’t just memory layout. A lot of other factors that impact performance.