Reasons for Why Array.Copy would be Slow on Reference Type

  • Thread starter Thread starter jehugaleahsa
  • Start date Start date
J

jehugaleahsa

Hello:

I have an internal class of type Node which inherits from NodeBase.
The only difference is that NodeBase has pointers to other NodeBases
and Node has a value associated with it.

Each NodeBase has an array of NodeBases. Occasionally, I want to make
this array grow in size. However, whenever I call
Array.Resize<NodeBase> or Array.Copy there is a huge delay.


I did some profiling and I am really confused. The profiler says that
the majority of time being spent is on an Array.Copy. I have tried
pulling the classes out and testing outside, but I don't experience
the bottleneck. Is there some sort of memory management voodoo going
on causing this delay?

Thanks for any insight or workarounds.

~Travis
 
Travis,

It's possible, but I imagine they would have to be very big arrays.

How big are the arrays that you are copying? Also, why not use a
List<NodeBase>? It might give you the performance characteristics you are
looking for (or maybe not, you have to find out for yourself).
 
Travis,

    It's possible, but I imagine they would have to be very big arrays..

    How big are the arrays that you are copying?  Also, why not use a
List<NodeBase>?  It might give you the performance characteristics you are
looking for (or maybe not, you have to find out for yourself).

--
          - Nicholas Paldino [.NET/C# MVP]
          - (e-mail address removed)




I have an internal class of type Node which inherits from NodeBase.
The only difference is that NodeBase has pointers to other NodeBases
and Node has a value associated with it.
Each NodeBase has an array of NodeBases. Occasionally, I want to make
this array grow in size. However, whenever I call
Array.Resize<NodeBase> or Array.Copy there is a huge delay.
I did some profiling and I am really confused. The profiler says that
the majority of time being spent is on an Array.Copy. I have tried
pulling the classes out and testing outside, but I don't experience
the bottleneck. Is there some sort of memory management voodoo going
on causing this delay?
Thanks for any insight or workarounds.
~Travis- Hide quoted text -

- Show quoted text -

Actually, I originally had a List<NodeBase>, except it was using a
custom class of my own which is similar to List<T> except it allows
for resizing. I had attempted to replace my List<T> class with an
array, but they both internally use Array.Copy. I avoided the problem
altogether by simply adding to my array, rather than resizing it. I
believe this works the majority of the time because List<T> doubles in
capacity, reducing the number of Array.Copy needed.

For now I have found a solution. At least now I can confidently say it
was because of Array.Copy. What I can't say is why it was being called
that often.
 
I don't see why you would not use a List<T>. The concern about the
doubling of the capacity is valid, but the Capacity property is not
read-only (you can set it), so you can pre-allocate the capacity to what you
see fit, and it should suit your purposes just fine.

As for it being the call to Array.Copy, that's supposed to be a pretty
quick call. Again, how many elements are we talking about?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Travis,

It's possible, but I imagine they would have to be very big arrays.

How big are the arrays that you are copying? Also, why not use a
List<NodeBase>? It might give you the performance characteristics you are
looking for (or maybe not, you have to find out for yourself).

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




I have an internal class of type Node which inherits from NodeBase.
The only difference is that NodeBase has pointers to other NodeBases
and Node has a value associated with it.
Each NodeBase has an array of NodeBases. Occasionally, I want to make
this array grow in size. However, whenever I call
Array.Resize<NodeBase> or Array.Copy there is a huge delay.
I did some profiling and I am really confused. The profiler says that
the majority of time being spent is on an Array.Copy. I have tried
pulling the classes out and testing outside, but I don't experience
the bottleneck. Is there some sort of memory management voodoo going
on causing this delay?
Thanks for any insight or workarounds.
~Travis- Hide quoted text -

- Show quoted text -

Actually, I originally had a List<NodeBase>, except it was using a
custom class of my own which is similar to List<T> except it allows
for resizing. I had attempted to replace my List<T> class with an
array, but they both internally use Array.Copy. I avoided the problem
altogether by simply adding to my array, rather than resizing it. I
believe this works the majority of the time because List<T> doubles in
capacity, reducing the number of Array.Copy needed.

For now I have found a solution. At least now I can confidently say it
was because of Array.Copy. What I can't say is why it was being called
that often.
 
    As for it being the call to Array.Copy, that's supposed to be a pretty
quick call.  Again, how many elements are we talking about?

We are talking about 15-100, typically. It grows as it goes.

I am not using List<T> because that would make life too easy. :-)

I set the initial capacity to 15, actually.

The problem with the resize was that it always set the capacity to
exactly the specified size. With add, I some times get more, which
reduces Array.Copys.

If I had used List<T>, I would have just added to begin with since you
can't resize.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top