6 Common data structures: List, Stack, and Queue
This chapter covers
- How built-in data structures like List, Stack, and Queue can negatively affect our 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 similar to object pools, avoiding both memory fragmentation and triggering the garbage collector.
If you haven’t caught on yet, data-oriented design is all about using arrays, preferably arrays of primitive types. Arrays are great when we have a set amount of data, like a fixed 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 they are detrimental to memory management.