I asked a separate question (now deleted) for what I thought the actual problem was.
Could the .NET Jitter not actually execute this loop:
Private Shared Sub CheckXmlValidity(ByVal textReader As System.IO.StreamReader)
Try
' Check for "interesting" xml documents.
Dim settings = New System.Xml.XmlReaderSettings()
settings.XmlResolver = Nothing
settings.MaxCharactersInDocument = 655360
' Successfully parse the file, otherwise an XmlException is to be thrown.
Dim reader = System.Xml.XmlReader.Create(textReader, settings)
Try
While reader.Read()
'Just checking.
End While
Finally
reader.Close()
End Try
Catch ex As Exception
Throw New HttpException(500, "Invalid Xml data", ex)
End Try
End Sub
I would hope not exactly because of the side-effect of the exceptions that could be thrown, but I’m just checking…
The JITer, and really any optimizer, can only remove items which have no effect on the execution of the problem. Proving a method has no effect is very hard to do in .Net. Particularly because every method call can have an effect if the source object is
null(it would throw an exception). Hence even methods are known to be a no-op can’t be fully removed without inserting anullcheck of sorts (1)In this case
XmlReader.Readis anabstractmethod on an unsealed type which is even harder. The JITer could only remove this call if it knew that every implementation ofXmlReader.Readwas a no-op and hence had no effect. It can’t ever know this though because the number of derivations ofXmlReaderis not a fixed set. A new DLL could be loaded at any time with a new derivation ofXmlReaderwhich had a meaningful definition and hence couldn’t be optimized away.(1) Note: I’m not saying the JITer does this, just that if it removed the method it would need to do some form of
nullchecking.