chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-users] Re: pathname-normalize


From: Reed Sheridan
Subject: [Chicken-users] Re: pathname-normalize
Date: Mon, 1 May 2006 11:26:20 -0500

> From: Kon Lovett <address@hidden>
Not to beat a dead horse but I thought people might be interested. I
timed the performance of 10000 runs of 'pathname-normalize' using the
2 impls below. (Ignore the reverse! & absolute-pathname for now.)

(define (pathname-normalize1 path)
        (let loop ([rparts (reverse! (string-split path "/"))] [oparts '()])
                (if (null? rparts)
                        (string-join (or (and (absolute-pathname? path) (cons 
"" oparts))
oparts) "/")
                        (let ([cur (car rparts)] [nxt (cdr rparts)])
                                (cond
                                        [(string=? "." cur)
                                                (loop nxt oparts)]
                                        [(and (not (null? nxt)) (string=? ".." 
cur))
                                                (loop (or (and (not (string=? 
".." (car nxt))) (cdr nxt)) nxt)
oparts)]
                                        [else
                                                (loop nxt oparts)])))))

(define (pathname-normalize2 path)
   (let loop ([rparts (reverse! (string-split path "/"))] [skip 0]
[acc '()])
     (match rparts
       (() (string-join (or (and (absolute-pathname? path) (cons ""
acc)) acc) "/"))
       (("." . more) (loop more skip acc))
       ((".." . more) (loop more (add1 skip) acc))
       ((path-part . more)
          (if (zero? skip)
           (loop more 0 (cons path-part acc))
           (loop more (sub1 skip) acc))))))

Test path: "asdf/../../asdf"

Unfortunately, this doesn't work for me:
#;12> (pathname-normalize2 "asdf/../../asdf")
""
Same with pathname-normalize1.

Also, if this is to be library code, string-join also has to go,
because it depends on srfi-13.  It's kind of awkward to write code
like this without srfi-13 or srfi-1, but we can put an extra loop in
there.


Interpreter:
<snip benchmarks>

A recursion is more expensive than a test, so this makes sense.

I'm not sure what you mean.  Both versions recur on loop.  But match
is definitely slower than explicitly destructuring the list yourself. If you macroexpand the match form, you can see that it creates extra
closures and uses equal? to test string equality.  It sacrifices speed
for clarity and conciseness.  I'm not sure how much that matters for
pathname-normalize, but speed never hurts.

Reed Sheridan




reply via email to

[Prev in Thread] Current Thread [Next in Thread]