Flyweight
The flyweight pattern is focused on reducing memory usage for large collections of objects and reducing the time spent loading them in. For the flyweight pattern, we first need to consider three things:
- Intrinsic data – The data values that are immutable and considered to always be true on an object
- Extrinsic data – The data values that are mutable and considered to be changeable per instance
- Memory costs – All data must be stored somewhere and for loaded objects, that means on our RAM
With that in mind, let’s look at trees in the forest. Taking a simple approach, we could load in and store the model, texture, and transform data once for each tree. This would make our data storage look like Figure 3.4, with each tree connected to its own plot of memory, holding its copy of the data needed for rendering:
Figure 3.4 – Diagram showing the data associated with each tree
If we consider the...