From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca (simark.ca [158.69.221.121]) by sourceware.org (Postfix) with ESMTPS id ED4CE3851C0D for ; Sat, 23 May 2020 22:02:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org ED4CE3851C0D Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=simark.ca Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=simark@simark.ca Received: from [10.0.0.11] (173-246-6-90.qc.cable.ebox.net [173.246.6.90]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPSA id 3A2321E072; Sat, 23 May 2020 18:02:59 -0400 (EDT) Subject: Re: [PATCH] Simple speedup for DWARF CU expansion To: Tom Tromey , gdb-patches@sourceware.org References: <20200523212154.15024-1-tom@tromey.com> From: Simon Marchi Message-ID: <1596d287-5c0b-95c5-ed8f-eb6f6cdc5ecb@simark.ca> Date: Sat, 23 May 2020 18:02:58 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.8.0 MIME-Version: 1.0 In-Reply-To: <20200523212154.15024-1-tom@tromey.com> Content-Type: text/plain; charset=utf-8 Content-Language: fr Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-8.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 23 May 2020 22:03:01 -0000 On 2020-05-23 5:21 p.m., Tom Tromey wrote: > I noticed that DWARF CU expansion had an easy-to-fix hot spot: > following DIE references in dwarf2_attr. > > This patch fixes the problem by caching the referent in the die_info, > if the two DIEs are in the same CU. (This restriction avoids any > issues with CU invalidation.) > > I ran "gdb -readnow" on itself 10 times. The mean before on this > machine was 14.522 seconds; with the patch it was 13.685 seconds. > > Historically I figured that CU expansion was not very important; but I > think occasionally it results in unexpected delays for users. So, > it's probably worth fixing. I'm not sure the micro-optimization > approach is really all that good in the long run. Better, I think, > would be to read types and function bodies lazily -- this would vastly > improve performance. (I attempted the latter once and saw a 40% > speedup, IIRC.) > > The use of dwarf2_attr could also be improved. Rather than looking up > attributes individually, it would probably be better for (most) > functions to loop once over all attributes, collecting useful > information. > > Meanwhile, this patch seems reasonably worthwhile. > > gdb/ChangeLog > 2020-05-23 Tom Tromey > > * dwarf2/read.c (dwarf2_attr): Use die_info::ref. > * dwarf2/die.h (struct die_info) : New member. > --- > gdb/ChangeLog | 5 +++++ > gdb/dwarf2/die.h | 5 +++++ > gdb/dwarf2/read.c | 11 ++++++++++- > 3 files changed, 20 insertions(+), 1 deletion(-) > > diff --git a/gdb/dwarf2/die.h b/gdb/dwarf2/die.h > index 5522ebdf311..83bcdf69a17 100644 > --- a/gdb/dwarf2/die.h > +++ b/gdb/dwarf2/die.h > @@ -94,6 +94,11 @@ struct die_info > struct die_info *sibling; /* Its next sibling, if any. */ > struct die_info *parent; /* Its parent, if any. */ > > + /* If the DIE has a DW_AT_specification or DW_AT_abstract_origin, > + and the referenced DIE appears in the same CU as this DIE, then > + this caches the referenced DIE. */ > + struct die_info *ref; > + > /* An array of attributes, with NUM_ATTRS elements. There may be > zero, but it's not common and zero-sized arrays are not > sufficiently portable C. */ > diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c > index e6d08110b2a..973c6c95ded 100644 > --- a/gdb/dwarf2/read.c > +++ b/gdb/dwarf2/read.c > @@ -19556,7 +19556,16 @@ dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu) > if (!spec) > break; > > - die = follow_die_ref (die, spec, &cu); > + if (die->ref != nullptr) > + die = die->ref; > + else > + { > + struct dwarf2_cu *save_cu = cu; > + struct die_info *ref = follow_die_ref (die, spec, &cu); > + if (cu == save_cu) > + die->ref = ref; > + die = ref; > + } It wouldn't hurt to add some comment here, something like: /* Cache referred* DIE to speed up subsequent accesses. Don't cache it if it's from another CU, as the other CU could be freed/invalidated and this pointer would become invalid. */ * Or "referent", as you used in the commit message? I didn't know about this meaning of the referent word. If I just saw the code without the explanation, I probably wouldn't understand the rationale. Otherwise, LGTM. Simon