Bug 16998 - perf improvements for searching GLOBAL_BLOCK
Summary: perf improvements for searching GLOBAL_BLOCK
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: symtab (show other bugs)
Version: HEAD
: P2 normal
Target Milestone: 18.1
Assignee: Tom Tromey
URL:
Keywords: performance
Depends on: 27930 32520 32482 32510 32511 32519 32939
Blocks:
  Show dependency treegraph
 
Reported: 2014-05-29 19:49 UTC by dje
Modified: 2025-10-20 16:31 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
Project(s) to access:
ssh public key:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description dje 2014-05-29 19:49:34 UTC
This pr is a corollary to pr 16994, and is for improving searching GLOBAL_BLOCK.
[I'm trying to break down the entirety of gdb symbol problems into pieces - there's just so many of them it's easier for me to keep track of them this way.
Still need one more bit of information to tie them together: I was thinking of adding a "gdb-performance" "keyword".]

When .gdb_index was added, it was added in a way to let it easily sit along side the psymtab implementation.  Which is totally reasonable as a first pass.

However, there is, I think, a performance cost.
The way gdb looks up global symbols (static ones too, but this bug is about global symbols), is that it first searches already expanded symtabs and only if not found there does gdb try the "quick_symbol_functions" API.

E.g.,

static int
lookup_symbol_global_iterator_cb (struct objfile *objfile,
                                  void *cb_data)
{
  struct global_sym_lookup_data *data =
    (struct global_sym_lookup_data *) cb_data;

  gdb_assert (data->result == NULL);

=>data->result = lookup_symbol_aux_objfile (objfile, GLOBAL_BLOCK,
                                            data->name, data->domain);
  if (data->result == NULL)
    data->result = lookup_symbol_aux_quick (objfile, GLOBAL_BLOCK,
                                            data->name, data->domain);

  /* If we found a match, tell the iterator to stop.  Otherwise,                                                                                                 
     keep going.  */
  return (data->result != NULL);
}

lookup_symbol_aux_objfile does this:

  ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
    {
      bv = BLOCKVECTOR (s);
      block = BLOCKVECTOR_BLOCK (bv, block_index);
=>    sym = lookup_block_symbol (block, name, domain);
      if (sym)
        {
          block_found = block;
          return fixup_symbol_section (sym, objfile);
        }
    }

