Commit a56e2f69 by Alexandre Oliva Committed by Alexandre Oliva

[PR c++/80290] recycle tinst garbage sooner

tinst_level objects are created during template instantiation, and
they're most often quite short-lived, but since there's no intervening
garbage collection, they accumulate throughout the pass while most by
far could be recycled after a short while.  The original testcase in
PR80290, for example, creates almost 55 million tinst_level objects,
all but 10 thousand of them without intervening GC, but we don't need
more than 284 of them live at a time.

Furthermore, in many cases, TREE_LIST objects are created to stand for
the decl in tinst_level.  In most cases, those can be recycled as soon
as the tinst_level object is recycled; in some relatively common
cases, they are modified and reused in up to 3 tinst_level objects.
In the same testcase, TREE_LISTs are used in all but 3 thousand of the
tinst_level objects, and we don't need more than 89 tinst_level
objects with TREE_LISTs live at a time.  Furthermore, all but 2
thousand of those are created without intervening GC.

This patch arranges for tinst_level objects to be refcounted (I've
seen as many as 20 live references to a single tinst_level object in
my testing), and for pending_template, tinst_level and TREE_LIST
objects that can be recycled to be added to freelists; that's much
faster than ggc_free()ing them and reallocating them shortly
thereafter.  In fact, the introduction of such freelists is what makes
this entire patch lighter-weight, when it comes to memory use, and
faster.  With refcounting alone, the total memory footprint was still
about the same, and we spent more time.

In order to further reduce memory use, I've arranged for us to create
TREE_LISTs lazily, only at points that really need them (when printing
error messages).  This grows tinst_level by an additional pointer, but
since a TREE_LIST holds a lot more than an extra pointer, and
tinst_levels with TREE_LISTs used to be allocated tens of thousands of
times more often than plain decl ones, we still save memory overall.

I was still not quite happy about growing memory use in cases that
used template classes but not template overload resolution, so I
changed the type of the errors field to unsigned short, from int.
With that change, in_system_header_p and refcount move into the same
word or half-word that used to hold errors, releasing a full word,
bringing tinst_level back to its original size, now without any
padding.

The errors field is only used to test whether we've had any errors
since the expansion of some template, to skip the expansion of further
templates.  If we get 2^16 errors or more, it is probably reasonable
to refrain from expanding further templates, even if we would expand
them before this change.

With these changes, compile time for the original testcase at -O0,
with default checking enabled, is cut down by some 3.7%, total GCable
memory allocation is cut down by almost 20%, and total memory use (as
reported by GNU time as maxresident) is cut down by slightly over 15%.


for  gcc/cp/ChangeLog

	PR c++/80290
	* cp-tree.h (struct tinst_level): Split decl into tldcl and
	targs.  Add private split_list_p, tree_list_p, and not_list_p
	inline const predicates; to_list private member function
	declaration; free public member function declaration; list_p,
	get_node and maybe_get_node accessors, and refcount data
	member.  Narrow errors to unsigned short.
	* error.c (print_instantiation_full_context): Use new
	accessors.
	(print_instantiation_partial_context_line): Likewise.  Drop
	const from tinst_level-typed parameter.
	* mangle.c (mangle_decl_string): Likewise.
	* pt.c (freelist): New template class.
	(tree_list_freelist_head): New var.
	(tree_list_freelist): New fn, along with specializations.
	(tinst_level_freelist_head): New var.
	(pending_template_freelist_head): Likewise.
	(tinst_level_freelist, pending_template_freelist): New fns.
	(tinst_level::to_list, tinst_level::free): Define.
	(inc_refcount_use, dec_refcount_use): New fns for tinst_level.
	(set_refcount_ptr): New template fn.
	(add_pending_template): Adjust for refcounting, freelists and
	new accessors.
	(neglectable_inst_p): Take a NULL d as a non-DECL.
	(limit_bad_template_recursion): Use new accessors.
	(push_tinst_level): New overload to create the list.
	(push_tinst_level_loc): Make it static, split decl into two
	args, adjust tests and initialization to cope with split
	lists, use freelist, adjust for refcounting.
	(push_tinst_level_loc): New wrapper with the old interface.
	(pop_tinst_level): Adjust for refcounting.
	(record_last_problematic_instantiation): Likewise.
	(reopen_tinst_level): Likewise.  Use new accessors.
	(instantiate_alias_template): Adjust for split list.
	(fn_type_unification): Likewise.
	(get_partial_spec_bindings): Likewise.
	(instantiate_pending_templates): Use new accessors.  Adjust
	for refcount.  Release pending_template to freelist.
	(instantiating_current_function_p): Use new accessors.

