emacs-devel
[Top][All Lists]
Advanced

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

locate-dominating-file calls `stat' too eagerly


From: Eli Zaretskii
Subject: locate-dominating-file calls `stat' too eagerly
Date: Mon, 29 Sep 2008 14:54:50 +0300

This fragment from locate-dominating-file:

      (while (and dir
                  ;; As a heuristic, we stop looking up the hierarchy of
                  ;; directories as soon as we find a directory belonging to
                  ;; another user.  This should save us from looking in
                  ;; things like /net and /afs.  This assumes that all the
                  ;; files inside a project belong to the same user.
                  (let ((prev-user user))
                    (setq user (nth 2 (file-attributes dir)))
                    (or (null prev-user) (equal user prev-user))))
        (if (setq files (and (file-directory-p dir)
                             (directory-files dir 'full regexp)))
            (throw 'found (car files))
          (if (equal dir
                     (setq dir (file-name-directory
                                (directory-file-name dir))))
              (setq dir nil))))

repeatedly calls file-directory-p, even after file-directory-p already
returned non-nil, which means that thereafter anything that
file-name-directory returns will also necessarily be a directory.
That looks like inefficiency, doesn't it?  Here's the modification I
propose:

    (let ((user nil)
          ;; Abbreviate, so as to stop when we cross ~/.
          (dir (abbreviate-file-name (file-name-as-directory file)))
          files dir-seen)
      (while (and dir
                  ;; As a heuristic, we stop looking up the hierarchy of
                  ;; directories as soon as we find a directory belonging to
                  ;; another user.  This should save us from looking in
                  ;; things like /net and /afs.  This assumes that all the
                  ;; files inside a project belong to the same user.
                  (let ((prev-user user))
                    (setq user (nth 2 (file-attributes dir)))
                    (or (null prev-user) (equal user prev-user))))
        (if (setq files (and (or dir-seen 
                                 (setq dir-seen (file-directory-p dir)))
                             (directory-files dir 'full regexp)))
            (throw 'found (car files))
          (if (equal dir
                     (setq dir (file-name-directory
                                (directory-file-name dir))))
              (setq dir nil))))

Does anyone see any problems with that, on any platform?




reply via email to

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