Over time symtabs get expanded, so the list to iterate over here grows.
But .gdb_index knows exactly which symtab(s) to look in.
[for global symbols there is only one, hence the "(s)" suffix is a bit of a misnomer, but I'm leaving that aside for now]
So why not just always use .gdb_index for GLOBAL_BLOCK lookups?
[That's an honest question, maybe I'm missing something.
Still digging.]
Comment 1 dje 2014-05-30 02:26:13 UTC
I haven't looked into the details, but I wanted to write this down while it's on my mind.  Iterating over symtabs hurts the non - .gdb_index (psymtabs) case too.
While we're slurping in psyms, can we build enough of a .gdb_index work-alike so that both the .gdb_index and psymtab cases can still use the same basic algorithm for symbol lookup (look in index for list of symtabs to search in).
Comment 2 Tom Tromey 2023-02-11 17:04:46 UTC
> But .gdb_index knows exactly which symtab(s) to look in.
...
> So why not just always use .gdb_index for GLOBAL_BLOCK lookups?

It makes sense to me.
Basically we'd want to extend the "quick" functionality
to some symtab-level operations as well, or maybe just
change the lookup code and ensure that the quick methods
also know to iterate over already-expanded symtabs.
Comment 3 Tom Tromey 2023-09-18 08:06:25 UTC
One subtlety here is that now that we have readnow_functions,
there is a situation where there really is no index.
Comment 4 Tom Tromey 2024-12-19 16:05:27 UTC
I have patches for this but they have uncovered some other bugs
that must be fixed first.
Comment 5 Tom Tromey 2025-01-05 18:28:20 UTC
The new indexer doesn't create an entry for "t::t"
(among other things) in the anon-struct.exp test.
Comment 6 Tom Tromey 2025-01-08 00:30:41 UTC
One idea might be to land the initial patches and just not
the one that causes the "t::t" thing to be an issue.
Comment 7 Tom Tromey 2025-02-22 00:45:11 UTC
I've been sending the independent bits of this series recently.
The main series itself is mostly done.  It is down to
6 regressions when using cc-with-gdb-index.  So hopefully
not too much longer.
Comment 8 Sourceware Commits 2025-09-10 22:18:06 UTC
The master branch has been updated by Tom Tromey <tromey@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=29fa4279c2f1bc2e0ac7cf77a95cbc0d83dd3c4a

commit 29fa4279c2f1bc2e0ac7cf77a95cbc0d83dd3c4a
Author: Tom Tromey <tom@tromey.com>
Date:   Sat Dec 7 15:51:24 2024 -0700

    Remove dwarf2_per_cu_data::mark
    
    This removes dwarf2_per_cu_data::mark, replacing it with a
    locally-allocated boolean vector.  It also inverts the sense of the
    flag -- now, the flag is true when a CU should be skipped, and false
    when the CU should be further examined.  Also, the validity of the
    flag is no longer dependent on 'file_matcher != NULL'.
    
    This patch makes the subsequent patch to searching a bit simpler, so
    I've separated it out.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16994
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16998
    Acked-By: Simon Marchi <simon.marchi@efficios.com>
Comment 9 Sourceware Commits 2025-09-10 22:18:12 UTC
The master branch has been updated by Tom Tromey <tromey@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=f88f9f42db8ff782758435214678ad87d3fc3a18

commit f88f9f42db8ff782758435214678ad87d3fc3a18
Author: Tom Tromey <tom@tromey.com>
Date:   Sat Dec 7 16:26:06 2024 -0700

    Have expand_symtabs_matching work for already-expanded CUs
    
    Currently, gdb will search the already-expanded symtabs in one loop,
    and then also expand matching symtabs in another loop.  However, this
    is somewhat inefficient -- when searching the already-expanded
    symtabs, all such symtabs are examined.  However, the various "quick"
    implementations already know which subset of symtabs might have a
    match.
    
    This changes the contract of expand_symtabs_matching to also call the
    callback for an already-expanded symtab.  With this change, and some
    subsequent enabling changes, the number of searched symtabs should
    sometimes be reduced.  This also cuts down on the amount of redundant
    code.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16994
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16998
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30736
    Acked-By: Simon Marchi <simon.marchi@efficios.com>
Comment 10 Sourceware Commits 2025-09-10 22:18:23 UTC
The master branch has been updated by Tom Tromey <tromey@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=5b66439bc8b0ce287852f62378cbfca28927ffd0

commit 5b66439bc8b0ce287852f62378cbfca28927ffd0
Author: Tom Tromey <tom@tromey.com>
Date:   Wed Dec 18 19:18:22 2024 -0700

    Convert default_collect_symbol_completion_matches_break_on
    
    This converts default_collect_symbol_completion_matches_break_on to
    the callback approach, merging the search loop and the call to
    expand_symtabs_matching.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16994
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16998
    Acked-By: Simon Marchi <simon.marchi@efficios.com>
Comment 11 Sourceware Commits 2025-09-10 22:18:30 UTC
The master branch has been updated by Tom Tromey <tromey@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=1696b45a6304d01f6253d43c691a682b77cb61b3

commit 1696b45a6304d01f6253d43c691a682b77cb61b3
Author: Tom Tromey <tom@tromey.com>
Date:   Wed Dec 18 17:26:16 2024 -0700

    Convert gdbpy_lookup_static_symbols
    
    This changes gdbpy_lookup_static_symbols to the callback approach,
    merging the search loop and the call to expand_symtabs_matching.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16994
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16998
    Acked-By: Simon Marchi <simon.marchi@efficios.com>
Comment 12 Sourceware Commits 2025-09-10 22:18:35 UTC
The master branch has been updated by Tom Tromey <tromey@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=59cc52253ea73f983b1d8d2f76cee6e03dddcb92

commit 59cc52253ea73f983b1d8d2f76cee6e03dddcb92
Author: Tom Tromey <tom@tromey.com>
Date:   Sat Dec 7 17:28:06 2024 -0700

    Convert ada_add_global_exceptions
    
    This converts ada_add_global_exceptions to the callback approach,
    merging the search loop and the call to expand_symtabs_matching.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16994
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16998
    Acked-By: Simon Marchi <simon.marchi@efficios.com>
Comment 13 Sourceware Commits 2025-09-10 22:18:41 UTC
The master branch has been updated by Tom Tromey <tromey@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=e271cb3281858fd475b7888043d6ed4539f0c8e3

commit e271cb3281858fd475b7888043d6ed4539f0c8e3
Author: Tom Tromey <tom@tromey.com>
Date:   Sat Dec 7 17:30:42 2024 -0700

    Convert ada_language_defn::collect_symbol_completion_matches
    
    This converts ada_language_defn::collect_symbol_completion_matches to
    the callback approach, merging the search loop and the call to
    expand_symtabs_matching.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16994
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16998
    Acked-By: Simon Marchi <simon.marchi@efficios.com>
Comment 14 Sourceware Commits 2025-09-10 22:18:46 UTC
The master branch has been updated by Tom Tromey <tromey@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=b7561b2a31ca40b59cc29311836755cc7dfe7bb4

commit b7561b2a31ca40b59cc29311836755cc7dfe7bb4
Author: Tom Tromey <tom@tromey.com>
Date:   Sat Dec 7 16:48:45 2024 -0700

    Convert ada-lang.c:map_matching_symbols
    
    This converts ada-lang.c:map_matching_symbols to the callback
    approach, merging the search loop and the call to
    expand_symtabs_matching.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16994
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16998
    Acked-By: Simon Marchi <simon.marchi@efficios.com>
Comment 15 Sourceware Commits 2025-09-10 22:18:56 UTC
The master branch has been updated by Tom Tromey <tromey@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=ce889924a72ac43e6792972f07082a4fa1f32d49

commit ce889924a72ac43e6792972f07082a4fa1f32d49
Author: Tom Tromey <tom@tromey.com>
Date:   Tue Dec 31 10:30:15 2024 -0700

    Simplify basic_lookup_transparent_type
    
    This patch changes basic_lookup_transparent_type to always work via
    the "quick" API -- that is, no separate search of the already-expanded
    symtabs is needed.
    
    This is more efficient when many CUs have already been expanded.  It
    also makes the lookup more consistent, as the result is no longer
    dependent on the order in which CUs were previously expanded.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16994
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16998
    Acked-By: Simon Marchi <simon.marchi@efficios.com>
Comment 16 Sourceware Commits 2025-09-10 22:19:01 UTC
The master branch has been updated by Tom Tromey <tromey@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=4a0b3e62a43567fadadf167390feaf147118fd76

commit 4a0b3e62a43567fadadf167390feaf147118fd76
Author: Tom Tromey <tom@tromey.com>
Date:   Tue Dec 31 13:11:50 2024 -0700

    Remove objfile::expand_symtabs_for_function
    
    objfile::expand_symtabs_for_function only has a single caller now, so
    it can be removed.  This also allows us to merge the expansion and
    searching phases, as done in other patches in this series.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16994
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16998
    Acked-By: Simon Marchi <simon.marchi@efficios.com>
Comment 17 Sourceware Commits 2025-09-10 22:19:07 UTC
The master branch has been updated by Tom Tromey <tromey@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=5f99f39a396476c122a04f8eb7c0c40c76c101db

commit 5f99f39a396476c122a04f8eb7c0c40c76c101db
Author: Tom Tromey <tom@tromey.com>
Date:   Tue Dec 31 13:15:17 2024 -0700

    Convert linespec.c:iterate_over_all_matching_symtabs
    
    This converts linespec.c:iterate_over_all_matching_symtabs to the
    callback approach, merging the search loop and the call to
    expand_symtabs_matching.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16994
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16998
    Acked-By: Simon Marchi <simon.marchi@efficios.com>
Comment 18 Sourceware Commits 2025-09-10 22:19:33 UTC
The master branch has been updated by Tom Tromey <tromey@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=c879f4dc3e317cf6353a45a803ecf00d577a13d8

commit c879f4dc3e317cf6353a45a803ecf00d577a13d8
Author: Tom Tromey <tom@tromey.com>
Date:   Tue Dec 31 13:30:18 2024 -0700

    Convert lookup_symbol_via_quick_fns
    
    This converts lookup_symbol_via_quick_fns to the callback approach,
    merging the search loop and the call to expand_symtabs_matching.
    
    Note that this changes lookup_symbol_via_quick_fns to use a
    best_symbol_tracker.  Before this patch there was a discrepancy here
    between the two search functions.
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16994
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16998
    Acked-By: Simon Marchi <simon.marchi@efficios.com>
Comment 19 Sourceware Commits 2025-09-10 22:19:38 UTC
The master branch has been updated by Tom Tromey <tromey@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=86ac8c546235a67d6a6bb29476a3a9ac8f7a620a

commit 86ac8c546235a67d6a6bb29476a3a9ac8f7a620a
Author: Tom Tromey <tom@tromey.com>
Date:   Thu Jan 2 15:28:18 2025 -0700

    Convert lookup_symbol_in_objfile
    
    This converts lookup_symbol_in_objfile to the callback approach by
    removing the call to lookup_symbol_in_objfile_symtabs.  (The latter is
    not removed as there are still other callers.)
    
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16994
    Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16998
    Acked-By: Simon Marchi <simon.marchi@efficios.com>
Comment 20 Tom Tromey 2025-09-11 00:08:10 UTC
Those dependencies don't really block this, since it is fixed.
Comment 21 Tom Tromey 2025-09-21 18:05:07 UTC
Meant to close this.