Bug 10739 - Linker does not remove unused fields initialisers
Summary: Linker does not remove unused fields initialisers
Alias: None
Product: iOS
Classification: Xamarin
Component: Tools ()
Version: 6.3.x
Hardware: PC Mac OS
: --- enhancement
Target Milestone: Future Cycle (TBD)
Assignee: Sebastien Pouliot
Depends on:
Reported: 2013-02-27 12:42 UTC by Marek Safar
Modified: 2017-09-28 08:27 UTC (History)
4 users (show)

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

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 for Bug 10739 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description Marek Safar 2013-02-27 12:42:01 UTC
Linker does not remove unused fields initialisers. There are quite few examples with code which is never used by simple app (e.g hello world) but still not removed by linker in mscorlib.

For example:
GC.cctor (+dependencies)
System.Reflection.Module.cctor (+dependencies)
and more
Comment 1 Sebastien Pouliot 2013-02-27 16:31:37 UTC
With the exception of the bindings, which are marked [CompilerGenerated] for safety, the linker does not re-write IL because it can be pretty complex. 

What happens is that the fields are not really unused, from the linker perspective, and get marked because:

a) the type, e.g. System.GC, was marked;
b) the static constructor is marked (because the type was marked and the static ctor, if present, needs to be run);
c) the IL scan of the .cctor marks every field being used (the stsfld);

In this case (GC) it's a good thing since the field is initialized to the return value of an internal call - and the linker cannot know if that call has side effects (even if the field is never used elsewhere).

There are other cases where the fields could have been:
* assigned values that would not have side effects (that removes calls, including object creation); and
* are not used outside the .cctor

To deal with them we would need to change how marking is done - not impossible but not easy either. If there's not many of them we could have special rules for the most common/costly .cctors.
Comment 4 PJ 2013-11-19 16:44:44 UTC
This bug was targeted for a past milestone, moving to the next non-hotfix active milestone.