Bug 38529 - UpdateIdValues for the main assembly is called a lot of times
Summary: UpdateIdValues for the main assembly is called a lot of times
Alias: None
Product: Android
Classification: Xamarin
Component: General ()
Version: 6.0.0
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Atsushi Eno
Depends on:
Reported: 2016-02-08 02:45 UTC by Jerome Laban
Modified: 2016-02-09 17:03 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

UpdateIdValues many calls repro (47.06 KB, application/x-zip-compressed)
2016-02-09 14:09 UTC, Jerome Laban

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Jerome Laban 2016-02-08 02:45:43 UTC
Using Xamarin.Android (and most probably any version below) the method Resource.UpdateIdValues for the main assembly is called a very large number of times.

It is called by number of classes and nested classes for every generated "Resource" class, for every assembly in the current package, because of their .cctor calling global::Android.Runtime.ResourceIdManager.UpdateIdValues().

In a large application, this method weights about 9500 lines of simple (mostly duplicate) assignations and on a quite fast CPU (i7-4790 @ 3.6GHz) virtual machine, the time spent running all these UpdateIdValues is about 280ms.

Lower powered CPUs, on actual devices, this time goes up significantly and impacts the startup time of the application.
Comment 1 Jonathan Pryor 2016-02-08 16:47:07 UTC
@Eno: Could UpdateIdValues() have a check to skip re-running after it's been run once?
Comment 2 Atsushi Eno 2016-02-08 17:13:11 UTC
UpdateIdValues() is invoked only once per type, it is in the static constructor. There is no chance for re-running. And they are invoked because they are required.

We need reproducible case that this really matters.
Comment 3 Jerome Laban 2016-02-09 14:09:10 UTC
Created attachment 14960 [details]
UpdateIdValues many calls repro
Comment 4 Jerome Laban 2016-02-09 14:12:05 UTC
To repro:

* Restore all the packages (all googleplay is included to make Resources.cs very large)
* Place a breakpoint somewhere in the UpdateIdValues method
* Notice that the method is called multiple times

This gets worse if there are libraries to be initialized, because all Resource.cs classes call global::Android.Runtime.ResourceIdManager.UpdateIdValues();
Comment 5 Atsushi Eno 2016-02-09 14:53:15 UTC
Thank you for the repro case. OK, you are right, there could be problematic cases that it could matter in reality.

As a principle, they are invoked because they are required, and they are invoked once, and the number of the calls are not going to be reduced.
But there could be chance that we can optimize the implementation of UpdateIdValues() like reducing required reflection calls for newer libraries (they are there because we need to keep compatibility with libraries that had been published earlier).
It is not likely short term task to slip in to the next major release, but I would seek for the chance in that direction.
Comment 6 Atsushi Eno 2016-02-09 17:03:30 UTC
Actually Jonp came up with another idea to quickly workaround this issue and now it is applied. So hopefully it is included in the next major release (unless it causes any drawback).
Thanks for the report.

[master 63415c2]