LambdaSix / InfiniMap

InfiniMap - (Nearly) Infinite Maps
MIT License
27 stars 1 forks source link

Chunk reference caching #16

Open graspee opened 7 years ago

graspee commented 7 years ago

I'm just about to write my own generic infinite map structure so i took a look at yours to see how you did it. I've only taken quick looks really so far so apologies if I've got this wrong but it seems like every time you want to get the data associated with an co-ord you are doing a dictionary look up on the co-ords to find what chunk it's in. Are you concerned about the performance of this when interrogating a lot of cells often, such as in pathfinding or FOV ?

I was considering having each chunk have a chunk reference for the 8 chunks surrounding it (2d version of infinimap only). If you wanted to check a line of cells going east for example you would check to see if you had gone off the edge of a chunk yet and if you had you would check the chunk's "east" chunkref; if it's nul you still have to look it up in the dictionary but then you write the answer in there so you only have to do it once. I was thinking entities (e.g. mobs) would have a chunk reference field so when you wanted to move them or check stuff associated with them you would be on the right chunk to start with.

Am I being too paranoid about the dictionary look up speed?

LambdaSix commented 7 years ago

I haven't extensively performance tested the chunk storage on large maps with heavy pathfinding so I can't specifically speak to that.

For the chunk referencing, I did do an experiment with holding references in that way and maintaining a short-list of recently accessed chunks, I found that it didn't work very well with the way the code currently handles unloading dormant chunks and entity references; as I had two separate cache-pools to look after.

The library is generally tailored towards least-effort usage, so the normal access pattern is that every call to the indexer calculates and retrieves the chunk containing those coordinates via dictionary lookup, to handle deferred loading/generation and unloading simply.

But if you know in advance you'll be doing a large number of accesses on a specific area, you can use ChunkMap<T>::ChunksWithin to get references to Chunk's between two sets of world space coordinates, which will elide the dictionary lookup requirement.