Critical performance issue in Xamarin.Forms Layouts

AlexeiGarbuzenkoAlexeiGarbuzenko UAMember ✭✭
edited July 2014 in Xamarin.Forms

Guys,
Right now I'm in the middle of porting process of my huge WinRT project from WPF to XF. I'm trying to keep same architecture and layout and that helped to find serious bottleneck.

First of all I found very long startup time on real iOS devices. You know, Apple hardware is really powerful so that was a first signal that something is wrong. But the things gone mad when I added a popup window with dynamic content.
Take a look at that picture:

You can also watch the movie from iOS Simulator to see how it works in mostly ideal environment. I wasn't able to get such performance on real devices:

Whole page is a ContentPage containing a Grid with back image, Grid with a nested Grid for a menu, Grid for misc controls and buttons and whole new ContentView for popup, sliding from the top. Popup contains Grid as a parent with back image, nested Grid for navigation and paging and 3x2 Grid for each page with thumbnails. You might already mentioned a lot of Grids here. Actually, any other Layout works the same, main idea is huge control tree in real-world project. That structure worked perfectly on Windows 8 and WPF, but on Xamarin.Forms loading thumbnails into popup lasted 5+ seconds in iOS Simulator. Even more, it took about 20 (!!) seconds on iPod 4 to do the same.

First of all I though that it might be caused by lot of grids, but changing them to AbsoluteLayouts gave nothing at all. Than I thought it might be because of image loading, but even removing thumbnails, custom fonts, frames, etc. gave no significant change.
After 6 hours struggling with the system I realized that every time I add the control to existing hierarchy there is significant lag. I made few tests and found that instead of adding each thumbnail and description directly to popup I can create 'proxy' object (i.e. Grid :)), put everything inside and than add it to the page. That approach allowed me to minimize the time to 500ms on Simulator, ~1 s on iPad and ~4s on iPod.

