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 (show other bugs)
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)

See Also:
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

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]

Notice (2018-05-21): bugzilla.xamarin.com will be switching to read-only mode on Thursday, 2018-05-25 22:00 UTC.

Please join us on Visual Studio Developer Community and GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs and copy them to the new locations as needed for follow-up. The See Also field on each Bugzilla bug will be updated with a link to its new location when applicable.

After Bugzilla is read-only, if you have new information to add for a bug that does not yet have a matching issue on Developer Community or GitHub, you can create a follow-up issue in the new location. Copy and paste the title and description from this bug, and then add your new details. You can get a pre-formatted version of the title and description here:

In special cases you might also want the comments:

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.

Note You need to log in before you can comment on or make changes to this bug.