unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* [patch] 21.3 executable.find - Use cache in `executable-find'
@ 2004-02-08 11:37 Jari Aalto+mail.linux
  2004-03-24 11:13 ` Eli Zaretskii
  0 siblings, 1 reply; 5+ messages in thread
From: Jari Aalto+mail.linux @ 2004-02-08 11:37 UTC (permalink / raw)



It is common that searches for programs are done multiple times, so it
would be good if the precious values were cached just like bash does.
Here is patch to make this happen.

This patch supposes that my other patches have been applied
(See patch to implement `executable-command-find-unix-p')


2004-02-08 Sun  Jari Aalto  <jari.aalto <AT> poboxes.com>

        * progmodes/executable.el (executable-find-cache): New variable.
        (executable-find-sans-suffix): New function.
        (executable-find-path): Renamed. Was `executable-find'.
        (executable-find-cache): New function.
        (executable-find): Rewritten. Now utilizes cache
        `executable-find-cache'. Added new OPTIONAL parameter `no-cache'
        to force searching COMMAND again.


Index: executable.el
===================================================================
RCS file: /cygdrive/h/data/version-control/cvsroot/emacs/gnu-emacs/lisp213/progmodes/executable.el,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -IId: -u -b -w -r1.5 -r1.6
--- executable.el	3 Feb 2004 16:34:18 -0000	1.5
+++ executable.el	8 Feb 2004 11:30:11 -0000	1.6
@@ -107,6 +107,10 @@
 
 (defvar executable-command nil)
 
+(defvar executable-find-cache nil
+  "*`executable-find' cache.
+Syntax: '((program path) ...).")
+
 (defcustom executable-self-display "tail"
   "*Command you use with argument `+2' to make text files self-display.
 Note that the like of `more' doesn't work too well under Emacs \\[shell]."
@@ -205,10 +209,21 @@
          ((search-forward "find: bad option -maxdepth" nil t)
           t)))))
 
+(defun executable-find-sans-suffix (command)
+  "Return COMMAND without `executable-binary-suffixes'."
+  (let ((ext (file-name-extension command)))
+    (if (and ext
+             (member (concat "." ext) executable-binary-suffixes))
+        (file-name-sans-extension command)
+      command)))
+
+(defsubst executable-find-cache (command)
+  "Look up COMMAND from `executable-find-cache'."
+  (assoc command executable-find-cache))
+
 ;;;###autoload
-(defun executable-find (command)
-  "Search for COMMAND in exec-path and return the absolute file name.
-Return nil if COMMAND is not found anywhere in `exec-path'."
+(defun executable-find-path (command)
+  "Search for COMMAND in exec-path and return the absolute file name."
   (let ((list exec-path)
         file)
     (while list
@@ -229,6 +244,29 @@
               (cdr list))))
     file))
 
+;;;###autoload
+(defun executable-find (command &optional no-cache)
+  "Search for COMMAND in exec-path and return the absolute file name.
+Return nil if COMMAND is not found anywhere in `exec-path'.
+Optional parameter NO-CACHE says to search all PATH hierarchy
+without looking the program location from `executable-find-cache'."
+  (let (cache
+        path)
+    (unless no-cache
+      (setq path (executable-find-cache
+                  (executable-find-sans-suffix command))))
+    (unless path
+      ;;  Was not in cache, or user requested full PATH search
+      (when (setq path (executable-find-path command))
+        ;;  Record this to cache
+        (setq cache
+              (list (executable-find-sans-suffix
+                     (file-name-nondirectory command))
+                    path))))
+    (when cache
+      (push cache executable-find-cache))
+    path))
+
 (defun executable-chmod ()
   "This gets called after saving a file to assure that it be executable.
 You can set the absolute or relative mode in variable `executable-chmod' for
@@ -241,7 +279,6 @@
                                (- executable-chmod)
                              (logior executable-chmod
                                      (file-modes buffer-file-name)))))))
