古老而又老套:MSXML和FreeThreadedDomDocument与DomDocument

I've noticed a lot of folks who still do COM development using MSXML (2, 3, or 4) and MSXML2.DOMDocument or MSXML2.FreeThreadedDOMDocument in wrongheaded ways. I wanted to make folks aware of a few tips and thoughts around these two components from experience and elsewhere around the net.

我注意到许多人仍然使用错误的方式使用MSXML(2、3或4)和MSXML2.DOMDocument或MSXML2.FreeThreadedDOMDocument进行COM开发。 我想让人们从经验和网络上的其他地方了解关于这两个组成部分的一些技巧和想法。

MSXML exposes DOMDocument and FreeThreadedDOMDocument. They are DIFFERENT and using one vs. the other (depending GREATLY on how) can make a 7x to 10x diference in performance. XML is very powerful, but remember that even though it's easy to use [Xml.Load("somexml.xml")] it's also easy to GREATLY slow your code down in VERY few lines of code.

MSXML公开DOMDocument和FreeThreadedDOMDocument。 它们是不同的,使用一个与另一个(取决于方式)可以使性能相差7到10倍。 XML非常强大,但是请记住,即使易于使用[Xml.Load(“ somexml.xml”)],也很容易在很少的几行代码中大大降低代码速度。

DOM文档 (DOMDocument)
这些对象使用所谓的“ Rental”线程模型。 这意味着可以从任何线程访问它们,但一次只能访问一个线程。 只要您不尝试在线程之间共享DOM对象,就可以使用这些对象。
DomDocument最佳做法 (Best Practices for DomDocument)
  • When using Single Threaded EXEs (VB6, etc) and manipulating Xml with in a single transaction, you're working on a single thread -> Use DomDocument.

    当使用单线程EXE(VB6等)并在单个事务中处理Xml时,您正在使用单个线程->使用DomDocument。
  • When using Classic ASP and manipulating Xml within a single page request, you're working on a single thread -> Use DomDocument

    当使用Classic ASP并在单个页面请求中处理Xml时,您正在使用单个线程->使用DomDocument
FreeThreadedDOMDocument (FreeThreadedDOMDocument )

The "free-threaded" DOM document exposes the same interface as the "rental" threaded document. This object can be safely shared across any thread in the same process. Free-threaded documents are generally slower than rental documents because of the extra thread safety work they do. You use them when you want to share a document among multiple threads at the same time, avoiding the need for each of those threads to load it's own copy.

“自由线程” DOM文档公开与“租赁”线程文档相同的接口。 可以在同一进程中的任何线程之间安全地共享此对象。 自由线程文档通常比租用文档慢,因为它们会额外进行线程安全工作。 当您想同时在多个线程之间共享文档时,可以使用它们,而无需每个线程都加载其自己的副本。

If you do need to share objects between threads, you have two choices:

如果确实需要在线程之间共享对象,则有两种选择:

1. use "FreeThreadedDOMDocument", which exposes all the same interfaces as DOMDocument, but is multi-thread safe (with a corresponding performance hit due to internal locking and synchronization). It can be safely stored in ASP Application state on IIS.

1.使用“ FreeThreadedDOMDocument”,它公开了与DOMDocument相同的所有接口,但具有多线程安全性(由于内部锁定和同步而导致相应的性能下降)。 它可以安全地存储在IIS上的ASP应用程序状态中。

For C++ people:

对于C ++用户:

2. Change your threads to use single-threaded apartments(COINIT_APARTMENTTHREADED) and then marshall interface pointers between your threads (See CoMarshalInterThreadInterfaceInStream).

2.更改线程以使用单线程单元(COINIT_APARTMENTTHREADED),然后在线程之间封送接口指针(请参阅CoMarshalInterThreadInterfaceInStream)。

FreeThreadedDomDocument最佳做法 (Best Practices for FreeThreadedDomDocument)
  • When using Classic ASP and storing Xml in the Application Object or Session Object (which is a questionable practice, can affect performance, and is not recommend for the inexperienced) -> Use FreeThreadedDomDocument

    当使用经典ASP并将Xml存储在应用程序对象或会话对象中时(这是一个有问题的做法,可能会影响性能,对于没有经验的人不建议这样做)->使用FreeThreadedDomDocument
  • There is not any good reason that I can think of to use FreeThreadedDomDocument in a Single Threaded Exe (unless you're marshalling it off somewhere)

    我没有想到可以在单线程Exe中使用FreeThreadedDomDocument的任何充分理由(除非您要在某个地方将其编组)

Note: The fastest way to load an XML Document (assuming you're loading it into a DOM) The fastest way to load an XML document is to use the default "rental" threading model (which means the DOM document can be used by only one thread at a time) with validateOnParse, resolveExternals, and preserveWhiteSpace all disabled, like this in (for example…note it could happily be in VBScript) Javascript:

注意:加载XML文档的最快方法(假设将其加载到DOM中)加载XML文档的最快方法是使用默认的“租赁”线程模型(这意味着DOM文档只能由一次禁用validateOnParse,resolveExternals和preserveWhiteSpace的一个线程),例如(例如?注意,它可能很高兴在VBScript中)Javascript:

var doc = new ActiveXObject("MSXML2.DOMDocument"); doc.validateOnParse = false;
doc.resolveExternals = false;
doc.preserveWhiteSpace = false;
doc.load("somexml.xml");

var doc = new ActiveXObject(“ MSXML2.DOMDocument”); doc.validateOnParse = false; doc.resolveExternals = false; doc.preserveWhiteSpace = false; doc.load(“ somexml.xml”);

If you have an element-heavy XML document that contains a lot of white space between elements and stored in Unicode, it can actually be smaller in memory than on disk. Files that have a more balanced ratio of elements to text content end up at about 1.25 to 1.5 the UCS-2 disk file size when in memory. Files that are very data-dense, such as an attribute - heavy XML - persisted ADO recordset, can end up more than twice the disk-file size when loaded into memory. NOTE: These tips are for the COM MSXML Components and do NOT represent best practices in .NET. .NET has much richer and highly nuanced XML support. Also note that loading XML into a DOM is fairly slow anywhere since you're loading XML in to a pre-parsed and indexed tree. An example of an inefficient operation would be to load a 3000k XML file into a DOM, then perform a SelectNodes("//somenode") in order to retrieve a single value. [Listening to: The Verve Pipe - The Freshman (Special Version)]

如果您有大量元素的XML文档,其中包含大量元素之间的空白并以Unicode格式存储,则实际上它在内存中的大小可能比在磁盘上的小。 内存中元素与文本内容比率更为均衡的文件最终的大小约为UCS-2磁盘文件大小的1.25至1.5。 数据密集型文件(例如属性-重XML-持久化的ADO记录集)在加载到内存中时,其最终磁盘大小可能是磁盘文件大小的两倍。 注意:这些技巧是针对COM MSXML组件的,并不代表.NET中的最佳实践。 .NET具有更丰富,更细微的XML支持。 还要注意,由于将XML加载到预先分析并建立索引的树中,因此将XML加载到DOM中的速度相当慢。 低效操作的一个示例是将3000k XML文件加载到DOM中,然后执行SelectNodes(“ // somenode”)以便检索单个值。 [听:神韵管子-大一新生(特别版)]

翻译自: https://www.hanselman.com/blog/an-oldie-but-a-goodie-msxml-and-freethreadeddomdocument-vs-domdocument

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值