# HG changeset patch
# User Petter Tomner <tomner@kth.se>
# Date 1639704485 -3600
#      Fri Dec 17 02:28:05 2021 +0100
# Node ID c8fad495bb11fecd8d84792e4ae0fb78128f68f1
# Parent  cc66e5654de21af172969ab88e28ff1ebbecfe07
interpreter: Avoid string construction 2

const string references instead of copies.

libinterp/corefcn/
        * fcn-info.cc: const ref
        * fcn-info.h: cache full name too
        * symscope.cc: static empty string
        * symscope.h: const ref

diff -r cc66e5654de2 -r c8fad495bb11 libinterp/corefcn/fcn-info.cc
--- a/libinterp/corefcn/fcn-info.cc	Thu Dec 16 21:46:44 2021 +0100
+++ b/libinterp/corefcn/fcn-info.cc	Fri Dec 17 02:28:05 2021 +0100
@@ -485,7 +485,7 @@
 
         if (! (fcn->is_subfunction () || fcn->is_anonymous_function ()))
           {
-            std::string ff = fcn->fcn_file_name ();
+            const std::string &ff = fcn->fcn_file_name ();
 
             if (! ff.empty ())
               {
@@ -676,7 +676,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
@@ -909,7 +909,7 @@
       {
         // Private function.
 
-        std::string dir_name = search_scope.dir_name ();
+        const std::string &dir_name = search_scope.dir_name ();
 
         if (! dir_name.empty ())
           {
@@ -943,7 +943,7 @@
 
         // Local function.
 
-        std::string fcn_file = search_scope.fcn_file_name ();
+        const std::string &fcn_file = search_scope.fcn_file_name ();
 
         if (! fcn_file.empty ())
           {
diff -r cc66e5654de2 -r c8fad495bb11 libinterp/corefcn/fcn-info.h
--- a/libinterp/corefcn/fcn-info.h	Thu Dec 16 21:46:44 2021 +0100
+++ b/libinterp/corefcn/fcn-info.h	Fri Dec 17 02:28:05 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 (nm), local_functions (),
           private_functions (), class_constructors (), class_methods (),
           cmdline_function (), autoload_function (), function_on_path (),
           built_in_function ()
@@ -196,18 +196,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 cc66e5654de2 -r c8fad495bb11 libinterp/corefcn/symscope.cc
--- a/libinterp/corefcn/symscope.cc	Thu Dec 16 21:46:44 2021 +0100
+++ b/libinterp/corefcn/symscope.cc	Fri Dec 17 02:28:05 2021 +0100
@@ -385,4 +385,7 @@
 
     return ppsr->localfunctions ();
   }
+
+  // An empty string object to return const references to
+  const std::string symbol_scope::m_empty_string;
 }
diff -r cc66e5654de2 -r c8fad495bb11 libinterp/corefcn/symscope.h
--- a/libinterp/corefcn/symscope.h	Thu Dec 16 21:46:44 2021 +0100
+++ b/libinterp/corefcn/symscope.h	Fri Dec 17 02:28:05 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;
   };