-
 
 (defun executable-interpret (command)
   "Run script with user-specified args, and collect output in a buffer.

-- 
http://tiny-tools.sourceforge.net/
Swatch @time   http://www.mir.com.my/iTime/itime.htm
               http://www.ryanthiessen.com/swatch/resources.htm
Use Licenses!  http://www.linuxjournal.com/article.php?sid=6225
Which Licence? http://www.linuxjournal.com/article.php?sid=4825
OSI Licences   http://www.opensource.org/licenses/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [patch] 21.3 executable.find - Use cache in `executable-find'
       [not found] <mailman.2083.1076239601.928.bug-gnu-emacs@gnu.org>
@ 2004-02-10 21:21 ` Jari Aalto+mail.linux
  0 siblings, 0 replies; 5+ messages in thread
From: Jari Aalto+mail.linux @ 2004-02-10 21:21 UTC (permalink / raw)


* Sun 2004-02-08 jari.aalto <AT> poboxes.com (Jari Aalto+mail.linux) gnu.emacs.bug
* <http://groups.google.com/groups?oi=djq&as_umsgid=%3Cmailman.2083.1076239601.928.bug-gnu-emacs@gnu.org>
| It is common that searches for programs are done multiple times, so it
| would be good if the precious values were cached just like bash does.
| Here is patch to make this happen.
| 
| This patch supposes that my other patches have been applied
| (See patch to implement `executable-command-find-unix-p')
| 

One small fix needed to get it working.

2004-02-10 Tue  Jari Aalto  <jari.aalto <AT> poboxes.com>

        * progmodes/executable.el (executable-find-cache): Returned
        (command path), when should have returned path. fixed.



Index: executable.el
===================================================================
RCS file: /cygdrive/h/data/version-control/cvsroot/emacs/gnu-emacs/lisp213/progmodes/executable.el,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -IId: -u -r1.6 -r1.7
--- executable.el	8 Feb 2004 11:30:11 -0000	1.6
+++ executable.el	10 Feb 2004 21:19:06 -0000	1.7
@@ -219,7 +219,9 @@
 
 (defsubst executable-find-cache (command)
   "Look up COMMAND from `executable-find-cache'."
-  (assoc command executable-find-cache))
+  (let ((elt (assoc command executable-find-cache)))
+    (when elt
+      (nth 1 elt))))
 
 ;;;###autoload
 (defun executable-find-path (command)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [patch] 21.3 executable.find - Use cache in `executable-find'
  2004-02-08 11:37 [patch] 21.3 executable.find - Use cache in `executable-find' Jari Aalto+mail.linux
@ 2004-03-24 11:13 ` Eli Zaretskii
       [not found]   ` <y8pp51f9.fsf@blue.sea.net>
  0 siblings, 1 reply; 5+ messages in thread
From: Eli Zaretskii @ 2004-03-24 11:13 UTC (permalink / raw)
  Cc: bug-gnu-emacs

> From: jari.aalto@poboxes.com (Jari Aalto+mail.linux)
> Date: Sun, 08 Feb 2004 13:37:16 +0200
> 
> It is common that searches for programs are done multiple times, so it
> would be good if the precious values were cached just like bash does.

Any measurements to show that this indeed yields significant savings
in Emacs?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [patch] 21.3 executable.find - Use cache in `executable-find'
       [not found]   ` <y8pp51f9.fsf@blue.sea.net>
@ 2004-03-25  6:49     ` Eli Zaretskii
  2004-03-25  9:14       ` Jari Aalto+mail.linux
  0 siblings, 1 reply; 5+ messages in thread
From: Eli Zaretskii @ 2004-03-25  6:49 UTC (permalink / raw)
  Cc: bug-gnu-emacs

> From: jari.aalto@poboxes.com (Jari Aalto+mail.linux)
> Date: Thu, 25 Mar 2004 00:24:10 +0200
> 
> | Any measurements to show that this indeed yields significant savings
> | in Emacs?
> 
> No, but just common sense.

My common sense says ``profile before you optimize''.

> I have 50 directories in path and depending
> just where the /bin might be it could take ages to find same programs
> all again.

I'd like to know how long is ``ages''.  With today's caching OSes,
you'd be surprised how fast that could be.

I could be wrong, of course, but only measurements will prove or
disprove that.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [patch] 21.3 executable.find - Use cache in `executable-find'
  2004-03-25  6:49     ` Eli Zaretskii
@ 2004-03-25  9:14       ` Jari Aalto+mail.linux
  0 siblings, 0 replies; 5+ messages in thread
From: Jari Aalto+mail.linux @ 2004-03-25  9:14 UTC (permalink / raw)
  Cc: bug-gnu-emacs

* 2004-03-25 Eli Zaretskii <eliz <AT> elta.co.il> mail.default.spool
| > From: jari.aalto <AT> poboxes.com (Jari Aalto+mail.linux)
| > Date: Thu, 25 Mar 2004 00:24:10 +0200
| > 
| > | Any measurements to show that this indeed yields significant savings
| > | in Emacs?
| > 
| > No, but just common sense.
| 
| My common sense says ``profile before you optimize''.

But the results in this time are obvious. Cached path value is always
faster than doing directory searches. Disk I/O is always slower than
memory based search. Depending on how busy disk is, things are even
worse.

| > I have 50 directories in path and depending
| > just where the /bin might be it could take ages to find same programs
| > all again.
| 
| I'd like to know how long is ``ages''.  With today's caching OSes,
| you'd be surprised how fast that could be.
| 
| I could be wrong, of course, but only measurements will prove or
| disprove that.

See results above. I used slightly different Emacs version, because
Cygwin is the "non patched" and Native NT Emacs is "patched" and
I don't have an easy way to make NTEmacs "unpatched". 

It suprised, that NTEmacs were even slower - I though it would have
been the other way round.

Every time program is called the "search time" is added. Now if the
program is called in a loop, that will even more make it slower (those
cases are rare I believe).

But anyway, it does improve things.
Jari

Emacs 21.3 / Win32 native, caching active

time: 90000 microseconds                << First search, without cache
time: 0 microseconds                    << now cached.
time: 0 microseconds
time: 0 microseconds
time: 0 microseconds
time: 0 microseconds
time: 0 microseconds
time: 0 microseconds
time: 0 microseconds
time: 0 microseconds

Emacs 21.3 / Win32 Cygwin version, no caching
 
time: 13000 microseconds
time: 14000 microseconds
time: 13000 microseconds
time: 14000 microseconds
time: 13000 microseconds
time: 36000 microseconds
time: 14000 microseconds
time: 14000 microseconds
time: 14000 microseconds
time: 15000 microseconds


Test case:

(let ((exec-path exec-path)
      a
      b 
      list)
  (dotimes (x 40)
    (push "/this/dummy" list))
  (append list exec-path)
  (dotimes (x 10)
    (setq a (current-time))
    (executable-find "ls")
    (setq b (current-time))
    (sit-for 0.3)
    (insert
     (format "\ntime: %s microseconds"
             (- (caddr b) (caddr a))))))


-- 
http://tiny-tools.sourceforge.net/
Swatch @time   http://www.mir.com.my/iTime/itime.htm
               http://www.ryanthiessen.com/swatch/resources.htm
Use Licenses!  http://www.linuxjournal.com/article.php?sid=6225
Which Licence? http://www.linuxjournal.com/article.php?sid=4825
OSI Licences   http://www.opensource.org/licenses/

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2004-03-25  9:14 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-02-08 11:37 [patch] 21.3 executable.find - Use cache in `executable-find' Jari Aalto+mail.linux
2004-03-24 11:13 ` Eli Zaretskii
     [not found]   ` <y8pp51f9.fsf@blue.sea.net>
2004-03-25  6:49     ` Eli Zaretskii
2004-03-25  9:14       ` Jari Aalto+mail.linux
     [not found] <mailman.2083.1076239601.928.bug-gnu-emacs@gnu.org>
2004-02-10 21:21 ` Jari Aalto+mail.linux

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).