On 10/20/26 8:26 AM, Chris Burr wrote:
PCH objects are currently laid out in saving_htab order, which is the
hash of their address, so objects that are used together (such as the
trees of one function body) end up scattered over the file.  The PCH is
mapped privately, so every page a translation unit writes to gets
copied, and with a large C++ PCH most pages end up dirty even for a
trivial translation unit.

This patch allocates the objects in the order in which
gt_pch_note_object first sees them, which roughly follows a depth-first
walk from the roots, so related objects end up next to each other within
the region of their size order.  It also adds
gt_pch_note_likely_modified to put objects that are likely to be written
to at the start of their region, which c_common_write_pch uses for the
declarations of functions with a body.

The generated code and the size of the PCH are unchanged.  Peak private
memory and wall time of cc1plus with a 10 MB preprocessed C++ header,
at --param=ggc-min-expand=10:

                empty TU          92 functions
   before    476 MB  12.5 s     728 MB  32.7 s
   after     356 MB   7.8 s     606 MB  25.9 s

Assisted-by: Claude Opus 5.5

gcc/ChangeLog:

        * ggc.h (gt_pch_note_likely_modified): Declare.
        * ggc-common.cc: Include sbitmap.h.
        (struct ptr_data): Add seq field.
        (pch_note_seq, pch_likely_modified): New variables.
        (gt_pch_note_object): Set seq.
        (gt_pch_note_likely_modified): New function.
        (ggc_call_alloc): Rename to...
        (ggc_call_collect): ...this.  Store the object at index seq of
        state->ptrs instead of allocating space for it.
        (gt_pch_save): Reset pch_note_seq.  Record the sequence numbers
        of the objects passed to gt_pch_note_likely_modified in a bitmap.
        Allocate space for the objects in the order in which they were
        noted, those in the bitmap first.

gcc/c-family/ChangeLog:

        * c-pch.cc: Include cgraph.h.
        (c_common_write_pch): Call gt_pch_note_likely_modified for the
        declarations of the functions with a body.

Signed-off-by: Chris Burr <[email protected]>
---
  gcc/c-family/c-pch.cc |  8 ++++++
  gcc/ggc-common.cc     | 61 ++++++++++++++++++++++++++++++++++++++-----
  gcc/ggc.h             |  3 +++
  3 files changed, 66 insertions(+), 6 deletions(-)

diff --git a/gcc/c-family/c-pch.cc b/gcc/c-family/c-pch.cc
index 8976edfe851..ee18acaee04 100644
--- a/gcc/c-family/c-pch.cc
+++ b/gcc/c-family/c-pch.cc
@@ -22,6 +22,7 @@ along with GCC; see the file COPYING3.  If not see
  #include "coretypes.h"
  #include "target.h"
  #include "c-common.h"
+#include "cgraph.h"
  #include "timevar.h"
  #include "flags.h"
  #include "debug.h"
@@ -173,6 +174,13 @@ c_common_write_pch (void)
cpp_write_pch_deps (parse_in, pch_outfile); + /* The compilations using the PCH may compile the functions defined in
+     it, modifying their declarations.  */

What modifications, exactly?

+  cgraph_node *node;
+  FOR_EACH_FUNCTION (node)
+    if (DECL_SAVED_TREE (node->decl) || DECL_STRUCT_FUNCTION (node->decl))

Does checking DECL_INITIAL work instead of the other two?

Jason

Reply via email to