pioneerspacesim/pioneer — pull request #937 by Ziusudra (2012-02-02 02:20:52+00:00)
Title: CustomSystem speed improvement

This changes `CustomSystem` from using a single list of all custom systems to a map of per sector lists.

While profiling I noticed that `CustomSystem::GetCustomSystemsForSector` was accounting for a surprisingly significant percentage of the time spent in `l_starsystem_get_nearby_systems`, just under 50%. Only slightly less than `StarSystem::GetCached` which I expected to account for the vast majority of time.

With the custom systems in a single list, it had to iterate through all of them to find the few that were in that sector. We currently have only five fully custom systems, but 588 custom systems that specify the star(s) around which systems are generated.

This does use a bit more memory. My testing (more about that later) shows that the increase in average memory is 712.4k or 0.493%.

For `Pi::MainLoop` the average percentage of time spent in `CustomSystem::GetCustomSystemsForSector` was 22.661%. This change reduces that to 0.54%. For `l_starsystem_get_nearby_systems` those numbers are 33.765% to 1.248%. And for `Sector::Sector` they are 84.926% to 11.856%.

Testing was perfromed using a save file in Sol, far from any bodies, and at set-speed:0. The executable was started and the save file loaded from the starting menu. After waiting, still paused, for memory usage to stabilize and recording it, [VerySleepy](http://www.codersnotes.com/sleepy) was used to attach and profile for 60 seconds at maximum time acceleration. This was repeated ten times for each executable.

And now I have some questions:

Is the map (and previously the list) ever destructed? Does it matter?

Would it be better to give `l_add_to_sector` a local empty list that would be copied into the map when needed? Or use the existing new list and copy that in, then delete?

Would it be better if much of this were moved elsewhere?