From-SVN: r259457
parent e66e5d5f
2018-04-18 Alexandre Oliva <aoliva@redhat.com>
PR c++/80290
* cp-tree.h (struct tinst_level): Split decl into tldcl and
targs. Add private split_list_p, tree_list_p, and not_list_p
inline const predicates; to_list private member function
declaration; free public member function declaration; list_p,
get_node and maybe_get_node accessors, and refcount data
member. Narrow errors to unsigned short.
* error.c (print_instantiation_full_context): Use new
accessors.
(print_instantiation_partial_context_line): Likewise. Drop
const from tinst_level-typed parameter.
* mangle.c (mangle_decl_string): Likewise.
* pt.c (freelist): New template class.
(tree_list_freelist_head): New var.
(tree_list_freelist): New fn, along with specializations.
(tinst_level_freelist_head): New var.
(pending_template_freelist_head): Likewise.
(tinst_level_freelist, pending_template_freelist): New fns.
(tinst_level::to_list, tinst_level::free): Define.
(inc_refcount_use, dec_refcount_use): New fns for tinst_level.
(set_refcount_ptr): New template fn.
(add_pending_template): Adjust for refcounting, freelists and
new accessors.
(neglectable_inst_p): Take a NULL d as a non-DECL.
(limit_bad_template_recursion): Use new accessors.
(push_tinst_level): New overload to create the list.
(push_tinst_level_loc): Make it static, split decl into two
args, adjust tests and initialization to cope with split
lists, use freelist, adjust for refcounting.
(push_tinst_level_loc): New wrapper with the old interface.
(pop_tinst_level): Adjust for refcounting.
(record_last_problematic_instantiation): Likewise.
(reopen_tinst_level): Likewise. Use new accessors.
(instantiate_alias_template): Adjust for split list.
(fn_type_unification): Likewise.
(get_partial_spec_bindings): Likewise.
(instantiate_pending_templates): Use new accessors. Adjust
for refcount. Release pending_template to freelist.
(instantiating_current_function_p): Use new accessors.
2018-04-16 Alexandre Oliva <aoliva@redhat.com> 2018-04-16 Alexandre Oliva <aoliva@redhat.com>
PR c++/85039 PR c++/85039
......
...@@ -5870,19 +5870,71 @@ struct GTY((chain_next ("%h.next"))) tinst_level { ...@@ -5870,19 +5870,71 @@ struct GTY((chain_next ("%h.next"))) tinst_level {
/* The immediately deeper level in the chain. */ /* The immediately deeper level in the chain. */
struct tinst_level *next; struct tinst_level *next;
/* The original node. Can be either a DECL (for a function or static /* The original node. TLDCL can be a DECL (for a function or static
data member) or a TYPE (for a class), depending on what we were data member), a TYPE (for a class), depending on what we were
asked to instantiate. */ asked to instantiate, or a TREE_LIST with the template as PURPOSE
tree decl; and the template args as VALUE, if we are substituting for
overload resolution. In all these cases, TARGS is NULL.
However, to avoid creating TREE_LIST objects for substitutions if
we can help, we store PURPOSE and VALUE in TLDCL and TARGS,
respectively. So TLDCL stands for TREE_LIST or DECL (the
template is a DECL too), whereas TARGS stands for the template
arguments. */
tree tldcl, targs;
private:
/* Return TRUE iff the original node is a split list. */
bool split_list_p () const { return targs; }
/* Return TRUE iff the original node is a TREE_LIST object. */
bool tree_list_p () const
{
return !split_list_p () && TREE_CODE (tldcl) == TREE_LIST;
}
/* Return TRUE iff the original node is not a list, split or not. */
bool not_list_p () const
{
return !split_list_p () && !tree_list_p ();
}
/* Convert (in place) the original node from a split list to a
TREE_LIST. */
tree to_list ();
public:
/* Release storage for OBJ and node, if it's a TREE_LIST. */
static void free(tinst_level *obj);
/* Return TRUE iff the original node is a list, split or not. */
bool list_p () const { return !not_list_p (); }
/* Return the original node; if it's a split list, make it a
TREE_LIST first, so that it can be returned as a single tree
object. */
tree get_node () {
if (!split_list_p ()) return tldcl;
else return to_list ();
}
/* Return the original node if it's a DECL or a TREE_LIST, but do
NOT convert a split list to a TREE_LIST: return NULL instead. */
tree maybe_get_node () const {
if (!split_list_p ()) return tldcl;
else return NULL_TREE;
}
/* The location where the template is instantiated. */ /* The location where the template is instantiated. */
location_t locus; location_t locus;
/* errorcount+sorrycount when we pushed this level. */ /* errorcount+sorrycount when we pushed this level. */
int errors; unsigned short errors;
/* True if the location is in a system header. */ /* True if the location is in a system header. */
bool in_system_header_p; bool in_system_header_p;
/* Count references to this object. */
unsigned char refcount;
}; };
bool decl_spec_seq_has_spec_p (const cp_decl_specifier_seq *, cp_decl_spec); bool decl_spec_seq_has_spec_p (const cp_decl_specifier_seq *, cp_decl_spec);
......
...@@ -3457,11 +3457,11 @@ print_instantiation_full_context (diagnostic_context *context) ...@@ -3457,11 +3457,11 @@ print_instantiation_full_context (diagnostic_context *context)
if (p) if (p)
{ {
pp_verbatim (context->printer, pp_verbatim (context->printer,
TREE_CODE (p->decl) == TREE_LIST p->list_p ()
? _("%s: In substitution of %qS:\n") ? _("%s: In substitution of %qS:\n")
: _("%s: In instantiation of %q#D:\n"), : _("%s: In instantiation of %q#D:\n"),
LOCATION_FILE (location), LOCATION_FILE (location),
p->decl); p->get_node ());
location = p->locus; location = p->locus;
p = p->next; p = p->next;
...@@ -3475,7 +3475,7 @@ print_instantiation_full_context (diagnostic_context *context) ...@@ -3475,7 +3475,7 @@ print_instantiation_full_context (diagnostic_context *context)
static void static void
print_instantiation_partial_context_line (diagnostic_context *context, print_instantiation_partial_context_line (diagnostic_context *context,
const struct tinst_level *t, struct tinst_level *t,
location_t loc, bool recursive_p) location_t loc, bool recursive_p)
{ {
if (loc == UNKNOWN_LOCATION) if (loc == UNKNOWN_LOCATION)
...@@ -3492,18 +3492,18 @@ print_instantiation_partial_context_line (diagnostic_context *context, ...@@ -3492,18 +3492,18 @@ print_instantiation_partial_context_line (diagnostic_context *context,
if (t != NULL) if (t != NULL)
{ {
if (TREE_CODE (t->decl) == TREE_LIST) if (t->list_p ())
pp_verbatim (context->printer, pp_verbatim (context->printer,
recursive_p recursive_p
? _("recursively required by substitution of %qS\n") ? _("recursively required by substitution of %qS\n")
: _("required by substitution of %qS\n"), : _("required by substitution of %qS\n"),
t->decl); t->get_node ());
else else
pp_verbatim (context->printer, pp_verbatim (context->printer,
recursive_p recursive_p
? _("recursively required from %q#D\n") ? _("recursively required from %q#D\n")
: _("required from %q#D\n"), : _("required from %q#D\n"),
t->decl); t->get_node ());
} }
else else
{ {
......
...@@ -3777,7 +3777,7 @@ mangle_decl_string (const tree decl) ...@@ -3777,7 +3777,7 @@ mangle_decl_string (const tree decl)
if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl)) if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
{ {
struct tinst_level *tl = current_instantiation (); struct tinst_level *tl = current_instantiation ();
if ((!tl || tl->decl != decl) if ((!tl || tl->maybe_get_node () != decl)
&& push_tinst_level (decl)) && push_tinst_level (decl))
{ {
template_p = true; template_p = true;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment