6 Common data structures: List, Stack, and Queue
This chapter covers
- How built-in data structures like List, Stack, and Queue can hurt performance.
- Implementing common data structures like List, Stack, and Queue using data-oriented design.
- Using stack-allocated arrays to pass data from Logic back to the Board.
So far, we have been using arrays of primitive types to store our data. We use arrays for two main reasons. First, they are allocated in a contiguous chunk of memory, which helps us practice data locality and maximizes the chance that our data will be in the L1 cache. Second, we can treat them like object pools, avoiding memory fragmentation and garbage-collector triggers.
If you haven’t caught on yet, data-oriented design is all about using arrays, preferably arrays of primitive types. Arrays work well when you have a fixed amount of data, like a set number of enemies. But what happens when we want to add and remove enemies from an array dynamically? Languages like C# offer built-in data structures, for example, List, that make it easy to add and remove elements.
Unfortunately, popular data structures, including built-in ones like List, Stack, and Queue, don’t align well with data-oriented design. Using them can cause significant performance penalties and hurt memory management.