changeset 22034:8df31c24dce3

Optimize do_make_absolute for most common calling usage. * oct-env.cc: Put in shortcut path to detect when path is '.' which is called every time that Octave returns to prompt. Rename variable tmp to sep_pos for clarity.
author Rik <rik@octave.org>
date Mon, 04 Jul 2016 07:39:02 -0700
parents 98ee8b1ebbeb
children 634fbedbfb5b
files liboctave/system/oct-env.cc
diffstat 1 files changed, 14 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/system/oct-env.cc	Sun Jul 03 09:21:31 2016 -0700
+++ b/liboctave/system/oct-env.cc	Mon Jul 04 07:39:02 2016 -0700
@@ -386,15 +386,16 @@
       if (dot_path.empty () || s.empty () || do_absolute_pathname (s))
         return s;
 
+      // Optimization: every time Octave returns to the prompt it calls
+      // make_absolute_filename with '.' as argument.
+      if (s == ".")
+        return dot_path;
+
       std::string current_dir = dot_path;
 
-      size_t pos = current_dir.length () - 1;
-
-      if (! octave::sys::file_ops::is_dir_sep (current_dir[pos]))
+      if (! octave::sys::file_ops::is_dir_sep (current_dir.back ()))
         current_dir.append (octave::sys::file_ops::dir_sep_str ());
 
-      // FIXME: this is probably not correct for all systems.
-
       size_t i = 0;
       size_t slen = s.length ();
 
@@ -403,7 +404,7 @@
           if (s[i] == '.')
             {
               if (i + 1 == slen)
-                return current_dir;
+                break;
 
               if (octave::sys::file_ops::is_dir_sep (s[i+1]))
                 {
@@ -425,23 +426,23 @@
                 }
             }
 
-          size_t tmp;
-          tmp = s.find_first_of (octave::sys::file_ops::dir_sep_chars (), i);
+          size_t sep_pos;
+          sep_pos = s.find_first_of (octave::sys::file_ops::dir_sep_chars (), i);
 
-          if (tmp == std::string::npos)
+          if (sep_pos == std::string::npos)
             {
-              current_dir.append (s, i, tmp-i);
+              current_dir.append (s, i, sep_pos-i);
               break;
             }
-          else if (tmp == i)
+          else if (sep_pos == i)
             {
               /* Two separators in a row, skip adding 2nd separator */
               i++;
             }
           else
             {
-              current_dir.append (s, i, tmp-i+1);
-              i = tmp + 1;
+              current_dir.append (s, i, sep_pos-i+1);
+              i = sep_pos + 1;
             }
         }