[racket-dev] Speeding up `in-directory`

From: Sam Tobin-Hochstadt (samth at cs.indiana.edu)
Date: Wed Sep 4 15:44:41 EDT 2013

On Wed, Sep 4, 2013 at 3:23 PM, Matthew Flatt <mflatt at cs.utah.edu> wrote:
> At Wed, 4 Sep 2013 15:13:31 -0400, Sam Tobin-Hochstadt wrote:
>> On Wed, Sep 4, 2013 at 12:29 PM, Matthew Flatt <mflatt at cs.utah.edu> wrote:
>> >
>> >                                  (directory-list
>> >                                   (path->complete-path d init-dir)))])
>>
>>
>> I'm pretty sure this is wrong, but I'm not sure how to fix it.  In
>> particular, there's no reason that `init-dir` should have any relation
>> to any of the paths being generated, and so using it here is wrong.
>> You can break the code with
>>
>> (for ([i (in-directory6 d)])
>>   (current-directory "/")
>>   (displayln i))
>>
>> when run with a non-#f value of d.
>
> I think that if `d` is a relative path then it should be treated as
> relative to the current directory at the time that `in-directory6` is
> called:
>
>  * When I use "/home/mflatt/tmp" for `d`, I get a listing of files
>    under "/home/mflatt/tmp".
>
>  * When I use "tmp" and my initial directory is "/home/mflatt", I still
>    get a listing of files under "/home/mflatt/tmp".
>
> Both of those are as they should be, I think.
>
> When I use `in-directory` instead of `in-directory6`, then it behaves
> in a way that I think is less useful and should count as a bug in
> `in-directory`.

I was able to provoke the bad behavior as follows:

Create a racket program with `main` like this in ~/tmp/find.rkt:

(define d (if (= 0 (vector-length (current-command-line-arguments)))
              #f
              (vector-ref (current-command-line-arguments) 0)))

(for ([i (in-directory6 d)])
  (current-directory "/")
  (displayln i))

Then I did this:

% cd ~/tmp/foo
% racket ~/tmp/find.rkt ~/tmp/bar
.... correct output ....
% racket ~/tmp/find.rkt ../bar
.... much less output ...

Removing the `(current-directory "/")` line in the middle makes the
two calls produce identical output.

Sam

Posted on the dev mailing list.