[Mono-dev] DataRowCollection.Remove performance
Julian Hamilton
julian.hamilton at energyexemplar.com
Thu Aug 1 04:58:33 UTC 2013
Hi, I've never posted here before, so forgive me if I break etiquette.
However, I'm trying to submit a suggestion for a code change. While
developing our product which runs on both Windows and Linux, our Linux
users reported a significant difference in speed between the application
running on Windows under MS.NET versus Mono. I've tracked down the main
cause of the problem and I thought I'd share my change for possible
inclusion in a future release of Mono.
This is the current code for DataRowCollection.RemoveInternal...
internal void RemoveInternal (DataRow row)
{
if (row == null)
throw new IndexOutOfRangeException ("The given datarow is not
in the current DataRowCollection.");
int index = List.IndexOf (row);
if (index < 0)
throw new IndexOutOfRangeException ("The given datarow is not
in the current DataRowCollection.");
List.RemoveAt (index);
for (; index < List.Count; ++index)
((DataRow) List [index]).RowID = index;
}
Using List.IndexOf(row) is causing the poor performance. However,
DataRowCollection has its own IndexOf() method which is designed to be
fast and safe, taking advantage of the cached RowID member and testing
that the row is a member in the DataRowCollection. This should be used
here instead, resulting in the following code change...
internal void RemoveInternal (DataRow row)
{
if (row == null)
throw new IndexOutOfRangeException ("The given datarow is not
in the current DataRowCollection.");
- int index = List.IndexOf (row);
+ int index = this.IndexOf (row);
if (index < 0)
throw new IndexOutOfRangeException ("The given datarow is not
in the current DataRowCollection.");
List.RemoveAt (index);
for (; index < List.Count; ++index)
((DataRow) List [index]).RowID = index;
}
In my own testing this sped up row deletions by approximately 360%, but
you can ascertain a more accurate measure for yourself.
Thanks,
-Julian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/mono-devel-list/attachments/20130801/07644366/attachment.html>
More information about the Mono-devel-list
mailing list