# HG changeset patch
# User Petter Tomner <tomner@kth.se>
# Date 1637934231 -3600
#      Fri Nov 26 14:43:51 2021 +0100
# Node ID 40f4dd049e71f2147ab82d2bf3cb346c9ca22b88
# Parent  bf619727bf6c2f00e70f95f471d447ef67ab2088
interpreter: Avoid string construction

Split function look-ups into a querry and process part to not construct
strings.

Replace some string literals with const static string objects.

Make some getter functions return const string references.

libinterp/corefcn/
        * fcn-info.cc: static strings, split up querry/process querry
        * fcn-info.h
        * load-path.cc: split up querry/process querry
        * load-path.h: fcn_map_type -> unordered_map
        * symscope.cc: empty string object
        * symscope.h: fcn_file_name and dir_name const ref
libinterp/octave-value/
        * ov-base.cc: btyp_class_name -> const
        * ov-base.h
libinterp/parse-tree/
        * pt-eval.cc: split up querry/process querry
        * pt-eval.h: m_autoload_map -> unordered_map

diff -r bf619727bf6c -r 40f4dd049e71 libinterp/corefcn/fcn-info.cc
--- a/libinterp/corefcn/fcn-info.cc	Sun Nov 21 15:25:04 2021 +0100
+++ b/libinterp/corefcn/fcn-info.cc	Fri Nov 26 14:43:51 2021 +0100
@@ -54,14 +54,18 @@
   {
     octave_value retval;
 
+    static const std::string who {"fcn_info::fcn_info_rep::load_private_function"};
     load_path& lp
-      = __get_load_path__ ("fcn_info::fcn_info_rep::load_private_function");
+      = __get_load_path__ (who);
 
-    std::string file_name = lp.find_private_fcn (dir_name, name);
+    bool found;
+    auto it = lp.querry_toplvl_priv_fcn (dir_name, found);
 
-    if (file_name.empty ())
+    if (!found)
       return retval;
 
+    std::string file_name = lp.process_querry_toplvl_priv_fcn (it, dir_name, name);
+
     octave_value ov_fcn = load_fcn_from_file (file_name, dir_name);
 
     if (ov_fcn.is_undefined ())
@@ -96,15 +100,19 @@
   {
     octave_value retval;
 
-    std::string dir_name;
+    static const std::string who{"fcn_info::fcn_info_rep::load_class_constructor"};
+    load_path& lp
+      = __get_load_path__ (who);
 
-    load_path& lp
-      = __get_load_path__ ("fcn_info::fcn_info_rep::load_class_constructor");
+    bool found;
+    load_path::method_querry_handle h = lp.querry_method (name, package_name, found);
 
-    std::string file_name = lp.find_method (name, name, dir_name, package_name);
+    if (found)
+      {
+	std::string dir_name;
 
-    if (! file_name.empty ())
-      {
+	std::string file_name = lp.process_querry_method (h, name, dir_name);
+
         octave_value ov_fcn
           = load_fcn_from_file (file_name, dir_name, name,
                                 package_name);
@@ -159,22 +167,27 @@
       retval = load_class_constructor ();
     else
       {
+	static const std::string who {"fcn_info::fcn_info_rep::load_class_method"};
+
         cdef_manager& cdm
-          = __get_cdef_manager__ ("fcn_info::fcn_info_rep::load_class_method");
+          = __get_cdef_manager__ (who);
 
         retval = cdm.find_method_symbol (name, dispatch_type);
 
         if (! retval.is_defined ())
           {
-            std::string dir_name;
+            load_path& lp = __get_load_path__ (who);
 
-            load_path& lp = __get_load_path__ ("fcn_info::fcn_info_rep::load_class_method");
+            bool found;
+            auto q = lp.querry_toplvl_method (dispatch_type, found);
 
-            std::string file_name = lp.find_method (dispatch_type, name,
-                                                    dir_name);
+            if (found)
+              {
+		std::string dir_name;
 
-            if (! file_name.empty ())
-              {
+		std::string file_name =
+		    lp.process_querry_toplvl_method (q, name, dir_name);
+
                 octave_value ov_fcn
                   = load_fcn_from_file (file_name, dir_name,
                                         dispatch_type);
@@ -197,7 +210,7 @@
                 // Search parent classes
 
                 symbol_table& symtab
-                  = __get_symbol_table__ ("fcn_info::fcn_info_rep::load_class_method");
+                  = __get_symbol_table__ (who);
 
                 const std::list<std::string>& plist
                   = symtab.parent_classes (dispatch_type);
@@ -303,7 +316,8 @@
             // There's a non-builtin class in the argument list.
             dispatch_type = args(i).class_name ();
 
-            symbol_table& symtab = __get_symbol_table__ ("get_dispatch_type");
+            static const std::string who {"get_dispatch_type"};
+            symbol_table& symtab = __get_symbol_table__ (who);
 
             for (int j = i+1; j < n; j++)
               {
@@ -676,7 +690,7 @@
 
         // Local function.
 
-        std::string fcn_file = search_scope.fcn_file_name ();
+        const std::string &fcn_file = search_scope.fcn_file_name ();
 
         // For anonymous functions we look at the parent scope so that if
         // they were defined within class methods and use local functions
@@ -1017,13 +1031,18 @@
 
     if (! autoload_function.is_defined ())
       {
+	static const std::string who{"fcn_info::fcn_info_rep::x_builtin_find"};
+
         tree_evaluator& tw
-          = __get_evaluator__ ("fcn_info::fcn_info_rep::x_builtin_find");
+          = __get_evaluator__ (who);
 
-        std::string file_name = tw.lookup_autoload (name);
+	bool found;
+	auto it = tw.autoload_querry (name, found);
 
-        if (! file_name.empty ())
+	if (found)
           {
+	    std::string file_name = tw.autoload_process_querry (it);
+
             std::size_t pos = file_name.find_last_of (sys::file_ops::dir_sep_chars ());
 
             std::string dir_name = file_name.substr (0, pos);
@@ -1049,21 +1068,46 @@
 
     if (function_on_path.is_undefined ())
       {
-        std::string dir_name;
-
+	static const std::string who {"fcn_info::fcn_info_rep::find_user_function"};
         load_path& lp
-          = __get_load_path__ ("fcn_info::fcn_info_rep::find_user_function");
+	  = __get_load_path__ (who);
 
+	if (lp.should_do_querry_1 (name))
+	  {
+	    bool found;
+	    size_t method_pos;
+	    auto q = lp.querry_find_fcn_1 (name,
+					   package_name,
+					   found, method_pos);
 
-        std::string file_name = lp.find_fcn (name, dir_name, package_name);
+	    if (found)
+	      {
+		std::string method = name.substr (method_pos);
+		std::string dir_name;
+		std::string file_name = lp.process_querry_method (q, method, dir_name);
 
-        if (! file_name.empty ())
+		octave_value ov_fcn =
+		    load_fcn_from_file (file_name, dir_name, "", package_name);
+
+		if (ov_fcn.is_defined ())
+		  function_on_path = ov_fcn;
+	      }
+          }
+        else
           {
-            octave_value ov_fcn
-              = load_fcn_from_file (file_name, dir_name, "", package_name);
+	    bool found;
+	    auto q = lp.querry_find_fcn_2(name, package_name, found);
 
-            if (ov_fcn.is_defined ())
-              function_on_path = ov_fcn;
+	    if (found)
+	      {
+        	std::string dir_name;
+        	std::string file_name = lp.process_querry_fcn_2 (q, name, dir_name);
+		octave_value ov_fcn =
+		    load_fcn_from_file (file_name, dir_name, "", package_name);
+
+		if (ov_fcn.is_defined ())
+		  function_on_path = ov_fcn;
+              }
           }
       }
 
@@ -1079,8 +1123,9 @@
 
     if (package.is_undefined ())
       {
+	static const std::string who {"fcn_info::fcn_info_rep::find_package"};
         cdef_manager& cdm
-          = __get_cdef_manager__ ("fcn_info::fcn_info_rep::find_package");
+	  = __get_cdef_manager__ (who);
 
         package = cdm.find_package_symbol (full_name ());
       }
diff -r bf619727bf6c -r 40f4dd049e71 libinterp/corefcn/fcn-info.h
--- a/libinterp/corefcn/fcn-info.h	Sun Nov 21 15:25:04 2021 +0100
+++ b/libinterp/corefcn/fcn-info.h	Fri Nov 26 14:43:51 2021 +0100
@@ -54,7 +54,7 @@
     public:
 
       fcn_info_rep (const std::string& nm)
-        : name (nm), package_name (), local_functions (),
+        : name (nm), package_name (), m_full_name (name), local_functions (),
           private_functions (), class_constructors (), class_methods (),
           cmdline_function (), autoload_function (), function_on_path (),
           built_in_function ()
@@ -66,6 +66,7 @@
             package_name = name.substr (0, pos);
             name = name.substr (pos+1);
           }
+
       }
 
       // No copying!
@@ -196,18 +197,17 @@
 
       octave_value dump (void) const;
 
-      std::string full_name (void) const
+      const std::string& full_name (void) const
       {
-        if (package_name.empty ())
-          return name;
-        else
-          return package_name + '.' + name;
+	return m_full_name;
       }
 
       std::string name;
 
       std::string package_name;
 
+      std::string m_full_name;
+
       // File name to function object.
       std::map<std::string, octave_value> local_functions;
 
diff -r bf619727bf6c -r 40f4dd049e71 libinterp/corefcn/load-path.cc
--- a/libinterp/corefcn/load-path.cc	Sun Nov 21 15:25:04 2021 +0100
+++ b/libinterp/corefcn/load-path.cc	Fri Nov 26 14:43:51 2021 +0100
@@ -1674,6 +1674,34 @@
   }
 
   std::string
+  load_path::package_info::process_querry_fcn_2 (const_fcn_map_iterator p,
+						 const std::string& fcn,
+						 std::string &dir_name,
+						 int type) const
+  {
+    std::string retval;
+
+    const file_info_list_type& file_info_list = p->second;
+
+    for (const auto& fi : file_info_list)
+      {
+	retval = sys::file_ops::concat (fi.dir_name, fcn);
+
+	if (check_file_type (retval, type, fi.types,
+			     fcn, "load_path::find_fcn"))
+	  {
+	    dir_name = fi.dir_name;
+	    break;
+	  }
+	else
+	  retval = "";
+      }
+
+    return retval;
+  }
+
+// Note: This function duplicates functionality with querry_fcn and process_querry_fcn* functions
+  std::string
   load_path::package_info::find_fcn (const std::string& fcn,
                                      std::string& dir_name,
                                      int type) const
@@ -1698,8 +1726,6 @@
       }
     else
       {
-        dir_name = "";
-
         const_fcn_map_iterator p = m_fcn_map.find (fcn);
 
         if (p != m_fcn_map.end ())
@@ -1726,6 +1752,34 @@
   }
 
   std::string
+  load_path::package_info::
+  process_querry_toplvl_priv_func (const_private_fcn_map_iterator q,
+				   const std::string& dir,
+				   const std::string& fcn,
+				   int type) const
+  {
+    std::string retval;
+
+    const dir_info::fcn_file_map_type& fcn_file_map = q->second;
+
+    dir_info::const_fcn_file_map_iterator p = fcn_file_map.find (fcn);
+
+    if (p != fcn_file_map.end ())
+      {
+	std::string fname
+	  = sys::file_ops::concat (sys::file_ops::concat (dir, "private"), fcn);
+
+	if (check_file_type (fname, type, p->second, fcn,
+			     "load_path::find_private_fcn"))
+	  retval = fname;
+      }
+
+    return retval;
+  }
+
+// Note: This function duplicates functionality with process_querry_toplvl_priv_func
+// and querry_toplvl_priv_func functions
+  std::string
   load_path::package_info::find_private_fcn (const std::string& dir,
                                              const std::string& fcn,
                                              int type) const
@@ -1757,6 +1811,42 @@
   }
 
   std::string
+  load_path::package_info::process_querry_method (const_method_map_iterator q,
+						  const std::string& meth,
+						  std::string& dir_name,
+						  int type) const
+  {
+    std::string retval;
+
+    const fcn_map_type& m = q->second;
+
+    const_fcn_map_iterator p = m.find (meth);
+
+    if (p != m.end ())
+      {
+        const file_info_list_type& file_info_list = p->second;
+
+        for (const auto& fi : file_info_list)
+          {
+            retval = sys::file_ops::concat (fi.dir_name, meth);
+
+            bool found = check_file_type (retval, type, fi.types,
+                                          meth, "load_path::find_method");
+
+            if (found)
+              {
+                dir_name = fi.dir_name;
+                break;
+              }
+            else
+              retval = "";
+          }
+      }
+
+    return retval;
+  }
+
+  std::string
   load_path::package_info::find_method (const std::string& class_name,
                                         const std::string& meth,
                                         std::string& dir_name,
@@ -1768,35 +1858,11 @@
 
     dir_name = "";
 
-    const_method_map_iterator q = m_method_map.find (class_name);
-
-    if (q != m_method_map.end ())
-      {
-        const fcn_map_type& m = q->second;
-
-        const_fcn_map_iterator p = m.find (meth);
-
-        if (p != m.end ())
-          {
-            const file_info_list_type& file_info_list = p->second;
-
-            for (const auto& fi : file_info_list)
-              {
-                retval = sys::file_ops::concat (fi.dir_name, meth);
-
-                bool found = check_file_type (retval, type, fi.types,
-                                              meth, "load_path::find_method");
-
-                if (found)
-                  {
-                    dir_name = fi.dir_name;
-                    break;
-                  }
-                else
-                  retval = "";
-              }
-          }
-      }
+    bool found;
+    const_method_map_iterator q = querry_method (class_name, found);
+
+    if (found)
+      retval = process_querry_method (q, meth, dir_name, type);
 
     return retval;
   }
diff -r bf619727bf6c -r 40f4dd049e71 libinterp/corefcn/load-path.h
--- a/libinterp/corefcn/load-path.h	Sun Nov 21 15:25:04 2021 +0100
+++ b/libinterp/corefcn/load-path.h	Fri Nov 26 14:43:51 2021 +0100
@@ -32,6 +32,7 @@
 #include <iosfwd>
 #include <list>
 #include <map>
+#include <unordered_map>
 #include <set>
 #include <string>
 
@@ -76,6 +77,8 @@
     bool contains_file_in_dir (const std::string& file_name,
                                const std::string& dir_name);
 
+    // process_querry_toplvl_method and querry_toplvl_method is a specialization of find_method
+    // bellow, to allow for a lookup without string construction
     std::string find_method (const std::string& class_name,
                              const std::string& meth,
                              std::string& dir_name,
@@ -108,18 +111,6 @@
     std::list<std::string>
     get_all_package_names (bool only_top_level = true) const;
 
-    std::string find_fcn (const std::string& fcn, std::string& dir_name,
-                          const std::string& pack_name = "")
-    {
-      return get_package (pack_name).find_fcn (fcn, dir_name);
-    }
-
-    std::string find_fcn (const std::string& fcn,
-                          const std::string& pack_name = "")
-    {
-      std::string dir_name;
-      return find_fcn (fcn, dir_name, pack_name);
-    }
 
     std::string find_private_fcn (const std::string& dir,
                                   const std::string& fcn,
@@ -371,7 +362,7 @@
     typedef file_info_list_type::iterator file_info_list_iterator;
 
     // <FCN_NAME, FILE_INFO_LIST>
-    typedef std::map<std::string, file_info_list_type> fcn_map_type;
+    typedef std::unordered_map<std::string, file_info_list_type> fcn_map_type;
 
     typedef fcn_map_type::const_iterator const_fcn_map_iterator;
     typedef fcn_map_type::iterator fcn_map_iterator;
@@ -380,14 +371,18 @@
     typedef std::map<std::string, dir_info::fcn_file_map_type>
       private_fcn_map_type;
 
+    // <CLASS_NAME, <FCN_NAME, FILE_INFO_LIST>>
+    typedef std::map<std::string, fcn_map_type> method_map_type;
+
+    public:
+
+    typedef method_map_type::const_iterator const_method_map_iterator;
+    typedef method_map_type::iterator method_map_iterator;
+
     typedef private_fcn_map_type::const_iterator const_private_fcn_map_iterator;
     typedef private_fcn_map_type::iterator private_fcn_map_iterator;
 
-    // <CLASS_NAME, <FCN_NAME, FILE_INFO_LIST>>
-    typedef std::map<std::string, fcn_map_type> method_map_type;
-
-    typedef method_map_type::const_iterator const_method_map_iterator;
-    typedef method_map_type::iterator method_map_iterator;
+    private:
 
     class package_info
     {
@@ -459,6 +454,55 @@
       find_private_fcn (const std::string& dir, const std::string& fcn,
                         int type = M_FILE | OCT_FILE | MEX_FILE) const;
 
+      const_private_fcn_map_iterator
+      querry_toplvl_priv_fcn (const std::string& dir_name, bool& found)
+      {
+	auto it = m_private_fcn_map.find (dir_name);
+
+	found = (it != m_private_fcn_map.cend ());
+
+	return it;
+      }
+
+      std::string
+      process_querry_toplvl_priv_func (const_private_fcn_map_iterator it,
+				       const std::string& dir,
+				       const std::string& fcn,
+				       int type = M_FILE | OCT_FILE | MEX_FILE) const;
+
+      const_fcn_map_iterator
+      querry_fcn_2 (const std::string &fcn, bool &found) const
+      {
+	const_fcn_map_iterator p = m_fcn_map.find (fcn);
+	found = (p != m_fcn_map.cend ());
+	return p;
+      }
+
+      std::string
+      process_querry_fcn_2 (const_fcn_map_iterator it,
+			    const std::string& fcn,
+			    std::string &dir_name,
+			    int type = M_FILE | OCT_FILE | MEX_FILE) const;
+
+      const_method_map_iterator
+      querry_method (const std::string& class_name, bool &found) const
+      {
+	const_method_map_iterator it = m_method_map.find (class_name);
+
+	found = (it != m_method_map.cend ());
+
+	return it;
+      }
+
+      std::string
+      process_querry_method (const_method_map_iterator it,
+			     const std::string& meth,
+			     std::string& dir_name,
+			     int type = M_FILE | OCT_FILE | MEX_FILE) const;
+
+      // process_querry_method and querry_method is a specialization of find_method
+      // bellow, to allow for a lookup without string construction
+
       std::string
       find_method (const std::string& class_name, const std::string& meth,
                    std::string& dir_name,
@@ -512,9 +556,12 @@
     // <PACKAGE_NAME, PACKAGE_INFO>
     typedef std::map<std::string, package_info> package_map_type;
 
+    public:
     typedef package_map_type::const_iterator const_package_map_iterator;
     typedef package_map_type::iterator package_map_iterator;
 
+    private:
+
     std::function<void (const std::string&)> add_hook;
 
     std::function<void (const std::string&)> remove_hook;
@@ -578,6 +625,148 @@
 
     std::string m_command_line_path;
 
+
+
+    public:
+
+    // Handle for a package_info reference, to not break implementation
+    // encapsulation of package_info (it is private)
+    class method_querry_handle {
+      public:
+      method_querry_handle (const method_querry_handle &&handle) :
+	m_pkg(handle.m_pkg), m_it (handle.m_it) {}
+      method_querry_handle (const method_querry_handle &handle) :
+	m_pkg(handle.m_pkg), m_it (handle.m_it) {}
+      private:
+      method_querry_handle (package_info &pkg, const_method_map_iterator it) :
+	m_pkg(pkg), m_it(it) {}
+
+      friend class load_path;
+
+      package_info &m_pkg;
+      const_method_map_iterator m_it;
+    };
+
+    class fcn_querry_handle {
+      public:
+      fcn_querry_handle (const fcn_querry_handle &&handle) :
+	m_pkg(handle.m_pkg), m_it (handle.m_it) {}
+      fcn_querry_handle (const fcn_querry_handle &handle) :
+	m_pkg(handle.m_pkg), m_it (handle.m_it) {}
+      private:
+      fcn_querry_handle (package_info &pkg, const_fcn_map_iterator it) :
+	m_pkg(pkg), m_it(it) {}
+
+      friend class load_path;
+
+      package_info &m_pkg;
+      const_fcn_map_iterator m_it;
+    };
+
+    const_private_fcn_map_iterator
+    querry_toplvl_priv_fcn (const std::string& dir_name, bool& found)
+    {
+      return m_top_level_package.querry_toplvl_priv_fcn (dir_name, found);
+    }
+
+    std::string
+    process_querry_toplvl_priv_fcn (const_private_fcn_map_iterator it,
+				    const std::string& dir_name,
+				    const std::string& fcn)
+    {
+      return m_top_level_package.process_querry_toplvl_priv_func (it, dir_name, fcn);
+    }
+
+    const_method_map_iterator
+    querry_toplvl_method (const std::string& class_name, bool &found)
+    {
+      return m_top_level_package.querry_method (class_name, found);
+    }
+
+    std::string
+    process_querry_toplvl_method (const_method_map_iterator it,
+				  const std::string& meth,
+				  std::string& dir_name)
+    {
+      return m_top_level_package.process_querry_method (it, meth, dir_name);
+    }
+
+    method_querry_handle
+    querry_method (const std::string& class_name,
+		   const std::string& package_name,
+		   bool &found)
+    {
+      auto &pkg = get_package (package_name);
+      auto it = pkg.querry_method (class_name, found);
+
+      return method_querry_handle {pkg, it};
+    }
+
+    std::string
+    process_querry_method (method_querry_handle handle,
+			   const std::string& meth,
+			   std::string& dir_name)
+    {
+      return handle.m_pkg.process_querry_method (handle.m_it, meth, dir_name);
+    }
+
+    static bool
+    should_do_querry_1 (const std::string& fcn)
+    {
+      if (fcn.length () > 0 && fcn[0] == '@')
+	return true;
+      return false;
+    }
+
+    method_querry_handle
+    querry_find_fcn_1 (const std::string& fcn,
+                       const std::string& pack_name,
+		       bool &found,
+		       std::size_t &metod_pos)
+    {
+      std::size_t pos = fcn.find ('/');
+
+      if (pos != std::string::npos)
+	{
+	  std::string class_name = fcn.substr (1, pos-1);
+	  metod_pos = pos+1;
+
+	  return querry_method (class_name, pack_name, found);
+	}
+
+      found = false;
+      return {m_top_level_package, const_method_map_iterator {} };
+    }
+
+    fcn_querry_handle
+    querry_find_fcn_2 (const std::string &fcn, const std::string &package_name, bool &found)
+    {
+      auto &pkg = get_package (package_name);
+      auto it = pkg.querry_fcn_2 (fcn, found);
+
+      return fcn_querry_handle {pkg, it};
+    }
+
+    std::string
+    process_querry_fcn_2 (fcn_querry_handle h,
+			  const std::string& fcn,
+			  std::string &dir_name)
+    {
+      return h.m_pkg.process_querry_fcn_2 (h.m_it, fcn, dir_name);
+    }
+
+    std::string find_fcn (const std::string& fcn, std::string& dir_name,
+                          const std::string& pack_name = "")
+    {
+      return get_package (pack_name).find_fcn (fcn, dir_name);
+    }
+
+    std::string find_fcn (const std::string& fcn,
+                          const std::string& pack_name = "")
+    {
+      std::string dir_name;
+      return find_fcn (fcn, dir_name, pack_name);
+    }
   };
 
   extern std::string
diff -r bf619727bf6c -r 40f4dd049e71 libinterp/corefcn/symscope.cc
--- a/libinterp/corefcn/symscope.cc	Sun Nov 21 15:25:04 2021 +0100
+++ b/libinterp/corefcn/symscope.cc	Fri Nov 26 14:43:51 2021 +0100
@@ -384,4 +384,11 @@
 
     return ppsr->localfunctions ();
   }
+
+  // An empty string object to return const references to
+  const std::string symbol_scope::m_empty_string;
 }
+
+
+
+
diff -r bf619727bf6c -r 40f4dd049e71 libinterp/corefcn/symscope.h
--- a/libinterp/corefcn/symscope.h	Sun Nov 21 15:25:04 2021 +0100
+++ b/libinterp/corefcn/symscope.h	Fri Nov 26 14:43:51 2021 +0100
@@ -271,11 +271,11 @@
       m_fcn_file_name = name;
     }
 
-    std::string fcn_file_name (void) const { return m_fcn_file_name; }
+    const std::string & fcn_file_name (void) const { return m_fcn_file_name; }
 
     void cache_dir_name (const std::string& name);
 
-    std::string dir_name (void) const { return m_dir_name; }
+    const std::string & dir_name (void) const { return m_dir_name; }
 
     void mark_primary_fcn_scope (void) { m_is_primary_fcn_scope = true; }
 
@@ -630,14 +630,14 @@
         m_rep->cache_dir_name (name);
     }
 
-    std::string fcn_file_name (void) const
+    const std::string & fcn_file_name (void) const
     {
-      return m_rep ? m_rep->fcn_file_name () : "";
+      return m_rep ? m_rep->fcn_file_name () : m_empty_string;
     }
 
-    std::string dir_name (void) const
+    const std::string & dir_name (void) const
     {
-      return m_rep ? m_rep->dir_name () : "";
+      return m_rep ? m_rep->dir_name () : m_empty_string;
     }
 
     void mark_primary_fcn_scope (void)
@@ -722,6 +722,8 @@
     }
 
   private:
+    // An empty string object to return const references to
+    static const std::string m_empty_string;
 
     std::shared_ptr<symbol_scope_rep> m_rep;
   };
diff -r bf619727bf6c -r 40f4dd049e71 libinterp/octave-value/ov-base.cc
--- a/libinterp/octave-value/ov-base.cc	Sun Nov 21 15:25:04 2021 +0100
+++ b/libinterp/octave-value/ov-base.cc	Fri Nov 26 14:43:51 2021 +0100
@@ -86,7 +86,7 @@
   return retval;
 }
 
-std::string btyp_class_name[btyp_num_types+1] =
+const std::string btyp_class_name[btyp_num_types+1] =
 {
   "double", "single", "double", "single",
   "int8", "int16", "int32", "int64",
diff -r bf619727bf6c -r 40f4dd049e71 libinterp/octave-value/ov-base.h
--- a/libinterp/octave-value/ov-base.h	Sun Nov 21 15:25:04 2021 +0100
+++ b/libinterp/octave-value/ov-base.h	Fri Nov 26 14:43:51 2021 +0100
@@ -93,7 +93,7 @@
   btyp_num_types = btyp_unknown
 };
 
-extern OCTINTERP_API std::string btyp_class_name [];
+extern OCTINTERP_API const std::string btyp_class_name [];
 
 inline bool btyp_isnumeric (builtin_type_t btyp)
 { return btyp <= btyp_uint64; }
diff -r bf619727bf6c -r 40f4dd049e71 libinterp/parse-tree/pt-eval.cc
--- a/libinterp/parse-tree/pt-eval.cc	Sun Nov 21 15:25:04 2021 +0100
+++ b/libinterp/parse-tree/pt-eval.cc	Fri Nov 26 14:43:51 2021 +0100
@@ -4419,20 +4419,30 @@
     return m;
   }
 
+  std::string
+  tree_evaluator::autoload_process_querry (std::unordered_map<std::string, std::string>::const_iterator it) const
+  {
+    load_path& lp = m_interpreter.get_load_path ();
+
+    return lp.find_file (it->second);
+  }
+
+  // lookup_autoload is a composition of autoload_querry and autoload_process_querry.
+  // The later can be used to avoid string ctors/dtors at the callsite
   std::string tree_evaluator::lookup_autoload (const std::string& nm) const
   {
-    std::string retval;
-
-    auto p = m_autoload_map.find (nm);
-
-    if (p != m_autoload_map.end ())
-      {
-        load_path& lp = m_interpreter.get_load_path ();
-
-        retval = lp.find_file (p->second);
-      }
-
-    return retval;
+    // This function is split up in autoload_querry and autoload_process_querry
+    // to avoid std::string construction and destruction in fcn_info_rep::find_autoload
+
+    bool found;
+    // autoload_querry just does m_autoload_map.find (nm) and
+    // sets found to true if it found anything
+    auto it = autoload_querry (nm, found);
+
+    if (found)
+      return autoload_process_querry (it);
+
+    return "";
   }
 
   std::list<std::string> tree_evaluator::autoloaded_functions (void) const
diff -r bf619727bf6c -r 40f4dd049e71 libinterp/parse-tree/pt-eval.h
--- a/libinterp/parse-tree/pt-eval.h	Sun Nov 21 15:25:04 2021 +0100
+++ b/libinterp/parse-tree/pt-eval.h	Fri Nov 26 14:43:51 2021 +0100
@@ -34,6 +34,7 @@
 #include <set>
 #include <stack>
 #include <string>
+#include <unordered_map>
 
 #include "bp-table.h"
 #include "call-stack.h"
@@ -572,6 +573,20 @@
 
     octave_map get_autoload_map (void) const;
 
+    std::unordered_map<std::string, std::string>::const_iterator
+    autoload_querry (const std::string & nm, bool &found) const
+      {
+        auto p = m_autoload_map.find (nm);
+
+        found = (p != m_autoload_map.end ());
+
+        return p;
+      }
+
+    std::string autoload_process_querry (std::unordered_map<std::string, std::string>::const_iterator it) const;
+
+    // lookup_autoload is a composition of autoload_querry and autoload_process_querry.
+    // The later can be used to avoid string ctors/dtors at the callsite
     std::string lookup_autoload (const std::string& nm) const;
 
     std::list<std::string> autoloaded_functions (void) const;
@@ -885,7 +900,7 @@
     const std::list<octave_lvalue> *m_lvalue_list;
 
     // List of autoloads (function -> file mapping).
-    std::map<std::string, std::string> m_autoload_map;
+    std::unordered_map<std::string, std::string> m_autoload_map;
 
     bp_table m_bp_table;
 
