Re: Holding Returned Data in Arrays

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Niki Estner (niki.estner_at_cube.net)
Date: 05/13/04


Date: Thu, 13 May 2004 17:28:49 +0200

According to the docs foreach uses the IEnumerable/IEnumerator interface
which does not provide random-access, so it will have to store the reference
you give it.
I'm not sure if this answer is general enough for you, it applies to all
foreach loops.
It it however generally safe to make a copy of a collection object: they are
usually implemented as reference types, so your sample line:
> fileInfoArray = dir.GetFiles()
will not copy the data in the collection, it will only create another
reference to it - so you may always do this if in doubt.

Niki

"Alric" <anonymous@discussions.microsoft.com> wrote in
news:B5E1DC96-D6B6-48BF-9591-0948B294ACC6@microsoft.com...
> I have tried to find an answer to this question, but I don't think I know
the correct search terms.
>
> When is it appropriate to cache the data returned from a function in a
holding variable? I am curious regarding what is really happening under the
.net hood.
>
> For example, let's look at looping through files in a directory.
>
> I could loop through the output of the GetFiles() function:
> >
> Dim loopFileInfo As FileInfo
> Dim dir As New DirectoryInfo("C:\Windows")
>
> For Each loopFileInfo In dir.GetFiles()
> Debug.WriteLine(loopFileInfo.FullName)
> Next
> >
>
> Or I could hold that output in an array and loop through that collection:
> >
> Dim loopFileInfo As FileInfo
> Dim fileInfoArray() As FileInfo
> Dim dir As New DirectoryInfo("C:\Windows")
>
> fileInfoArray = dir.GetFiles()
>
> For Each loopFileInfo In fileInfoArray
> Debug.WriteLine(loopFileInfo.FullName)
> Next
> >
>
> Will dir.GetFiles() be called more than once if I loop directly through
it? I always thought GetFiles() would be called once and then the output
would be stored in memory while the For Each loop processed, but some
reading lately has challenged this understanding.
>
> Obviously my question is not focused on this example. I'm trying to
understand what the general pattern is for the CLR. Is there generally a
performance benefit by caching collection return values before iterating
through them? Or are you just creating a second copy of the data for no
reason?
>
> In some basic testing of this GetFiles() example, no performance benefit
was seen by creating the holding collection. Also, my original
understanding -- that the function would only be called once -- has been
validated. If I starting iterating through GetFiles() and then add a few
files to the directory, the files are not magically added to the GetFiles()
output.



Relevant Pages

  • Re: StackOverflow Exception in JNI
    ... > I have a loop written in C/JNI which calls back a Java object (which ... temporary objects in your native method and then return a valid ... reference without thinking too much about these issues. ...
    (comp.lang.java.programmer)
  • Re: Looping-related Memory Leak
    ... memory leak / balloon for reasons I cannot figure out. ... end of the loop. ... reference count never reaches zero, and they remain alive until the ... the generational collector doesn't break cycles that involve ...
    (comp.lang.python)
  • Re: dynamic form validation / multiple forms
    ... for diagnosing the problems that produce errors on the browser. ... part is redundant as soon as you are passing the reference to the form ... of the form element but then using that name to look-up ... global variables a loop counters is particularly problematic as it ...
    (comp.lang.javascript)
  • [PATCH] async scsi scanning, version 13
    ... * Tells the midlayer this host is going to do an asynchronous scan. ... * This traverses over each devices of @shost. ... * out of the loop. ... * takes a reference on each device and releases it at the end. ...
    (Linux-Kernel)
  • Re: general performance question
    ... first Object is eligible for garbage collection. ... For a loop, the value set in the last iteration also leaks the ... How can it if the reference is created in the block? ... The method's stack frame isn't collected until the method exits; it has no notion of block scope. ...
    (comp.lang.java.programmer)