patrickdenglerhobbisoft / WorldDefense

World Defense Bugs
0 stars 0 forks source link

Optimizations #32

Open Helikin opened 8 years ago

Helikin commented 8 years ago

Here is a collection of possible optimizations.

I will add more optimization notes when I see an obvious optimization opportunity in the code.

Helikin commented 8 years ago

I started the game in a profiler and the LocationManager.MapResolution property takes most of the time. I checked the code and found this:

public double MapResolution
        {
            get
            {
                double temp=0;
               Application.Current.Dispatcher.Invoke(
                    DispatcherPriority.Background,
                    (Action)delegate ()
            { 

                double latitudeRadians = MathHelper.ToRadians(_map.Center.Latitude);
                temp= 156543.04 * Math.Cos(latitudeRadians) / Math.Pow(2, _map.ZoomLevel);
            });
                return temp;
            }
        }

If I remove the Invoke() everything works fine and a lot faster. (Changes not checked in.) This code is terrible. It blocks the current thread until the Dispatcher thread executes the delegate with low "Background" priority. If there are cross thread call problems, then I suggest that the LocationManager listens to Bing Map view change events and caches any values it needs.

Helikin commented 8 years ago

I have commented out the Dispatcher.Invoke code and added an assert to check for cross-thread problems. Besides causing performance problem, the Invoke() also changed the initialization order and some asserts failed when a game was started with Continue.