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

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


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.

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