4 ms·
I watched the linked original video to understand the motivation, and read the article, and I think this is a misguided solution to a real problem. These "join
by jzwinck 11y ago
I watched the linked original video to understand the motivation, and read the article, and I think this is a misguided solution to a real problem.
These "joint allocations" seem to be "a small pool of heterogenous types." The motivation is that heap allocations are expensive, so we want to coalesce them. The only way this would matter at all is if we are creating lots of these Mesh objects.
The given solutions make one heap allocation per Mesh object, as opposed to three. Create six million Meshes and you get two million allocations.
Instead, one pool should be created for each of the three array types: Vector2, int, and Vector3. Now, create six million Meshes and you can do somewhere between three and a few dozen allocations, depending on your pool's initial size and growth strategy.
- jasode 11y ago>, create three million Meshes I didn't watch the presentation so I may be wrong but it's possible you're misinterpret how Mesh was intended to be used. The "million" objects is meant to be inside one Mesh. Mesh is singular. The vertices are plural (millions). If there's more than one Mesh, it would be dozens or hundreds of Mesh (per video game characters).
- jzwinck 11y agoBut if you only ever create a single Mesh, then you wouldn't care to coalesce the allocations, because three allocations instead of one is inconsequential in the course of an entire program lifetime. I agree that there are many vertices and edges within each Mesh, but there better also be tons of Meshes within one program, or there is no problem to begin with.
- svalorzen 11y agoI believe that the main problem is not allocation time (games already have loading times, that's not a problem), but fast access and not breaking the cache.
- jzwinck 11y agoThe title of the first slide in the video is "Heap allocations are expensive!" And using a pool the way I'm proposing does not fragment the heap. It might be slightly worse if you serially touch a lot of Meshes which have only a few vertices and edges each, but this is a corner case and a trade-off anyway.
- slavik81 11y agoMeshes are typically quite big. Moving two small blocks of data together helps to improve cache behaviour as the two blocks can then fit in a single cache line. Moving two giant blocks of data closer together is not going to make much difference, because they probably span hundreds or thousands of cache lines anyways.
- quicknir 11y agoI don't know as I'm not a game developer, but my guess is that individual Meshes may correspond to things that need to be created and destructed on a regular basis during runtime. If that's the case, then depending on the details it may be preferable to have an array of Meshes, and not try to pool all the Meshes together. I think it's risky to say that it's misguided without the full context of the problem.