Skip to content

Commit 4f163d6

Browse files
committed
Started adding data locality
1 parent 7beeef9 commit 4f163d6

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

README.md

+15-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Patterns from the book Game Programming Patterns:
2424
13. [Component](#13-component)
2525
14. [Event Queue](#14-event-queue)
2626
15. [Service Locator](#15-service-locator)
27-
16. ~~[Data Locality](#16-data-locality)~~
27+
16. [Data Locality](#16-data-locality)
2828
17. [Dirty Flag](#17-dirty-flag)
2929
18. [Object Pool](#18-object-pool)
3030
19. [Spatial Partition](#19-spatial-partition)
@@ -324,7 +324,7 @@ This pattern is almost the same as the [Observer](#3-observer) pattern. The only
324324

325325
**How to implement?**
326326

327-
Combine the [Command](#1-command) pattern with a C#'s built-in queue. In the Update method you pick the first Command in the queue and run it while measuring time. If you have time to spare, you run the next Command, and so on until you are out of time. How much time you can spend on the Event Queue each update depends on the game, so you have to experiment.
327+
Combine the [Command](#1-command) pattern with a C#'s built-in queue. In the Update method you pick the first Command in the queue and run it while measuring time. To measure time you can use System.Diagnostics.Stopwatch. If you have time to spare, you run the next Command, and so on until you are out of time. How much time you can spend on the Event Queue each update depends on the game, so you have to experiment.
328328

329329
**When is it useful?**
330330

@@ -360,7 +360,19 @@ When making your game you use many standardized methods to for example generate
360360

361361
## 16. Data Locality
362362

363-
-
363+
Have you done all otpimizations you can possible do? Is the game still too slow? Then this pattern may help you. It can make your game faster by accelerating memory access.
364+
365+
**How to implement?**
366+
367+
* You have to arrange data to take advantage of CPU caching. The basic idea is that you should organize your data structures so that the things you're processing are next to each other in memory. This is a big topic and can't be summarized here, so you should read about it in the book.
368+
369+
* This Unity article suggest that you should use struct instead of class because they are more cache friendly [How to Write Faster Code Than 90% of Programmers](https://jacksondunstan.com/articles/3860).
370+
371+
* Unity has implemented this pattern in their [Data-Oriented Technology Stack (DOTS)](https://unity.com/dots).
372+
373+
**When is it useful?**
374+
375+
* According to the book, this pattern should be used when everything else has failed. It's a waste of time to optimize code that doesn't need to be optimized - and it may also make the code more complicated to understand. You also have to make sure that cache misses is the reason your code is slow, so you have to first measure it.
364376

365377

366378

0 commit comments

Comments
 (0)