[02/27] Avoid extra allocations in block

Message ID 20230120214618.3236224-3-tom@tromey.com
State New
Headers
Series C++-ify struct block |

Commit Message

Tom Tromey Jan. 20, 2023, 9:45 p.m. UTC
  block_set_scope and block_set_using unconditionally allocate the block
namespace object.  However, this isn't truly needed, so arrange to
only allocate when it is.
---
 gdb/block.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
  

Patch

diff --git a/gdb/block.c b/gdb/block.c
index 751f67d30f7..f24a2b5d084 100644
--- a/gdb/block.c
+++ b/gdb/block.c
@@ -320,6 +320,12 @@  void
 block_set_scope (struct block *block, const char *scope,
 		 struct obstack *obstack)
 {
+  if (scope == nullptr || scope[0] == '\0')
+    {
+      /* Don't bother.  */
+      return;
+    }
+
   block_initialize_namespace (block, obstack);
 
   block->namespace_info ()->scope = scope;
@@ -346,6 +352,12 @@  block_set_using (struct block *block,
 		 struct using_direct *using_decl,
 		 struct obstack *obstack)
 {
+  if (using_decl == nullptr)
+    {
+      /* Don't bother.  */
+      return;
+    }
+
   block_initialize_namespace (block, obstack);
 
   block->namespace_info ()->using_decl = using_decl;