-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Ignore comments and whitespace when parsing read-only XML files #6232
Conversation
I should have read the issues more carefully. The problem is in the difference between |
…ng back XmlTextReader behavior
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 have applied @KirillOsenkov's suggestions and am publishing the PR. Poking at internal members with reflection is admittedly ugly. However,
- Full Framework is pretty much frozen so the risk of being broken by changes in
System.Xml
is very small. - Core is tracking this as an API suggestion and if the win is confirmed in the perf lab, we should be able to ultimately switch to calling a proper public API.
@@ -26,6 +28,9 @@ internal static XmlReaderExtension Create(string filePath, bool loadAsReadOnly) | |||
private readonly Stream _stream; | |||
private readonly StreamReader _streamReader; | |||
|
|||
private static volatile PropertyInfo _normalizationPropertyInfo; |
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.
Whenever one uses volatile
one should add a comment explaining what is going on ;)
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 wonder if explicitly calling Volatile.Read
and Volatile.Write
would make it more obvious. Comment added!
None of my comments are a deal breaker, so fix if you like or not, use your judgement. |
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.
Feedback applied, thank you.
@@ -26,6 +28,9 @@ internal static XmlReaderExtension Create(string filePath, bool loadAsReadOnly) | |||
private readonly Stream _stream; | |||
private readonly StreamReader _streamReader; | |||
|
|||
private static volatile PropertyInfo _normalizationPropertyInfo; |
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 wonder if explicitly calling Volatile.Read
and Volatile.Write
would make it more obvious. Comment added!
if (propertyInfo == null) | ||
{ | ||
BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.SetProperty | BindingFlags.Instance; | ||
propertyInfo = xmlReaderType.GetProperty("Normalization", bindingFlags); |
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.
Nit: why do you need to introduce a propertyInfo local variable as opposed to just modifying _normalizationPropertyInfo as relevant?
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.
It's a volatile field so I want to access it only when I really need to to avoid unnecessary memory barriers.
if (_normalizationPropertyInfo == null)
{
..
}
return _normalizationPropertyInfo;
would read the field twice on the fast path. The cost is small, limited to still rather exotic platforms, and negligible in the grand scheme of things, but a good practice anyway.
(In retrospect, explicit Volatile.Read
and Volatile.Write
may have been a better choice for readability.)
When I run
I know that normally people do not run Debug builds, but I assume the assert was put there on purpose (it's been there for a long time): |
…es (dotnet#6232)" This reverts commit 1d1fec7.
Opened #6669 to revert this PR. |
Fixes #2576
Context
The code already has support for parsing project files as read-only when we know that we're not going to be asked to write them back to disk. This flag is currently unused because the initial implementation in #3584 introduced a regression related to whitespace in attribute values (#4210).
Changes Made
Trivially reverted part of #4213 that addressed the regression and added a hack to make
XmlReader
behave the same asXmlTextReader
.Testing
Existing unit tests for correctness and ETW for performance.
XmlDocumentWithLocation.Load()
is ~26% faster with this change compared to baseline when building .NET Core projects. This translates to about 10 ms per one incremental CLI build of a Hello world application.Notes