At that point I was sure that there are some minor issues on my side and tried to optimize the hierarchy and get rid of 1/2 of Grids. I flattened the hierarchy (despite the fact that most of markup now looks like a flat cuneiform table + there is a code that layouts some of controls, instead of using aligns) and got 100ms loading time on Simulator (as you've seen in the movie), 500ms on iPad, 1s on iPod. You might call it a victory, but from my point of view it is still too much - native iOS apps can handle this in less than 15 ms without any significant delay.

Than I captured the traces with Mono Profiler. To minimize influence of other parts I just start the app, click the button to show pop-up and close the app when it ready. Session length is about ~20 seconds, popup creation took about 5 seconds, startup took ~10 seconds. Take a look what took the most time:

Method call summary
Total(ms) Self(ms)      Calls Method name
   15433        1        608 Xamarin.Forms.Platform.iOS.VisualElementPackager:HandleChildAdded (Xamarin.Forms.VisualElement)
   15001        1       4161 Xamarin.Forms.VisualElement:InvalidateMeasure ()
   14991       65       1327 Xamarin.Forms.Layout:OnChildMeasureInvalidated (object,System.EventArgs)
   14563       29       1826 Xamarin.Forms.EnumerableExtensions:ForEach (System.Collections.Generic.IEnumerable`1,System.Action`1)
   13751        4        609 Xamarin.Forms.Platform.iOS.VisualElementPackager:Load ()

Full trace here: http://paste2.org/FUBCh4hP

My popup processing is somewhere in the middle:
4558 0 1 Vkraina.MainView:MapsButton_OnClick (object,System.EventArgs)

It took 4.5 seconds, that is not as huge comparing to other parts - invalidating measures and handling collection updates took 15 seconds of total 20 seconds application run time!

After looking at the deeper trace I realized that there is nothing I can do on my side, because most of the time was spend in internals alike Xamarin.Forms.Layout:InternalChildrenOnCollectionChanged:
http://paste2.org/8cwHjYa5

So I gave up and temporary suspended the project - it was great idea to use Xamarin.Forms for iOS and Android port, but now with such performance issues I'm really thinking of switching to native UI. Or I can continue porting process and pray God (or XF team?) for an urgent fix before the deadline. Because my customer will surely kill me for poor performance on real devices.

Please guys, take a look at the problem, we could benefit even from a temporary solution with pair of simple BatchLayoutBegin/BatchLayoutCommit methods that would group all internal callbacks during dynamic content creation and application loading stage.

Thank you!

Answers

  • EricMaupinEricMaupin USXamarin Team Xamurai

    Would you be willing to privately email (ermau @ xamarin) us your app? There's some improvement coming in the next update, but we'd like to take a look at how your app does and see specifically how you're using things.

  • MaruMaru DEMember ✭✭

    @ermau‌ It would be nice when you post back here when you should find something. Maybe some things come to light that we must look for when programming things. :)

  • AlexeiGarbuzenkoAlexeiGarbuzenko UAMember ✭✭

    @ermau‌
    I'm just checking, did you get my letter with app sources? Also is there any estimate on 'next update' with improvement you mentioned?

  • MaruMaru DEMember ✭✭

    Pushing this!

    @ermau‌ Any news on this?

    @Nomad1‌ Did you updated to the latest release and got improvements?

  • AlexeiGarbuzenkoAlexeiGarbuzenko UAMember ✭✭

    Just checked it with test project and iPad 3.
    With 1.1.1.6206
    Processing Maps button took 00:00:01.2331980

    With 1.2.1.6229:
    Processing Maps button took 00:00:02.0877455

    On simulator I also got 2 times regression.

    Also 1.2.0 broke iOS 5 compatibility because of attributed text, so I can't test on my slower device, iPod 4.

  • EricMaupinEricMaupin USXamarin Team Xamurai

    I'm just checking, did you get my letter with app sources? Also is there any estimate on 'next update' with improvement you mentioned?

    I did, but haven't had a chance to look at it yet. 1.2 should have improved things, we will investigate.

  • AlexeiGarbuzenkoAlexeiGarbuzenko UAMember ✭✭

    No performance changes in 1.2.2 pre 1/2/3, still slow as hell, few seconds for generating layout on iPad 3.

  • AlexeiGarbuzenkoAlexeiGarbuzenko UAMember ✭✭

    With 1.2.2.6243

    2014-08-04 10:51:24.583 VkrainaiOS[1264:a0b] Processing Maps button took 00:00:00.4072070
    2014-08-04 10:51:30.150 VkrainaiOS[1264:a0b] Processing Maps button took 00:00:00.3522716
    2014-08-04 10:51:32.082 VkrainaiOS[1264:a0b] Processing Maps button took 00:00:00.3401626
    2014-08-04 10:51:37.910 VkrainaiOS[1264:a0b] Processing Maps button took 00:00:00.3599792
    

    With 1.1.1.6206

    2014-08-04 11:23:23.378 VkrainaiOS[1400:a0b] Processing Maps button took 00:00:00.2321357
    2014-08-04 11:23:26.087 VkrainaiOS[1400:a0b] Processing Maps button took 00:00:00.2077810
    2014-08-04 11:23:28.290 VkrainaiOS[1400:a0b] Processing Maps button took 00:00:00.1863201
    2014-08-04 11:23:30.354 VkrainaiOS[1400:a0b] Processing Maps button took 00:00:00.1863015
    

    These numbers are from simulator on i5 Mac. On real devices things are 10 times slower, meaning 1.5-2 seconds when showing dialog on iPad 3 with 1.1.1 and up to 4 seconds lag with 1.2.2.

    Let me remind that there is nothing criminal in the code: I'm just adding 46 thumbnails with captions to container.

  • AndreyLitvinovAndreyLitvinov RUMember
    edited August 2014

    UP! 1.2.2 still slow. Our project is ready for publish, but it is impossible. Such slow application does not allow us to earn money. On IOS, on Andorid, on WF no matter. On IOS, on Android, on WF no difference.

    For example:

    • 36116 18892 1 (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
    • 25417 1 6131 Xamarin.Forms.VisualElement:InvalidateMeasure ()
    • 24796 25 2322 Xamarin.Forms.Layout:OnChildMeasureInvalidated (object,System.EventArgs)

    InvalidateMeasure ?! This is all useful work?

  • steveleonardsteveleonard USMember

    I am having similar issues, our app is ready to ship but its just to slow. Is there any news on improvements?

  • AlexeiGarbuzenkoAlexeiGarbuzenko UAMember ✭✭

    Nope. 1.2.3 pre 1 have same performance issues. 1.1.1.6206 have 2x better performance than current builds.

  • xcodexcode USMember
    edited August 2014

    i hope xamarin team is doing something for this problem.

    and that's true slow app can not earn money. because clients dun like laggy or freeze .

  • +1

    Screens are taking a long time to show up, and I strongly suspect it is due to the views laying themselves out.

  • Slightly faster, but still have performance issues with 1.2.3 pre 2:

    2014-09-10 10:55:37.895 VkrainaiOS[22051:a0b] Processing Maps button took 00:00:00.3602963 2014-09-10 10:55:41.639 VkrainaiOS[22051:a0b] Processing Maps button took 00:00:00.2908061 2014-09-10 10:55:43.926 VkrainaiOS[22051:a0b] Processing Maps button took 00:00:00.2892052

    P.S. my app is already released on iOS and Google Play with XF 1.1.1 having descent performance, but it still 2x faster than with current branch.

  • xcodexcode USMember

    hello , can you tell me how to down grade to 1.1.1 xamarin ??

  • AlexeiGarbuzenkoAlexeiGarbuzenko UAMember ✭✭
    edited September 2014

    @xcode‌

    Write this line in NuGet Console:
    Update-Package Xamarin.Forms -version 1.1.1.6206

    If you are using Xamarin Studio, you may need to install 'Package Console Extension'.

  • mattwardmattward GBMember Xamurai

    @xcode @Nomad1 - In Xamarin Studio you can also find older package versions in the Add Packages dialog. If you run a search like the following:

    Xamarin.Forms version:*
    

    You will see all the NuGet package versions for Xamarin.Forms in the Add Packages dialog. This will mean you do not need to install the other addin to get the Package Console Extension window.

  • MihaMarkicMihaMarkic SI ✭✭✭✭

    @Nomad1 you might try this approach where I create some sort of "nested don't do layout"

  • @MihaMarkic
    I already did as workaround - created custom layout container that stops notification hell for child controls. It speeds up whole narrow part by approx. ~50%, but things still horribly slow, especially on 1.2.* branch. To solve the issue completely I have either to flatten all hierarchy, avoid nesting controls, etc., or wait for XF fix. I choose second approach and stick to 1.1.1 branch for that project.

  • hcustohcusto BAMember, Beta

    @Nomad1‌ Did you tested performance of XF 1.3? Anything changed?

  • hcustohcusto BAMember, Beta

    Uuupppssss, anyone from Xamarin have any comments? It would help us a lot if you can take a look and see is this performance because of "something" particular in this project or?

  • MichaelOConnorMichaelOConnor USUniversity ✭✭

    I am seeing similar issues since I upgraded to Xamarin Forms 1.3. My app is now too slow to ship, I'm dead in the water. In debug I can literally see it take 3-4 seconds to add a control to a StackLayout. I tried putting a version on the iPad without any debug code and running it and it's only marginally better.

  • VincentNyangaVincentNyanga USMember

    Similar issues with me. The app takes time for the first page to appear. Something should be done.

  • EvgeniyZverevEvgeniyZverev USMember ✭✭

    Any news?

  • JohnHardmanJohnHardman GBUniversity ✭✭✭✭✭

    Can anybody from Xamarin tell us if there is a performance test run as part of the dev/test/release cycle, that produces any figures that might be of interest to the community (both in terms of times and memory usage)? Obviously, there are too many possible combinations to have every scenario included, but timings of repeated CRUD tests for each type of control in each type of layout on each platform, timings for the layout manager for scenarios involving a range of numbers of controls on a page on each platform, etc. would be a starting point.

    If those are automated to run on the same pieces of kit for each release candidate, any changes between one version and another should be fairly easy to spot.

  • DanielNelson.9924DanielNelson.9924 USMember ✭✭

    We've got a fairly complicated UI working well in XF. It was a b*tch to get it working well. I wish I had understood Xamarin Forms was more for simple apps when I started.

    One of the biggest things that helped was using a "Fixed Label." render, look here:

    http://stackoverflow.com/questions/26364509/why-is-xamarin-forms-so-slow-when-displaying-a-few-labels-especially-on-android/26928690#26928690

    We have renders for labels, buttons, entries and listviews.

  • AlexanderGrfAlexanderGrf DEMember

    I suppose there aren't any news on this, yet?

  • RenaudLaloireRenaudLaloire BEUniversity ✭✭

    Hi,

    I am just adding my own experience here: I have an application that performs well on iOS but on Android, the same screen load takes more than 3 seconds. I do have a lot of components but not that many labels. I do have a lot of grids though ... No matter what I do, it seems that the slowness of my screens is directly linked to the number of elements defined in XAML.

    Xamarin, I can totally understand that you might have some "adjustments" to do especially in Android regarding your layouting engine, and quite frankly, I find it already impressive being able to define custom control in XAML the way I do in WPF but could you please communicate a little more about that matter ?

    Just knowing that you are working on it would already be something but so far, I haven't read much from you on that.

    Thanks in advance.

  • GeorgeCookGeorgeCook PEUniversity ✭✭✭

    @RenaudLaloire they have communicated several times on loading issues. Search the forum for ideas on what to do. The bottom line is that there is not much you can do with android loading times. Xamarin have pretty much done all they can there. It has nothing to do with layout. As for the layout performance issues they have been addressed many times since this thread was started last year.

    I suggest you do a better search for xamarin forms android loading times to see what others do, and/or if your issue is genuinely related to layout engine performance that you start a new thread with your specific problem, screenshots or diagrams and code in order to get assistance.

    You will achieve nothing tacking on vague posts to nearly year old threads.

  • RenaudLaloireRenaudLaloire BEUniversity ✭✭

    @GeorgeCook if what you are saying is correct, that Xamarin has been doing all they can do with android loading times, I would not give much hope to the future of Xamarin.Forms ... Nevertheless, I don't believe one second this is the case and still hope that xamarin is going to communicate soon about the improvement of their XAML layouting engine.

    Now, regarding searching on this forum, I obviously did try every single workaround suggested by the community, that does not mean that the problem of slowness in Xamarin.Forms / Android has been solved.

    I am a strong defender of Xamarin.Forms, like you I suppose, that does not mean you have to be blind regarding areas that must be improved for the sake of the product and its community.

    Thanks for your time.

  • GeorgeCookGeorgeCook PEUniversity ✭✭✭
    edited August 2015

    Hi @RenaudLaloire

    I don't think you understand my point. It sounds like you are referring to two issues : slowness of Xamarin forms in general and slowness of Xamarin Forms loading on android.

    This thread you are posting to, is ancient, and refers to critical slowness a user experienced a year ago. That issue was resolved in subsequent releases.

    It makes no sense to post to this very well written, specific thread, which explicitly documents a specific performance issue with vague ramblings about Xamarin performances and/or loading times.

    My suggestion to you remains : if you have issues about performance, start a NEW thread, which explicitly documents your issue, so we can better help you.

    I think you will find (from the many postings here from Xamarin staffers) that there is in fact a minimum Android loading time, which is not going to disappear anytime soon. I suggest you try the suggestions there to mitigate your loading time, and if not create a new post.

    If your objective is just to blow off steam (and that's cool, too, in my book), then yes, by all means post to a 13 month old thread, kept awake by a few others who are just writing "me too - when will it be fixed", who have possibly only read the title and not the original thread; but don't expect any meaningful help.

    I hope you take this in the intended way : I'm very happy to help you with your application performance issue if you start a thread and sufficiently document your issue (I suggest screenshots, some measurements, and being prepared to answer lots of questions).

    As for your loading time issue; you will see from :
    https://forums.xamarin.com/discussion/45468/3-5-seconds-startup-for-android
    https://forums.xamarin.com/discussion/25170/slow-startup-dll-loading
    https://forums.xamarin.com/discussion/1324/slow-startup

    That there is nothing you can do about that.

    Good day

  • RenaudLaloireRenaudLaloire BEUniversity ✭✭

    @GeorgeCook ... As standard behavior on forums (and I am not talking about XM forums), there is nothing more annoying than people creating new posts on subjects already covered by existing posts. In my case, I do have problem with performance while loading / layouting complex screen and this post is all about that ...

    Now, thank you for the three links you sent to me but I did not write anywhere that I had performance problems loading my dlls as you suggested otherwise I would have posted my comment in one of these.

    I propose to close this discussion as this is not going anywhere.

    Thanks for your time again.

Sign In or Register to comment.