3

I have a Xamarin Forms application that I originally developed using Prism and Unity. I am finding Unity to be rather slow when first resolving an object though once that first time has passed all subsequent resolutions of the same object type are significantly quicker.

Based upon some unrelated advice I received on another thread I decided to try swapping from Unity to DryIoc. Unfortunately I am finding this to be even slower when resolving objects but without the speed improvement when subsequently resolving new instances of those same object types.

So, my question is this, is there anything that I can do with either of the containers that will improve the performance? For example...

  • Is there anything within Unity that will allow me to pre-configure the objects that are likely to be created so that the first resolution is as fast as the subsequent resolutions?
  • Is there anything I can do with DryIoc that will speed up all object resolutions?

For reference, my own usage of each is very simple; I am using RegisterInstance for most of my objects (settings, database etc.) and injecting them into my ViewModel objects through the constructors. The Forms and ViewModel objects themselves are all registered using the Prism RegisterTypeForNavigation extension method

Martin Robins
  • 6,033
  • 10
  • 58
  • 95
  • 1
    @dadhi might be able to point out some optimizations for you. You also may want to swap out `RegisterInstance` for `UseInstance`. That said are you sure it's the container and not something else in your app? If you look at the [benchmarks](http://www.palmmedia.de/Blog/2011/8/30/ioc-container-benchmark-performance-comparison), DryIoc is one of the faster containers available. – Dan Siegel May 30 '17 at 14:34
  • That is what I thought and why I tried swapping to it. I will look at the differences between UseInstance and RegisterInstance and see if anything stands out. The delay is always within the Prism INavigationService.NavigateAsync but as there is practically no delay when navigating to the same form a second time when using Unity I have kind of ruled out that along with the ViewModelLocator service. – Martin Robins May 30 '17 at 15:51
  • `RegisterInstance` was deprecated in favor of `UseInstance` for DryIoc. Are you using any of the Navigation interfaces (`IConfirmNavigation`, `INavigationAware`, etc)? Any code you have in there may be what is actually slowing you down. – Dan Siegel May 30 '17 at 15:54
  • @DanS. Yes, I do use IConfirmNavigation and INavigationAware as well as INavigationService. As I say though, if I use Unity it is slow for the first navigation (can be as much as a few seconds) but then subsequent navigations are quicker (sub-second) whereas when using DryIoc it remains slow for all navigations (though not as slow as the first navigation with Unity). I had hoped that swapping to DryIoc would address the initial delay as the benchmarks suggested it might but was not banking on the delays being there every time an object is resolved, – Martin Robins May 30 '17 at 16:10
  • 1
    without know the specifics of what you're doing in your app it is really quite difficult to say what's causing this behavior. If want to post a sample that replicates the behavior I'm certainly happy to take a look. Otherwise you can reach me on the Prism Slack channel where I can privately take a look at your issue. – Dan Siegel May 30 '17 at 16:15
  • @DanS. How do I get an invitation to the slack channel? I have tried https://prismslack.herokuapp.com/ but it is again at it's limits. – Martin Robins May 30 '17 at 16:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/145478/discussion-between-dan-s-and-martin-robins). – Dan Siegel May 30 '17 at 16:26

2 Answers2

2

First, you need to switch to UseInstance and check results.

Then ideally, it would be great to have a test of DryIoc vs Unity for specific use case.

Last, it may be related to this issue https://bitbucket.org/dadhi/dryioc/issues/453/migration-from-unity

dadhi
  • 4,807
  • 19
  • 25
  • Thanks for the help. It is all working properly now (see my own answer) but I will be completing the switch to DryIoc and will look into using UseInstance instead of RegisterInstance - will let you know how I get on. – Martin Robins May 31 '17 at 15:27
  • Glad it solved, looking forward to your feedback in any form. – dadhi May 31 '17 at 18:05
1

The problems I was seeing were not related to the IoC containers but instead a problem with Xamarin that was introduced at the beginning of May with the 15.2 update of Visual Studio. Installing the latest update (15.2.26430.12) and rebuilding the solution restored the kind of performance that I was expecting.

Martin Robins
  • 6,033
  • 10
  • 58
  • 95
  • 1
    Everyone says how Unity is slow and DryIoc is the way to go. I'm using Unity and my XF app takes seconds to get after the splash screen. You're saying DryIoc is not actually going to improve things, despite all the benchmarks I've seen. What did you end up using? – Don Box Nov 12 '17 at 10:32