Skip to content

Address SqlXml to CreateSqlReader dependency for assembly size #30912

Closed

Description

@stephentoub

System.Data.Common.dll contains the SqlXml type. This type uses reflection to access the internal CreateSqlReader method in System.Private.Xml.dll:
https://github.com/dotnet/corefx/blob/5bc2806f33090e78b38fafe4d5b46d5a0a4c1f08/src/System.Data.Common/src/System/Data/SQLTypes/SqlXml.cs#L134
To ensure that this internal method (which has no other consumers) isn't trimmed away, System.Private.Xml lists this method in its ILLinkTrim.xml file:
https://github.com/dotnet/corefx/blob/5bc2806f33090e78b38fafe4d5b46d5a0a4c1f08/src/System.Private.Xml/src/ILLinkTrim.xml#L3-L6
which means that even if SqlXml isn't used by an app, this method and everything it depends on can't be trimmed away. That ends up keeping alive ~300KB of code that's likely otherwise unused. We should address this somehow, e.g. https://github.com/dotnet/corefx/issues/41201 (though this is a case of the cross-assembly private reflection mentioned there).

Activity

  1. Wraith2 commented on Sep 20, 2019

    @Wraith2
    Contributor

    I think it might be a good idea to move the XmlSqlBinaryReader type and support out of the private xml implementation possibly up into SqlClient assuming it's the only consumer. That would slim the main framework and remove this reflection dependency I think.

  2. MichalStrehovsky commented on Sep 20, 2019

    @MichalStrehovsky
    Member

    Might be worth filing an issue on IL Linker: the way this works in .NET Native is that trivial uses of reflection (especially these lightup patterns) are guaranteed to be evaluated at compile time.

    We didn't need to mark things like this as roots in .NET Native because if the code that had the lightup was kept, so was the target of the lightup (thanks to the analysis).

    IL Linker can only do a small subset of this analysis, but I think it should already understand typeof(Foo).GetMethod("Bar"). Something might be wrong if the extra roots in ILLinkTrim.xml are needed.

  3. stephentoub commented on Sep 20, 2019

    @stephentoub
    MemberAuthor

    @MichalStrehovsky, part of the issue is that we do two passes with the linker: once as part of building the assembly (which is then published in the shared framework, the SDK, etc.) and once as part of publishing an app. We run it during the former to strip out things like internal/private consts, code in shared files where everything in the file isn't used by every assembly including that file, etc., in order to reduce the size of the shared framework. It's then run again during app publishing to do app-level trimming. In this case, CreateSqlReader is an internal method that's not referenced anywhere in System.Private.Xml.dll; it's only used by System.Data.Common.dll. So if it's not in the ILLinkTrim.xml file, the first linker pass that's only looking at the individual assembly trims it out.

  4. MichalStrehovsky commented on Sep 20, 2019

    @MichalStrehovsky
    Member

    Or is this because CoreFX build runs IL Linker on each assembly individually as part of the build when linker is allowed to trim everything that is not public and we need to prevent trimming at that stage?

  5. MichalStrehovsky commented on Sep 20, 2019

    @MichalStrehovsky
    Member

    Does IL Linker support multiple ILLinkTrim files? Maybe we could have two - one gets embedded in the assembly and gets used at both FX build and publish, and the other is only used as part of CoreFX build?

    (Don't get me wrong, I hate these reflection lightups targeting privates more than anyone, but I'm not optimistic that we will be able to get rid of all of them.)

  6. Wraith2 commented on Sep 20, 2019

    @Wraith2
    Contributor

    I'm not entirely convinced this one is even needed. SqlClient doesn't use this api It goes around it using the public api and if we can do that compatibly then I don't see why SqlXml needs private reflection or the binary xml reader in private xml either.

  7. stephentoub commented on Sep 20, 2019

    @stephentoub
    MemberAuthor

    If we can fix this by deleting stuff, that will make me happy.

  8. Wraith2 commented on Sep 26, 2019

    @Wraith2
    Contributor

    I've traced down what's going on and I don't see a good way to resolve it all. SqlXml will be passed a stream containing binary xml and the handler for that is internal inSystem.Private.Xml, XmlSqlBinaryReader. The reflection used in System.Data.Common is the way to create that type. Trying to lift XmlSqlBinaryReader out of the xml assembly will require copies of some of the xml internal support types (XmlValueConverter, XmlCharType among others) types a binary resources and localizable strings.

    I think the attribute approach of identifying the dependencies so the linker can follow them intelligently is the best way to resolve this.

  9. stephentoub commented on Dec 6, 2019

    @stephentoub
    MemberAuthor

    @Wraith2, thanks for investigating.

  10. transferred this issue fromdotnet/corefxon Feb 1, 2020
  11. added this to the 5.0 milestone on Feb 1, 2020
  12. added
    untriaged
    on Feb 23, 2020
  13. added
    linkable-framework
    and removed
    untriaged
    on Feb 25, 2020
  14. added a commit that references this issue on May 1, 2020
    c614097
  15. ghost locked as resolved and limited conversation to collaborators on Dec 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Xmllinkable-framework

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions