From patchwork Fri Sep 10 11:23:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Giuliano Procida X-Patchwork-Id: 44919 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 503AD385841B for ; Fri, 10 Sep 2021 11:23:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 503AD385841B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1631273016; bh=xhKbMzF1RwB/Xr2k0wQg4vS2fqiEI5UzY8+1xN8UCyc=; h=Date:In-Reply-To:References:Subject:To:List-Id:List-Unsubscribe: List-Archive:List-Help:List-Subscribe:From:Reply-To:Cc:From; b=nNnlO7en5PR/ofBQ0Ib0m7G+HwcVNXLkPNBGZNA0wzzpr+GHBzFy51bHMXf/qssvr lKs163hjn2dQSCIMxNF+mBZZhH9R0iwuUpVhGXd9IZd8/VA+tUuZ2HnH1tc/uDLanp K0UWb3EkyTnEW06Px95F/FTwoeo8GWCOSVpJ9pSQ= X-Original-To: libabigail@sourceware.org Delivered-To: libabigail@sourceware.org Received: from mail-qt1-x849.google.com (mail-qt1-x849.google.com [IPv6:2607:f8b0:4864:20::849]) by sourceware.org (Postfix) with ESMTPS id 3B8173858403 for ; Fri, 10 Sep 2021 11:23:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3B8173858403 Received: by mail-qt1-x849.google.com with SMTP id q19-20020ac87353000000b0029a09eca2afso4930251qtp.21 for ; Fri, 10 Sep 2021 04:23:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=xhKbMzF1RwB/Xr2k0wQg4vS2fqiEI5UzY8+1xN8UCyc=; b=cM5sVJ5L4/8cqVkoO5T/gEc314SxtOCknwFBLbXHzNJcA3iKzcdEMIMYTiazzSPuTL 69b+c6p4B9bjcs2COkAe/cTPhyl08CWfU1JStfOxc7XzdhUXblj1PetBOgMkjIOi70Nn 0/axks/PWJ6cl6XyxQyEQe1TEkQJvTpTYifN567A+IwkPu6/6aVETE+BDUHOyW5LVdmx RVb8xVA4rZBj0dXJ6BQ2j3bRgtobMJJOSUlrSrmD/cXB0hnqM/CHNDlYtuNkc+u5WYH2 VepT+roeJq9Rx8tAyVjYKPVFY6qEw8+04THhwUpYYemSq+Gjtia0mu4OsszQuZ32mjVz 39MA== X-Gm-Message-State: AOAM532fa7KS2rXc6e2NR/gCAIsmei49gOocKD2PNuGzSajJAdW7hz1/ vg3lTN7i+wKYic2rKvebY+m7OjOusa55jQHt7q2qqjUyPAQ5eouAImCgGWrIP3xkCXw43DPI8HS QZxms1z8Y6vWuN7LhOIORZgi5bzNHhcqoPYdHC8tNn5TAyEisqu5xoH2WAn+YrwU8ua+hnU8= X-Google-Smtp-Source: ABdhPJy1ThPQkprNOW4FaojLxqCnGEi4dHGf6uZG0Y6cBPkyPDBXvn2/ZGbzIlJeFmrFCPszsuhnqpWHiCvgtw== X-Received: from tef.lon.corp.google.com ([2a00:79e0:d:210:fe2b:14b7:895d:9020]) (user=gprocida job=sendgmr) by 2002:a0c:c490:: with SMTP id u16mr7862099qvi.26.1631273011738; Fri, 10 Sep 2021 04:23:31 -0700 (PDT) Date: Fri, 10 Sep 2021 12:23:22 +0100 In-Reply-To: <20210910112325.806676-1-gprocida@google.com> Message-Id: <20210910112325.806676-2-gprocida@google.com> Mime-Version: 1.0 References: <20210910112325.806676-1-gprocida@google.com> X-Mailer: git-send-email 2.33.0.309.g3052b89438-goog Subject: [PATCH 1/4] abg-writer: get_id_for_type: refactor insertion and look-up To: libabigail@sourceware.org X-Spam-Status: No, score=-21.9 required=5.0 tests=BAYES_00, DKIMWL_WL_MED, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libabigail@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Mailing list of the Libabigail project List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-Patchwork-Original-From: Giuliano Procida via Libabigail From: Giuliano Procida Reply-To: Giuliano Procida Cc: maennich@google.com, kernel-team@android.com Errors-To: libabigail-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libabigail" The insertion and look-up in get_id_for_type can be refactored. This avoids an extra round of hashing and equality tests in the case of a newly inserted type. * src/abg-writer.cc (get_id_for_type): Fold together the look-up and insertion logic by always attempting to insert a dummy value. Signed-off-by: Giuliano Procida --- src/abg-writer.cc | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/src/abg-writer.cc b/src/abg-writer.cc index 9f48dc92..d23f4fd6 100644 --- a/src/abg-writer.cc +++ b/src/abg-writer.cc @@ -421,30 +421,35 @@ public: if (c == 0) c = const_cast(t); - type_ptr_map::const_iterator it = m_type_id_map.find(c); - if (it != m_type_id_map.end()) - return it->second; + auto insertion = m_type_id_map.emplace(c, interned_string()); + interned_string& id = insertion.first->second; - switch (m_type_id_style) - { - case SEQUENCE_TYPE_ID_STYLE: - { - interned_string id = get_id_manager().get_id_with_prefix("type-id-"); - return m_type_id_map[c] = id; - } - case HASH_TYPE_ID_STYLE: + if (insertion.second) + switch (m_type_id_style) { - interned_string pretty = c->get_cached_pretty_representation(true); - size_t hash = hashing::fnv_hash(pretty); - while (!m_used_type_id_hashes.insert(hash).second) - ++hash; - std::ostringstream os; - os << std::hex << std::setfill('0') << std::setw(8) << hash; - return m_type_id_map[c] = c->get_environment()->intern(os.str()); + case SEQUENCE_TYPE_ID_STYLE: + { + id = get_id_manager().get_id_with_prefix("type-id-"); + break; + } + case HASH_TYPE_ID_STYLE: + { + interned_string pretty = c->get_cached_pretty_representation(true); + size_t hash = hashing::fnv_hash(pretty); + while (!m_used_type_id_hashes.insert(hash).second) + ++hash; + std::ostringstream os; + os << std::hex << std::setfill('0') << std::setw(8) << hash; + id = c->get_environment()->intern(os.str()); + break; + } + default: { + ABG_ASSERT_NOT_REACHED; + break; + } } - } - ABG_ASSERT_NOT_REACHED; - return interned_string(); + + return id; } string