Martin Honnen wrote:[color=blue]
> Dima wrote:
>[color=green]
> > Call to XmlNode.GetElementsByTagName returns XmlNodeList that stays in
> > sync with XmlDocument thanks to events fired by XmlDocument. Once this
> > list is created there is no way to remove its event handlers from the
> > document. Calling GetElementsByTagName second time for the same tag
> > name will create new list and add more event handlers.[/color]
>
> If you already know that DOM collections returned by
> GetElementsByTagName are "live collections" kept in sync with the
> document, why do you then call the method with the same tag name again
> in your code? Can't you simply store the result of the first call and
> use that collection returned in the rest of your code?[/color]
Martin, thank you for your respose!
I use it because it is faster that using xpath. I fixed the problem by
switching to xpath. I could store the result, but I believe DOM
implementation has much better position to store this result: if
collection is live and once created it cannot be easily disposed, why
second call to GetElementsByTagName returns new collection?
[color=blue]
>[color=green]
> > As result over time these handlers accumulate and reach pretty high
> > number (millions).[/color]
>
> Have you run tests that show that even for collections gone out of scope
> (e.g. local variables created in a method and not returned by the
> method) those event handlers are still fired?[/color]
All collections I used in my code were local variables. I use C#, so
going out of scope will not free anything. I tried to set collection to
null and it predictably did not unregistered handlers. XmlNodeList is
not IDisposable. What else can I do? I probably could cast it to
XmlElementList (undocumented), get its OnListChanged handler
(undocumented) and unregister it, but so far I am trying to use only
documented features of .NET 1.1.
[color=blue]
>
> As long as your code keeps using a collection an implementation needs to
> keep it in sync.[/color]
I agree, but I use collection once and would like to dispose it, but I
do not see a way to do it.
[color=blue]
>[color=green]
> > To me it looks like a bug. Did I overlook somethning? Any feedback will
> > be appreciated.[/color]
>
> It is not clear whether you have a test case where you observe the
> performance loss or whether you are just speculating whether there might
> be a performance loss due to the need to keep collections in sync.
> Do you have code where you experience performance problems?[/color]
Yes, I profiled my app with ANTS profiler, and all AppendChild calls
are slow, because of call to XmlDocument.AfterEvent that invokes ~1.2
million handlers. Interestigly performance does not depend much on the
size of the document, but more on how many times GetElementsByTagName
was called.
I would not spend my time on speculation. I hope MS will fix it.
Again, I partially solved my problem by switching to XPath (which is
slower than GetElementsByTagName, according to ANTS profiler), but to
me behaviour of GetElementsByTagName seems simply dangerous. The only
functions that cannot be called twice are ctors and dtors.
GetElementsByTagName does not fit into this category, yet it behaives
that way.
[color=blue]
>
> --
>
> Martin Honnen --- MVP XML
>
http://JavaScript.FAQTs.com/[/color]