Hey guys! After a lot more work, my game is ~1000% faster, but the last optimization is making me scratch my head. To put objects into a dictionary to load, I check every chunk within a radius, get its distance, and have the dictionary sort them by distance. This means the closest chunks load first. I have a few problems with this.
1. I have to do !toLoad.ContainsKey(distanceSq), which is using about 30% of my cpu, because you cannot enter a value multiple times into a dictionary. Is there a faster way to prevent repeats?
2. Just adding to the dictionary gets laggy - it accounts for 40% of my cpu. I understand this is because of the amount of objects I'm loading.
So, I wanted to know if it was possible to get closest objects first without first iterating over all the possibilities. This would remove the need to queue my chunks or anything - just load them as they're detected, and also remove problems 1 and 2 above. I would be extremely appreciative if anyone knows a way I can go about this (It would finally make the game fully optimized)
I am assuming you are using C# or Java by the term dictionary. I'd like to ask for some context by what your chunk is, and what your engine is trying to do. For now I'm going to assume you mean terrain paging.
You're not really using a good option here. Dictionaries are not a viable option for massive amounts of data in realtime use. They will make constant allocations and/or copies as more and more data gets moved in. You want to keep data writing operations on them to a bare minimum.
Don't bother with trying to load in chunk data as you need them. Do that only for the resources. Assuming you are not doing anything crazy stupid, you can safely load most of your world data into memory, and then load the visual resources as you need them sense they take the most space.