From patchwork Wed Sep 7 01:44:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1675070 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=uVFkCOQo; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4MMlT76yqgz1yhP for ; Wed, 7 Sep 2022 11:45:47 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 59126388450C for ; Wed, 7 Sep 2022 01:45:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 59126388450C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1662515142; bh=sTBvqPc3wk28L8iRNb7QaWNe9nIKAieR2TRwtrwuh5g=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=uVFkCOQowxNxao4L19jE7rCyNzU0J69NvsHC4LKX9WPPO9UAAXGZl3LDqAO9eXJlJ Ud56xpDQ18MDWkzLYnV4H8ngx2eqmfLvmkGAkX8SAtaNEOiLSI9bWEC0Id1itoh+ML sfdgSFAJoYjzHpnCDgP5HaptWq3dKr+MUA+qZq5k= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 2D7AF384D188 for ; Wed, 7 Sep 2022 01:44:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2D7AF384D188 Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-382-g0CJzgVNMSSng_M2J-c-Ug-1; Tue, 06 Sep 2022 21:44:47 -0400 X-MC-Unique: g0CJzgVNMSSng_M2J-c-Ug-1 Received: by mail-qv1-f72.google.com with SMTP id y16-20020a0cec10000000b004a5df9e16c6so4957498qvo.1 for ; Tue, 06 Sep 2022 18:44:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date; bh=sTBvqPc3wk28L8iRNb7QaWNe9nIKAieR2TRwtrwuh5g=; b=DGuFdvctJlYBsEscU1QsaNwpDbxSey1RvmaTufv97RRO7Y3vqRoIWMShbcLcTdzYT4 BNoRNWjqbxHq1buHs6TPT51Zq6fMshOCa2w2D6z/nI2lEcmriUIAiTJKgACaNeqeiRkw QWVGuua5MjXWPy8nLntAKF1Ynu6eh9LEmvbSnLNG1sQ+T+AJRY2KUnmi20/shIB7RMh8 dO401D/uqLHHt7JekeHF7gf5eFkp4NPQEwkeNc622axxFGffbCBBwBZIaO4cewwKvb24 5jPqXHUEKwMtPGv3H7hfH32nFJtIimNcekpGCq3a7je6vgvNBF0Zp4d3wo00DVUeVK3O M5bA== X-Gm-Message-State: ACgBeo2jU29sBcNth48j0kXuLYsVWyRihr8MzefR6PFBb1kbV2scLPni hAprUKYAfOPqr6/I0/pv3yssyBNQT4ItozguP8WduxfItwLOP1S3qBDLjF9/PAfW1VoEi20jmEP jr9MofgrbGe1bopuJiAVf1C7XVnMrqg4zDwGpTqdYD5K2YlyEt3RnJWasm7sBwl+Ppg== X-Received: by 2002:a05:622a:1305:b0:344:9050:79dc with SMTP id v5-20020a05622a130500b00344905079dcmr1326443qtk.335.1662515087092; Tue, 06 Sep 2022 18:44:47 -0700 (PDT) X-Google-Smtp-Source: AA6agR6+1eVMPW6m2wBy2DmEhissp1hDb59+pqzXLN/DFicAxolwI0PY27KEksaMTl86FieQOs07RA== X-Received: by 2002:a05:622a:1305:b0:344:9050:79dc with SMTP id v5-20020a05622a130500b00344905079dcmr1326424qtk.335.1662515086751; Tue, 06 Sep 2022 18:44:46 -0700 (PDT) Received: from barrymore.redhat.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id n76-20020a37404f000000b006baed2827f2sm12217666qka.44.2022.09.06.18.44.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Sep 2022 18:44:46 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH RFA] libstdc++: small dynamic_cast optimization Date: Tue, 6 Sep 2022 21:44:44 -0400 Message-Id: <20220907014444.1469136-1-jason@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Cc: libstdc++@gcc.gnu.org Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" An email discussion of optimizing EH led me to wonder why we weren't doing this already. Not that this change affects EH at all. Tested x86_64-pc-linux-gnu. Does this seem worthwhile? libstdc++-v3/ChangeLog: * libsupc++/dyncast.cc (__dynamic_cast): Avoid virtual function call in simple success case. --- libstdc++-v3/libsupc++/dyncast.cc | 6 ++++++ 1 file changed, 6 insertions(+) base-commit: 0a2fba3697411c07a8330abfe7460ce62bce5e7f diff --git a/libstdc++-v3/libsupc++/dyncast.cc b/libstdc++-v3/libsupc++/dyncast.cc index 853c911a4cf..616e4c05766 100644 --- a/libstdc++-v3/libsupc++/dyncast.cc +++ b/libstdc++-v3/libsupc++/dyncast.cc @@ -71,6 +71,12 @@ __dynamic_cast (const void *src_ptr, // object started from if (whole_prefix->whole_type != whole_type) return NULL; + // Avoid virtual function call in the simple success case. + if (src2dst >= 0 + && src2dst == -prefix->whole_object + && *whole_type == *dst_type) + return const_cast (whole_ptr); + whole_type->__do_dyncast (src2dst, __class_type_info::__contained_public, dst_type, whole_ptr, src_type, src_ptr, result); if (!result.dst_ptr)