From patchwork Thu Sep 26 21:24:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1989958 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=K20/RrFm; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XF67M6d0Sz1xtG for ; Fri, 27 Sep 2024 07:24:47 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A86B73858408 for ; Thu, 26 Sep 2024 21:24:45 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id CCB093858D28 for ; Thu, 26 Sep 2024 21:24:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CCB093858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org CCB093858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727385868; cv=none; b=L7A2ilsQ9qY5SAqymKmG+A3G5lHYGc/Dg7bX+CbNuN+S5No6dM9rlYWKOj5p9JZPQT8p8xlFpALFRnlHcs242yT62K5r5esXJTvq6u/PFbP7fo3letAhtZ3xCLCe2RJ9nwQpAspCj89W8S54Uahj3YFeE89cmDNUiG0lcBEHb5o= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727385868; c=relaxed/simple; bh=0iaKlngKj1lPHuQKuaaO9/k9fEvGwdzGDbXi6tz5RR4=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=eArqmKSmmdyMB96r/M9/odJZNANxANfE5JkxZ+pGn5OZqG5DeC8noymVh/wOnfGpI8dfXYtdqSTYy9ge8VFeNyJeFvOOye9sEc8sbBmCFdj8D1hhi3hTubH/NgN1zMQXquZeFDMA+7IqWfNkfNPsPfZhsDk1vPU3HgMU8BFKV1Y= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1727385865; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=i64IZytK25NpgApco+02WK+Fee/1cJKc+dQfHw8bzv4=; b=K20/RrFmx3jm5v+pIHNQbPN2EJPmf94ww1/Fqcw0Apx7HjUISQSwnj+RVjtqrzGApXucYc Um9zoVR+XHNEBZIdF6M259NwH2i/ci2iMTmciacir/QNeq3chQy2XkOJaW+DTNBgDRqABj 4n2vK1Zv4N7sEGJpnxJLsIFyaGa6Nmk= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-219-7WXnDjPjP3Gi76ie2vI3Ng-1; Thu, 26 Sep 2024 17:24:22 -0400 X-MC-Unique: 7WXnDjPjP3Gi76ie2vI3Ng-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (unknown [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 92BAB19773F9 for ; Thu, 26 Sep 2024 21:24:21 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.61]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1F24D3003DEC; Thu, 26 Sep 2024 21:24:20 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 48QLOIhA779153 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 26 Sep 2024 23:24:18 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 48QLOHbL778617; Thu, 26 Sep 2024 23:24:17 +0200 Date: Thu, 26 Sep 2024 23:24:17 +0200 From: Jakub Jelinek To: David Malcolm , Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] diagnostic: Use vec instead of custom array reallocations for m_classification_history/m_push_list [PR116847] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Hi! diagnostic.h already relies on vec.h, it uses auto_vec in one spot. The following patch converts m_classification_history and m_push_list hand-managed arrays to vec templates. The main advantage is exponential rather than linear reallocation, e.g. with current libstdc++ headers if one includes all the standard headers there could be ~ 300 reallocations of the m_classification_history array (sure, not all of them will result in actually copying the data, but still). In addition to that it fixes some formatting issues in the code. Bootstrapped on i686-linux so far, bootstrap/regtest on x86_64-linux and i686-linux still pending, ok for trunk if it passes it? 2024-09-26 Jakub Jelinek PR libstdc++/116847 * diagnostic.h (diagnostic_option_classifier): Change type of m_classification_history from diagnostic_classification_change_t * to vec. Change type of m_push_list from int * to vec. Remove m_n_classification_history and m_n_push members. * diagnostic.cc (diagnostic_option_classifier::init): Set m_push_list to vNULL rather than nullptr. Don't initialize m_n_push. Initialize m_classification_history to vNULL. (diagnostic_option_classifier::fini): Call release () method on m_push_list instead of free on it. Call release () on m_classification_history. Don't clear m_n_push. (diagnostic_option_classifier::push): Adjust for m_push_list and m_classification_history being vectors rather than custom allocated arrays with counter. (diagnostic_option_classifier::pop): Likewise. (classify_diagnostic): Adjust for m_classification_history being vector rather than custom allocated array with counter. (update_effective_level_from_pragmas): Likewise. Jakub --- gcc/diagnostic.h.jj 2024-09-21 12:28:13.452940750 +0200 +++ gcc/diagnostic.h 2024-09-26 15:42:05.696731787 +0200 @@ -287,14 +287,10 @@ private: binary-wise or end-to-front, to find the most recent classification for a given diagnostic, given the location of the diagnostic. */ - diagnostic_classification_change_t *m_classification_history; - - /* The size of the above array. */ - int m_n_classification_history; + vec m_classification_history; /* For pragma push/pop. */ - int *m_push_list; - int m_n_push; + vec m_push_list; }; /* A bundle of options relating to printing the user's source code --- gcc/diagnostic.cc.jj 2024-09-21 12:28:13.438940942 +0200 +++ gcc/diagnostic.cc 2024-09-26 16:11:51.664502977 +0200 @@ -143,8 +143,8 @@ diagnostic_option_classifier::init (int m_classify_diagnostic = XNEWVEC (diagnostic_t, n_opts); for (int i = 0; i < n_opts; i++) m_classify_diagnostic[i] = DK_UNSPECIFIED; - m_push_list = nullptr; - m_n_push = 0; + m_push_list = vNULL; + m_classification_history = vNULL; } void @@ -152,8 +152,8 @@ diagnostic_option_classifier::fini () { XDELETEVEC (m_classify_diagnostic); m_classify_diagnostic = nullptr; - free (m_push_list); - m_n_push = 0; + m_classification_history.release (); + m_push_list.release (); } /* Save all diagnostic classifications in a stack. */ @@ -161,8 +161,7 @@ diagnostic_option_classifier::fini () void diagnostic_option_classifier::push () { - m_push_list = (int *) xrealloc (m_push_list, (m_n_push + 1) * sizeof (int)); - m_push_list[m_n_push ++] = m_n_classification_history; + m_push_list.safe_push (m_classification_history.length ()); } /* Restore the topmost classification set off the stack. If the stack @@ -173,19 +172,13 @@ diagnostic_option_classifier::pop (locat { int jump_to; - if (m_n_push) - jump_to = m_push_list [-- m_n_push]; + if (!m_push_list.is_empty ()) + jump_to = m_push_list.pop (); else jump_to = 0; - const int i = m_n_classification_history; - m_classification_history = - (diagnostic_classification_change_t *) xrealloc (m_classification_history, (i + 1) - * sizeof (diagnostic_classification_change_t)); - m_classification_history[i].location = where; - m_classification_history[i].option = jump_to; - m_classification_history[i].kind = DK_POP; - m_n_classification_history ++; + diagnostic_classification_change_t v = { where, jump_to, DK_POP }; + m_classification_history.safe_push (v); } /* Initialize the diagnostic message outputting machinery. */ @@ -880,31 +873,27 @@ classify_diagnostic (const diagnostic_co the pragmas were. */ if (where != UNKNOWN_LOCATION) { - int i; + unsigned i; /* Record the command-line status, so we can reset it back on DK_POP. */ if (old_kind == DK_UNSPECIFIED) { - old_kind = !context->option_enabled_p (option_id) - ? DK_IGNORED : DK_ANY; + old_kind = (!context->option_enabled_p (option_id) + ? DK_IGNORED : DK_ANY); m_classify_diagnostic[option_id.m_idx] = old_kind; } - for (i = m_n_classification_history - 1; i >= 0; i --) - if (m_classification_history[i].option == option_id.m_idx) + diagnostic_classification_change_t *p; + FOR_EACH_VEC_ELT_REVERSE (m_classification_history, i, p) + if (p->option == option_id.m_idx) { - old_kind = m_classification_history[i].kind; + old_kind = p->kind; break; } - i = m_n_classification_history; - m_classification_history = - (diagnostic_classification_change_t *) xrealloc (m_classification_history, (i + 1) - * sizeof (diagnostic_classification_change_t)); - m_classification_history[i].location = where; - m_classification_history[i].option = option_id.m_idx; - m_classification_history[i].kind = new_kind; - m_n_classification_history ++; + diagnostic_classification_change_t v + = { where, option_id.m_idx, new_kind }; + m_classification_history.safe_push (v); } else m_classify_diagnostic[option_id.m_idx] = new_kind; @@ -1033,7 +1022,7 @@ diagnostic_t diagnostic_option_classifier:: update_effective_level_from_pragmas (diagnostic_info *diagnostic) const { - if (m_n_classification_history <= 0) + if (m_classification_history.is_empty ()) return DK_UNSPECIFIED; /* Iterate over the locations, checking the diagnostic disposition @@ -1043,27 +1032,26 @@ update_effective_level_from_pragmas (dia for (location_t loc: diagnostic->m_iinfo.m_ilocs) { /* FIXME: Stupid search. Optimize later. */ - for (int i = m_n_classification_history - 1; i >= 0; i --) + unsigned int i; + diagnostic_classification_change_t *p; + FOR_EACH_VEC_ELT_REVERSE (m_classification_history, i, p) { - const diagnostic_classification_change_t &hist - = m_classification_history[i]; - - location_t pragloc = hist.location; + location_t pragloc = p->location; if (!linemap_location_before_p (line_table, pragloc, loc)) continue; - if (hist.kind == (int) DK_POP) + if (p->kind == (int) DK_POP) { /* Move on to the next region. */ - i = hist.option; + i = p->option; continue; } - diagnostic_option_id option = hist.option; + diagnostic_option_id option = p->option; /* The option 0 is for all the diagnostics. */ if (option == 0 || option == diagnostic->option_id) { - diagnostic_t kind = hist.kind; + diagnostic_t kind = p->kind; if (kind != DK_UNSPECIFIED) diagnostic->kind = kind; return kind;