Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 可以采取哪些措施来提高IHTMLDOMAttribute cast性能?_C#_.net_Performance_Com Interop_Mshtml - Fatal编程技术网

C# 可以采取哪些措施来提高IHTMLDOMAttribute cast性能?

C# 可以采取哪些措施来提高IHTMLDOMAttribute cast性能?,c#,.net,performance,com-interop,mshtml,C#,.net,Performance,Com Interop,Mshtml,我试图通过构建MSHTMLDOM并遍历它来清理/修改html树。 问题是,要花很长时间才能达到推特Twitter通讯的3-4秒或与之相当的时间。 在分析会话之后,我能够精确定位热点: private void AddAttributes(IHTMLDOMNode node) { string nodeName = node.nodeName; var attributes = (IHTMLAttributeCollection) node.attributes;

我试图通过构建MSHTMLDOM并遍历它来清理/修改html树。 问题是,要花很长时间才能达到推特Twitter通讯的3-4秒或与之相当的时间。

在分析会话之后,我能够精确定位热点:

private void AddAttributes(IHTMLDOMNode node)
{
        string nodeName = node.nodeName;
        var attributes = (IHTMLAttributeCollection) node.attributes;
        int length = attributes.length;
        for (int i = 0; i < length; i++)
        {
            //problem line
            IHTMLDOMAttribute attribute = attributes.item(i) as IHTMLDOMAttribute;

            string attributeName = attribute.nodeName;

            //do some work
            ...
        }
}
private void AddAttributes(IHTMLDOMNode)
{
字符串nodeName=node.nodeName;
var attributes=(IHTMLAttributeCollection)node.attributes;
int length=attributes.length;
for(int i=0;i
转换到IHTMLDOMAttribute需要75%的时间(相比之下,整个DOM创建只需要~3%)

AddAttributes的探查器输出: 职能机构:0.3% 调用函数: doclToComCall:41.5% JITutil_ChkCastAny:27.2% ?InterfaceMarshaler_转换器托管…:10%

在这种情况下,我可以做些什么来提高性能

我一直在这里:,看起来很相似,但我们一直在使用.NET3.5,所以动态性是毫无疑问的。在过去的几年中,还有其他一些关于类似问题的报告,但没有明确的答案,只有关于编组问题的提示


HTML Agility Pack虽然速度快得多,但无法解析CSS属性,这对我们来说至关重要。

尝试用JavaScript移动所有代码-如果由于COM/C互操作而导致速度变慢,您可能会获得更好的性能…我们需要从邮件解析HTML,整个项目都是基于C的。由于互操作,它预计会有某种性能损失,但这对于预期的行为来说太多了。显然,这是一个不明确的建议——尝试将处理更改/清理HTML/CSS的代码移到JavaScript上——将任何浏览器托管都保留在C#中。试试看你是否做得更好。好吧,我们做了一些接近它的事情。最终将问题类转移到C++帮助库,这基本上减少了编组到字符串的交换。不过,如果能找到一个替代答案,那就太好了。