-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add switch: Do not resolve URLs by defaults for XML #84169
Conversation
Note regarding the This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change. |
Tagging subscribers to this area: @dotnet/area-system-xml Issue DetailsFixes: #83495 This:
Creating PR with initial version since I wanted to collect feedback on this approach TODOs:
Size impact:
MStat numbers:
|
I got intrigued by the I guess if the actual API to enable the resolvers is |
src/libraries/System.Private.Xml/src/System/Xml/AppContextSwitchHelper.cs
Outdated
Show resolved
Hide resolved
|
||
namespace System.Xml | ||
{ | ||
public abstract partial class XmlResolver |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this really need to be in a new file? Why can't it just be in the main XmlResolver.cs
file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I followed convention for the ThrowingResolver
{ | ||
if (absoluteUri.Scheme == "file") | ||
{ | ||
return await Task.Run<Stream>(() => new FileStream(absoluteUri.LocalPath, FileMode.Open, FileAccess.Read, FileShare.Read, 1, useAsync: true)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is interesting... Does FileStream's ctor really require a separate thread to run?
Why wouldn't this just be return Task.FromResult(new FileStream(...));
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I extracted this logic from XmlDownloadManager.GetStreamAsync, I think we should be able to change this without issues in both places though since I don't think ctors can be async (unless there is some magic behind the back to make that work)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stephentoub - is there anything I'm missing here? I see you modified the XmlDownloadManager code in dotnet/corefx#41111, but the Task.Run<Stream>(...)
code was already there. Is there a reason to create the FileStream on a separate thread?
cc @adamsitnik
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason to create the FileStream on a separate thread?
Maybe someone was concerned that in these scenarios the file would be living on a remote file share that could take a non-trivial amount of time to open?
src/libraries/System.Private.Xml/src/System/Xml/XmlResolver.FileSystemResolver.cs
Outdated
Show resolved
Hide resolved
It's true but there are 2 levels of default resolver:
also for this particular scenario when we use default resolver we might still want to opt-out of URLs which this switch enables. |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get | ||
{ | ||
return GetCachedSwitchValue("Switch.System.Xml.IgnoreEmptyKeySequencess", ref s_ignoreEmptyKeySequences); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NOTE: there used to be a typo in the switch name here, I have fixed it here according to how it's documented:
https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/runtime/appcontextswitchoverrides-element
ReferenceOutputAssembly="false" | ||
SetTargetFramework="TargetFramework=netstandard2.0" | ||
OutputItemType="Analyzer" /> | ||
<ProjectReference Include="$(LibrariesProjectRoot)System.Text.RegularExpressions\gen\System.Text.RegularExpressions.Generator.csproj" ReferenceOutputAssembly="false" SetTargetFramework="TargetFramework=netstandard2.0" OutputItemType="Analyzer" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tried reverting this but VS keeps on changing this back
{ | ||
return XmlDownloadManager.GetStream(absoluteUri, _credentials, _proxy); | ||
} | ||
|
||
throw new XmlException(SR.Xml_UnsupportedClass, string.Empty); | ||
} | ||
|
||
// Maps a URI to an Object containing the actual resource. | ||
public override async Task<object> GetEntityAsync(Uri absoluteUri, string? role, Type? ofObjectToReturn) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved this without changes, for some reason we had separate file for async version (same for XmlDownloadManager)
{ | ||
if (absoluteUri.Scheme == "file") | ||
{ | ||
return Task.FromResult<object>(new FileStream(absoluteUri.LocalPath, FileMode.Open, FileAccess.Read, FileShare.Read, 1, useAsync: true)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: this method is moved from *Async
file which got removed. The only difference here is that we use Task.FromResult
rather than Task.Run
src/libraries/System.Private.Xml/src/System/Xml/XmlResolver.FileSystemResolver.cs
Show resolved
Hide resolved
src/libraries/System.Private.Xml/src/System/Xml/Core/LocalAppContextSwitches.cs
Show resolved
Hide resolved
<linker> | ||
<assembly fullname="System.Private.Xml"> | ||
<type fullname="System.Xml.LocalAppContextSwitches"> | ||
<method signature="System.Boolean get_AllowResolvingUrlsByDefault()" body="stub" value="false" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File name URLs are still valid URLs.
Should the name rather be AllowResolvingNonFileSystemUrlsByDefault
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
API review decided on System.Xml.XmlResolver.IsNetworkingEnabledByDefault
Draft Pull Request was automatically closed for 30 days of inactivity. Please let us know if you'd like to reopen it. |
Draft Pull Request was automatically closed for 30 days of inactivity. Please let us know if you'd like to reopen it. |
063d2a7
to
5c49b26
Compare
src/libraries/System.Private.Xml/src/System/Xml/Core/LocalAppContextSwitches.cs
Outdated
Show resolved
Hide resolved
...ystem.Private.Xml/tests/TrimmingTests/XmlUrlResolverDefaults.IsNetworkingEnabledByDefault.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: David Cantú <dacantu@microsoft.com>
…olverDefaults.IsNetworkingEnabledByDefault.cs Co-authored-by: David Cantú <dacantu@microsoft.com>
merging since the failures are unrelated to my changes |
Real world example from these savings: #89489. The size of ilc.exe shrinking by almost 30% from 15.2 MB to 11.1 MB! Awesome savings! (It also speeds up the inner loop of rebuilding the AOT compiler.) |
Fixes: #83495
The approach is adding new resolver which resolves URIs with file scheme (accessible with XmlResolver.FileSystemResolver) and switch which changes so that we use it rather than XmlUrlResolver. In most situation we never do any resolving for XMLs but XmlUrlResolver is still used to resolve URIs for root level APIs, the switch changes so that we use file resolver instead and never access the network.
This:
System.Xml.XmlResolver.IsNetworkingEnabledByDefault
) to use itCreating PR with initial version since I wanted to collect feedback on this approach
Open questions:
TODOs:
Size impact:
MStat numbers:
App for measurement (with switch set to false):
file.xml: