From patchwork Tue Feb 4 21:14:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ren=C3=A9_Scharfe?= X-Patchwork-Id: 11365253 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9ACF514B4 for ; Tue, 4 Feb 2020 21:14:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 7996D21744 for ; Tue, 4 Feb 2020 21:14:37 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=web.de header.i=@web.de header.b="i6faVX2U" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727450AbgBDVOg (ORCPT ); Tue, 4 Feb 2020 16:14:36 -0500 Received: from mout.web.de ([212.227.15.14]:51211 "EHLO mout.web.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727412AbgBDVOg (ORCPT ); Tue, 4 Feb 2020 16:14:36 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=dbaedf251592; t=1580850872; bh=kpVDWAoHKHnAbsjWOFGCtP1EjH264+p+9rzGg/9a8bE=; h=X-UI-Sender-Class:Subject:From:To:Cc:References:Date:In-Reply-To; b=i6faVX2U92R68zM9zvLTwAze+ZVoN2rXplRFvFdzBIDXo3mFeTtaPytAMgDrHudiI /5tUZWyfZWp03lfEd2AADVyUZWBV7nxu3Trfn8TQvQ7kACnHrM+My5xfAuxaUR86tP REvYdg12h8MWmXzWizMJGn6aKmZKYoyNPuuVews4= X-UI-Sender-Class: c548c8c5-30a9-4db5-a2e7-cb6cb037b8f9 Received: from [192.168.178.26] ([91.47.145.153]) by smtp.web.de (mrweb002 [213.165.67.108]) with ESMTPSA (Nemesis) id 0Lzrwh-1jclnn0dUJ-0154sg; Tue, 04 Feb 2020 22:14:32 +0100 Subject: [PATCH 01/10] name-rev: rewrite create_or_update_name() From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git Mailing List Cc: =?utf-8?q?SZEDER_G=C3=A1bor?= , =?utf-8?q?Martin_?= =?utf-8?q?=C3=85gren?= , Junio C Hamano References: <084909f8-fefa-1fe0-b2ce-74eff47c4972@web.de> Message-ID: Date: Tue, 4 Feb 2020 22:14:31 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: <084909f8-fefa-1fe0-b2ce-74eff47c4972@web.de> Content-Language: en-US X-Provags-ID: V03:K1:bXRKHDZ27tjf1WWzhNq7SnLX+qbk8SK33wAp8ffQB5JFN8Vlz81 s6TZZQCaQxFRB+kqViej1cyKJAYHY9pKE94OOwAThozE68m6e2/Hi01Ae6cTmjc7vdWAikE w/R2yZ0akR5OPdRvBIoK4Jqv110z4DqEqd36OgxA3by3T/oa6zsLBKQW7S1G9zOB4E8LQ2o CMilDBamezOMFaYYXcPzA== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:qu8Bk1NCpgE=:45dAbP6DmbvLUXwbj1ITeq ly3SG9JmQf5UgdaaPPAeuTZOtrMrTP0efRxaC6r//s/B0KQJ7wBQ5wGLOh31V6K6WTsc9MIVq OoHuEhU0QRwp5iqKAG9GHlZpwKr9hKbreORYo/f/mn6upO07RZPrwb2ZqGnaIba9HmzyV7Ew/ 9CR3Ew827tDGp8wKCxusA6sr5MtGtvrKra9cbRaDn8Auz+PCBYAz0GN7Z7Sb8roWDRUOHz4xH CtcWKA247alCPW98f/H/3Tio72VkXHKXBxfkIIk9mWfBQW4rGfcap4LatQS9OVoBOgP+fkvyv NTUKe8lLb1kFFjRtQNh497+P25UIld8BccvKvd7Y+2scbQ4HZq8CCt1eD9L7YccpN/B2/hea8 WF/2Ipfmh7raSg1gWOklrhd/KwsVHW8z2o8BMBb2PqrwyiMtWzqfyuMM0xrYAMQ/TaLjDp66O SEr9uKRbtHXiZJeD4mYrS0ZLLisaVlK05VgC2VooBLXwlU6QWoGvcWyC49fvMRdziVTIownsN rJ2TzbCdDz2MHpCMhr0S/xHUtI+wbZQSlaj+M8pyvVJIPQQb3YV4MXfSdObSxApvTEre4iA/t DPpyEfEXlBcyxQR1ytzX7MY3ROzhr2Vcksn+ayJtJ6NjfYdkhHOYSChFnWgN0LwJwGQSoiGpx 6MPmVEjQDXrtetpBAauKlhSiv3roXc0LI/fWesuZBYfJxyWuBqUYS4Fv4tsl+5IlwkfQue8OP vPGTLn39ZuOw7U4INbRTnE8RSBgE/RWwimTR1nXs0LBu2qQfGV1lbYBE2e544SKhR+FUyjfqk VHndWLQAy+hVUpXlhT6/HB3BRXevso/fDuyk20tGB2McZS/r+LDh7J0cVZRTt9D3DnObG08pL 1nkvZviJUJEX8FfT25+LciGcCUAA/NsLtIamY7xVQ2fkkgkgonLin2uYpBxKLmACQbbSSPDW3 2Vnjpxhk+mmSFzItuZ6AJ3/yy0X3lbbAx/R+LbbguiAtkrRLK/p5xuUZymarIkAdyhR01GcAV pyd56lCdGKxqAhmKEypaeMPEPuas43isQPEWJ8+88mSEVWQMvslHpSOBl36FlxkZ9zsmAgJAX aC1eVPvcoeJ6zW9N6ED034hr4zEArhsqb6+uHLypQTEqutMJSIgxkhgbWWsx6OVTppibRem+6 hHmcAYR/fJs/PDCW/ennNKoqVIfHRNp3yeyaGKWs6hbqN7VzAWuDF894GzqnbPJ31QTWveN0s Z9Gr98udFtWRt1bQ/ Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org This code was moved straight out of name_rev(). As such, we inherited the "goto" to jump from an if into an else-if. We also inherited the fact that "nothing to do -- return NULL" is handled last. Rewrite the function to first handle the "nothing to do" case. Then we can handle the conditional allocation early before going on to populate the struct. No need for goto-ing. Signed-off-by: Martin Ågren --- Original submission: https://lore.kernel.org/git/20190922081846.14452-1-martin.agren@gmail.com/ builtin/name-rev.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) -- 2.25.0 diff --git a/builtin/name-rev.c b/builtin/name-rev.c index 6b9e8c850b..c2239ac3f7 100644 --- a/builtin/name-rev.c +++ b/builtin/name-rev.c @@ -88,21 +88,21 @@ static struct rev_name *create_or_update_name(struct commit *commit, { struct rev_name *name = get_commit_rev_name(commit); + if (name && !is_better_name(name, taggerdate, distance, from_tag)) + return NULL; + if (name == NULL) { name = xmalloc(sizeof(*name)); set_commit_rev_name(commit, name); - goto copy_data; - } else if (is_better_name(name, taggerdate, distance, from_tag)) { -copy_data: - name->tip_name = tip_name; - name->taggerdate = taggerdate; - name->generation = generation; - name->distance = distance; - name->from_tag = from_tag; - - return name; - } else - return NULL; + } + + name->tip_name = tip_name; + name->taggerdate = taggerdate; + name->generation = generation; + name->distance = distance; + name->from_tag = from_tag; + + return name; } static void name_rev(struct commit *start_commit, From patchwork Tue Feb 4 21:15:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ren=C3=A9_Scharfe?= X-Patchwork-Id: 11365255 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id AD5D714B4 for ; Tue, 4 Feb 2020 21:15:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8A0022082E for ; Tue, 4 Feb 2020 21:15:26 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=web.de header.i=@web.de header.b="SivzZHQ1" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727607AbgBDVPZ (ORCPT ); Tue, 4 Feb 2020 16:15:25 -0500 Received: from mout.web.de ([212.227.15.14]:49111 "EHLO mout.web.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727441AbgBDVPZ (ORCPT ); Tue, 4 Feb 2020 16:15:25 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=dbaedf251592; t=1580850919; bh=1+cqlKfCF5Bd2AYUM+HFWu3EYPEXWSspXtdMJw2VRQc=; h=X-UI-Sender-Class:Subject:From:To:Cc:References:Date:In-Reply-To; b=SivzZHQ1WZIq4vX7jQDsNLQ9KGeg6cjoV/Ok16Q15PxnApJa/ZI/RtaoGuVq9hBe1 2I1MzgT1ZTeEM8c3mTFwgGCybp0fzbwqSwZpisTsYgT6sQ5502hKdc1YZJGTCZEFjs 3SrxEpDrUZv1JqHk0+zzhLfx0oAlhSMsHVEe/4BQ= X-UI-Sender-Class: c548c8c5-30a9-4db5-a2e7-cb6cb037b8f9 Received: from [192.168.178.26] ([91.47.145.153]) by smtp.web.de (mrweb003 [213.165.67.108]) with ESMTPSA (Nemesis) id 0MMmwZ-1itpgO35jz-008elc; Tue, 04 Feb 2020 22:15:19 +0100 Subject: [PATCH 02/10] name-rev: remove unused typedef From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git Mailing List Cc: =?utf-8?q?SZEDER_G=C3=A1bor?= , =?utf-8?q?Martin_?= =?utf-8?q?=C3=85gren?= , Junio C Hamano References: <084909f8-fefa-1fe0-b2ce-74eff47c4972@web.de> Message-ID: Date: Tue, 4 Feb 2020 22:15:19 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: <084909f8-fefa-1fe0-b2ce-74eff47c4972@web.de> Content-Language: en-US X-Provags-ID: V03:K1:ETwL/66/LAlLcitN1y6xd/o3c6wmLb8D85KS11qLo0jL1w98ovi clpDBSkfJFp35Asrk89QGY+U8FBteS0XqPOkWv7ZfXTF65xcQgdKYJuV/PG3TIn12iyCb8V MiuUkTxErSlF4863rhvnk39Nbq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h1jlqbEYJNlSVxDoUXfu0R3oj mKcnE5R4cDFrWMvBi+hZFPgAIdQTw8HM15hMKQDgueUdjdGLOPNvucqhISNLNjeER4ex2qRDf rquMRYK8a7JQS1OKICRHbwGYc1D4ZXVwLAiLrcyg3QVFD0utBQFrVBe1p8r+K3wZN1+Xke4kC RO0vd1uG7XH6T93Jl8aaRqAwTEGRtcSRnvyxi02TAab/lUzCEaK9u9vzLEGGW9/0W6Pl9jL7H COG0RTMJxHMRV1vy7uJLAJCs1hATiRR3ypmM0C5ZlN363BontTcXJb6Nz8d8+xlMP41+heOFK QxNfkl9uEbaJ9FSkF Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The type alias became unused with bf43abc6e6 (name-rev: use sizeof(*ptr) instead of sizeof(type) in allocation, 2019-11-12); remove it. Signed-off-by: René Scharfe --- builtin/name-rev.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) -- 2.25.0 diff --git a/builtin/name-rev.c b/builtin/name-rev.c index c2239ac3f7..a8de9cc561 100644 --- a/builtin/name-rev.c +++ b/builtin/name-rev.c @@ -16,13 +16,13 @@ */ #define CUTOFF_DATE_SLOP 86400 -typedef struct rev_name { +struct rev_name { const char *tip_name; timestamp_t taggerdate; int generation; int distance; int from_tag; -} rev_name; +}; define_commit_slab(commit_rev_name, struct rev_name *); From patchwork Tue Feb 4 21:16:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ren=C3=A9_Scharfe?= X-Patchwork-Id: 11365257 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4AAC114B4 for ; Tue, 4 Feb 2020 21:16:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2AF2821744 for ; Tue, 4 Feb 2020 21:16:15 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=web.de header.i=@web.de header.b="bMyTwZWt" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727468AbgBDVQO (ORCPT ); Tue, 4 Feb 2020 16:16:14 -0500 Received: from mout.web.de ([212.227.15.4]:42711 "EHLO mout.web.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727441AbgBDVQO (ORCPT ); Tue, 4 Feb 2020 16:16:14 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=dbaedf251592; t=1580850970; bh=UYSH2Se/6B53RqsOWdElwZF7XCppbE+BnUfGY40/1bY=; h=X-UI-Sender-Class:Subject:From:To:Cc:References:Date:In-Reply-To; b=bMyTwZWtoU9Mdu/qeroUh22MLE6087WMRAwpKOE9EiIwrfL4F7Nd/gUMIGotENu9z 6rrtg48S6I9kFf12A5NrDdoSbl+bzplUGjeMYbcLwLeR8AOI25egTMsXts6RV5xZOS 5XqjcwIQvUQdVMeei+fwCygzsj59YK6gam4vRDbo= X-UI-Sender-Class: c548c8c5-30a9-4db5-a2e7-cb6cb037b8f9 Received: from [192.168.178.26] ([91.47.145.153]) by smtp.web.de (mrweb002 [213.165.67.108]) with ESMTPSA (Nemesis) id 0MhDRB-1jBvT934YT-00MMV1; Tue, 04 Feb 2020 22:16:10 +0100 Subject: [PATCH 03/10] name-rev: respect const qualifier From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git Mailing List Cc: =?utf-8?q?SZEDER_G=C3=A1bor?= , =?utf-8?q?Martin_?= =?utf-8?q?=C3=85gren?= , Junio C Hamano References: <084909f8-fefa-1fe0-b2ce-74eff47c4972@web.de> Message-ID: <9993ab3c-41c0-cf0a-fc55-72853a54a49f@web.de> Date: Tue, 4 Feb 2020 22:16:10 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: <084909f8-fefa-1fe0-b2ce-74eff47c4972@web.de> Content-Language: en-US X-Provags-ID: V03:K1:7ea2rWoaXGSj/yVHr6MZg1O9n7tetWVEI8wEKgcNZnXbFWEYEfo UOyvBqiHjgz5r9yeXCEmXpnC23vKZE/KpLhV+c5fmnbkdzDw/xpzo4aObgrnnZhd6VM4Zou fn9C7hEV6CqZV1l9I35SzLUQ14TghNo16FC+03gRnpNw+KfnmfYSKQ/SUmrqZ4Cb1SHR0nH Ul9B6jR4uZzjYqwjlxDog== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:6It5o5daLM8=:vwqaJmD9gUdRBVwwTbSqK+ heZ26nsdzf4AYr2TGxW2hC4a+UjnYltqE3cS7MLRMHpYhQwDONEA7h+L4VIUWTFI7ml8H3CXr t/e44eepVfgoA4zslPO532oPZqTdGjAmX9iQQVRqvVCb0Kq6UH3ftXRZocHfYoZaK0yJAnxbY bDdpXabRnaOl/2+Gyh0NP97W5hkDnKthYObsb4rZcLFu50VKncLDWEA0hMc+ugvyqDDGgfRRJ VL0xaYjfrw4z5B7VQl9KmhxbKPwGsGSKPFTw15Jy7WPhATiH+yo7x3i71kn+6rwJNbAtziAVN U/D9x9Tm/IxjzQaGaqEupUFGx5OSE5DsT3ezbaVRsuSDfCbbSpow9M3AA+iBVhfg7XKgRPnyA QQE2r0k+MYFGyjtJ57VSbuDTOCTFBAs8o9CQ38yqHAomhAsOiAjd8DvTAomD0Shon5FO7HeCo LKykxmUnQ1CtZeWqxWR/7CVtAPafkyQxU90QEnj9gGvgjBdMKAnNI9jhX0ceKbRkQKgZESQLQ 05latk2/HN2ydUds5aDPKXffgaCkEWuYCfqq25IQ2JFqn64n2wyhUDcOBtbyrjJmRdnX+67Mu qxcC7+joWIAs+T3iCSUHdaz7KQFIRysacl/a85JwkOpNggKibGieiHwNng4rIER9tQngKscyn 3Z5T4k3LJ1YsASRGtcND8I8wjeW1KQb0iDOQPSSNAVEFSZgSCxQiZJ5th0c6VcOrG1Df7H61c axfakHirNt3OdmFQeyoZ4sSW2S0vPd63qmwBkGtEd59LxwSN0RR+kPhr/Vp26pjRwfWvINsWI 6BWErrciNJ/+O4Ug4eTlNhcIf7fyf2h6+aOBmcAoKDBbSu+3OjIIePrU0NNyAAhlJB6ft8Q+P rWh6rw8XoDAvBusaPzXZ0qkdjw0ha+cSc3MEw1LNsLd60rfRI/Z95LRG4THdV4LYt9RlvalaM i3myI1csGlhlMBEhWdXNVCi9QmPqR4l33pt7Wzcu4qSgyDxJKYZ2epZ3bAC6OT0jOuabVGkW4 VA+4quNYK7iQFJ1oa7WfYk9bSPdtl7VrX1lrX1FCGsmlgZTy9vS3MPHyp4WygQvZ8qHSY7glH SWR0pebjsVj7lIQuJVERtyjz43NA8clUtvDAMyZQ9wHfp+r2jsIVlMBL+OpcPQTOwvwO4uMrB QkuX5xDVof/CIOfnpjYr3P055Hl61HYX1Hv1+u1RRJZgUgrUfBoIEGM3L2bnsKvS76EqU= Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Keep the const qualifier of the first parameter of get_rev_name() even when casting the object pointer to a commit pointer, and further for the parameter of get_commit_rev_name(), as all these uses are read-only. Signed-off-by: René Scharfe --- builtin/name-rev.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) -- 2.25.0 diff --git a/builtin/name-rev.c b/builtin/name-rev.c index a8de9cc561..2e6820bd5b 100644 --- a/builtin/name-rev.c +++ b/builtin/name-rev.c @@ -32,7 +32,7 @@ static struct commit_rev_name rev_names; /* How many generations are maximally preferred over _one_ merge traversal? */ #define MERGE_TRAVERSAL_WEIGHT 65535 -static struct rev_name *get_commit_rev_name(struct commit *commit) +static struct rev_name *get_commit_rev_name(const struct commit *commit) { struct rev_name **slot = commit_rev_name_peek(&rev_names, commit); @@ -357,11 +357,11 @@ static const char *get_exact_ref_match(const struct object *o) static const char *get_rev_name(const struct object *o, struct strbuf *buf) { struct rev_name *n; - struct commit *c; + const struct commit *c; if (o->type != OBJ_COMMIT) return get_exact_ref_match(o); - c = (struct commit *) o; + c = (const struct commit *) o; n = get_commit_rev_name(c); if (!n) return NULL; From patchwork Tue Feb 4 21:17:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ren=C3=A9_Scharfe?= X-Patchwork-Id: 11365259 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D6A8D14B4 for ; Tue, 4 Feb 2020 21:17:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B5D95217BA for ; Tue, 4 Feb 2020 21:17:08 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=web.de header.i=@web.de header.b="J/i3Jiu+" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727543AbgBDVRI (ORCPT ); Tue, 4 Feb 2020 16:17:08 -0500 Received: from mout.web.de ([212.227.15.4]:50387 "EHLO mout.web.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727387AbgBDVRH (ORCPT ); Tue, 4 Feb 2020 16:17:07 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=dbaedf251592; t=1580851022; bh=xCa/Kt7YUUS3xzi6O3tgpqwkGEMIuv4RXsD0SZrMh58=; h=X-UI-Sender-Class:Subject:From:To:Cc:References:Date:In-Reply-To; b=J/i3Jiu+cRzgOv99zzfJwaIzJXJTTS4mZOD/CrEb+zcb4dBWaupny2lVA18GVKTdT d7uwJcv1ZqsUe9T8WGJYWnAZK1Vwx25FnZa+ipay3O7FAEPqJNueO2gya9IzaktJ/O 7bGVF3/FVzYkf13wgqyt0Xe9h2ALGjtc0HhUaZSY= X-UI-Sender-Class: c548c8c5-30a9-4db5-a2e7-cb6cb037b8f9 Received: from [192.168.178.26] ([91.47.145.153]) by smtp.web.de (mrweb002 [213.165.67.108]) with ESMTPSA (Nemesis) id 0LfAfQ-1jIl8g1mfI-00ooJi; Tue, 04 Feb 2020 22:17:02 +0100 Subject: [PATCH 04/10] name-rev: don't leak path copy in name_ref() From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git Mailing List Cc: =?utf-8?q?SZEDER_G=C3=A1bor?= , =?utf-8?q?Martin_?= =?utf-8?q?=C3=85gren?= , Junio C Hamano References: <084909f8-fefa-1fe0-b2ce-74eff47c4972@web.de> Message-ID: <32a0a2d6-3237-24e9-c647-6624cc2a1e89@web.de> Date: Tue, 4 Feb 2020 22:17:02 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: <084909f8-fefa-1fe0-b2ce-74eff47c4972@web.de> Content-Language: en-US X-Provags-ID: V03:K1:/HMtlztfJqEhAHMiTORttagDJQPZp+u0Mp3oribW9NPuEK1L/hn Vsahs9OmYZZYRrXi+FFl1tXGZFMGd/vI6OQ/DLsUyZrH/HrrL7R9QLn9b8NHb5MJ5IVlqcr LPfozn9WA0ZBpDBy88u1r8dO/jaaHp+tn1vZMSgYYf2EsrGaPGAmyjK1OAuboSeogkuQoY0 idZXusDDqX+0Xp7pttQnQ== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:3R95lme07b8=:qdjqXsLD8veaagp2X9+QHr 0+EZ7GeG9+TXS9/QX4vTj0TZTurwr+djDc22DtMlMPpiMePxxOOnM5WffjWnL4WDWQRDCgCqG me3JWkN/uUxH7vweXxOzAg8Pu0vayzT0+w+3aLNM3xwWMd2NQh238NmrXzSeJbV3R1Vb/aQM9 XfJw4blncMN0x92txFh800Bpzt6t13yUO8FZ14JxJFm9GF0EcsDqcsk4hHlXf9nBG6NgWZju3 VG6GelbIj3EgCFxV/t2v7Hy50EEUTHHhyxWXxYW8z+3+S5ogsx1xOYNchV/uRydr+YEXYWyfs wpn3qw/LOnf3xzG/JxjJkptOv4AMil1A0phNhf7UioirlpXiAIJAsZXv66ohhgd1+eNclWPJC i+x6KublKcHjUul/UaNea1/gUI/KerNSl1YrkM9a4cMAOnlQYawDtTj+pOuXQ7n7cABPx+k8i xXnxXrHkBTBOc6dgnxgC596OANPKnShBLqcuauEk014VYiIxRcZdNk4VACd46VEBLgdyI0Z0f nHRbD6iWSe+OtMQdbOzemOFtSkqGP/4o6ZgvSnRy0dnY7mIHqzWQW9fxAKoeW6U+zj9qjI3wY VfLnDkJgbY/S7uB/wuLYJvYVSeWw2Xy+AVr2NcO63UPHdWbD0N996/dMmi84iuZdMckoVJVEn ULKIokU9Gv2OMHcGIOl8Iy4NWAdH071vvprWhnrVWd3cSkm/hCYCDZHgwTOpGYMH8FonVY/rR AnGASL5a0QufoZBSShpNqz20BbptNbxfVTPCGMrqAlIZsj+zeOIgSsmkrRE0cpzQQkHp84dLz WNCPTxGp2wEIEzrebP5lWoa3McQKiVrfrnQBBzNt34UJnA/t4JTtb11lN2QL82nLwriyTSt5E xJ66SZOqv4eW3Br30VEku3+c7v5FF/tZjM4pwQ+ztLqkLu0rRrE1wg41VnrV8rynK1AxgOjs/ KSlKWYiv+9ZMwa+sMDOsfAd2BZZ5EtIdzdUfgpZAsjHLKzC/uPZwMghXwKVijeI+TWk/hj8pZ 0Ug9VkRpoQFPFgHFcdU4FxMNjQfBNTYUNtJByxlWKs2nEadT6r3OnG+3aT/5hN0qOHH09c7SV qLZjDWCVLD5o5A/rxV2csWIM9NVIEZS8haTZWx7EQG4M3Vssza96wKsUzbZQ0kRSFcbA4tJr4 yCbZ5Hq34ElEH8hynczoYZ+MboiGjUF/iFWaXUAOAajvvA+2GqhPX/kKB/BKIApbNatzk1aDn bxoByJfM2Ghj3NLR2 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org name_ref() duplicates the path string and passes it to name_rev(), which either puts it into a commit slab or ignores it if there is already a better name, leaking it. Move the duplication to name_rev() and release the copy in the latter case. Signed-off-by: René Scharfe --- builtin/name-rev.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) -- 2.25.0 diff --git a/builtin/name-rev.c b/builtin/name-rev.c index 2e6820bd5b..3e22a0503e 100644 --- a/builtin/name-rev.c +++ b/builtin/name-rev.c @@ -121,6 +121,8 @@ static void name_rev(struct commit *start_commit, if (deref) tip_name = to_free = xstrfmt("%s^0", tip_name); + else + tip_name = to_free = xstrdup(tip_name); if (!create_or_update_name(start_commit, tip_name, taggerdate, 0, 0, from_tag)) { @@ -323,7 +325,7 @@ static int name_ref(const char *path, const struct object_id *oid, int flags, vo if (taggerdate == TIME_MAX) taggerdate = commit->date; path = name_ref_abbrev(path, can_abbreviate_output); - name_rev(commit, xstrdup(path), taggerdate, from_tag, deref); + name_rev(commit, path, taggerdate, from_tag, deref); } return 0; } From patchwork Tue Feb 4 21:20:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ren=C3=A9_Scharfe?= X-Patchwork-Id: 11365265 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7CD9F14B4 for ; Tue, 4 Feb 2020 21:20:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 5BF2621741 for ; Tue, 4 Feb 2020 21:20:49 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=web.de header.i=@web.de header.b="lbUtdlFN" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727519AbgBDVUs (ORCPT ); Tue, 4 Feb 2020 16:20:48 -0500 Received: from mout.web.de ([212.227.15.4]:57587 "EHLO mout.web.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727451AbgBDVUs (ORCPT ); Tue, 4 Feb 2020 16:20:48 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=dbaedf251592; t=1580851243; bh=Ugs9xT4jmJSyATxd70N7c0TlN6kCg3oJOj9irJvZLPA=; h=X-UI-Sender-Class:Subject:From:To:Cc:References:Date:In-Reply-To; b=lbUtdlFNgyRqkKNnRyozYrs88PfhYOIdlpv/yLWEzPSw7GhEuUtHOhv/TRgAdqvBM 3B2oPemn+VLtoqhARfU9XVR7xoqyX/ieDIiKKGNaaEZxMVENLg6rerFlh3kVGMzZPV cPzZVtA0tUYddbbI6dSNR5h9YRo5Fb6D85X9LILI= X-UI-Sender-Class: c548c8c5-30a9-4db5-a2e7-cb6cb037b8f9 Received: from [192.168.178.26] ([91.47.145.153]) by smtp.web.de (mrweb001 [213.165.67.108]) with ESMTPSA (Nemesis) id 0LhvUI-1jLUCn2OQM-00nCSP; Tue, 04 Feb 2020 22:20:43 +0100 Subject: [PATCH 05/10] name-rev: don't _peek() in create_or_update_name() From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git Mailing List Cc: =?utf-8?q?SZEDER_G=C3=A1bor?= , =?utf-8?q?Martin_?= =?utf-8?q?=C3=85gren?= , Junio C Hamano References: <084909f8-fefa-1fe0-b2ce-74eff47c4972@web.de> Message-ID: Date: Tue, 4 Feb 2020 22:20:42 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: <084909f8-fefa-1fe0-b2ce-74eff47c4972@web.de> Content-Language: en-US X-Provags-ID: V03:K1:O5ID67PRVuvNsPCY5GY+b3PWZ1uSVppRvyP4TGDEZA7IEgoDptS bMlR6HsQxpOb1f3a1DiyHbGepFhyW20qldolNtH1cMOqrkDNQNcB5TQ1XudqhiNI+FBKsMA 6oCmVDheiCjflnIAWwts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ArqrJp9FFCpJhsBgw2N83hC54pMg YTmWLhHnUxw/zceCWjKM2jJQVKFMPmBEyO97CWniRFy0VpbShDCwcV7mbD5pamXpEkq2IBrAs SXTj82OaktSNmCRdGYuCsxseAof3Xi0HXk20hiRgCR/cHOKWI4t1zuFlqo8DEfPBcDsugNy5I o/+S+2I4WaJ/wm16f85NRm8NhTK7JJy0Z23eZMGLxCfhdHovc4CQ3OqCGEuTb0cCiLQujtqr+ AadYkA3j8vApd+mjBpjrSA9xQP2yYIUtDQU9UbEzAjmqGcGqnjzcVqHuh/4Luv9OFZJL4En4B wJbPxn4iyFyieY2fW Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Look up the commit slab slot for the commit once using commit_rev_name_at() and populate it in case it is empty, instead of checking for emptiness in a separate step using commit_rev_name_peek() via get_commit_rev_name(). Signed-off-by: René Scharfe --- builtin/name-rev.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) -- 2.25.0 diff --git a/builtin/name-rev.c b/builtin/name-rev.c index 3e22a0503e..41aed436ca 100644 --- a/builtin/name-rev.c +++ b/builtin/name-rev.c @@ -39,11 +39,6 @@ static struct rev_name *get_commit_rev_name(const struct commit *commit) return slot ? *slot : NULL; } -static void set_commit_rev_name(struct commit *commit, struct rev_name *name) -{ - *commit_rev_name_at(&rev_names, commit) = name; -} - static int is_better_name(struct rev_name *name, timestamp_t taggerdate, int distance, @@ -86,15 +81,14 @@ static struct rev_name *create_or_update_name(struct commit *commit, int generation, int distance, int from_tag) { - struct rev_name *name = get_commit_rev_name(commit); + struct rev_name **slot = commit_rev_name_at(&rev_names, commit); + struct rev_name *name = *slot; if (name && !is_better_name(name, taggerdate, distance, from_tag)) return NULL; - if (name == NULL) { - name = xmalloc(sizeof(*name)); - set_commit_rev_name(commit, name); - } + if (!name) + name = *slot = xmalloc(sizeof(*name)); name->tip_name = tip_name; name->taggerdate = taggerdate; From patchwork Tue Feb 4 21:22:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ren=C3=A9_Scharfe?= X-Patchwork-Id: 11365267 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E5D271398 for ; Tue, 4 Feb 2020 21:22:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id C425521744 for ; Tue, 4 Feb 2020 21:22:44 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=web.de header.i=@web.de header.b="YJL+DP3s" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727494AbgBDVWn (ORCPT ); Tue, 4 Feb 2020 16:22:43 -0500 Received: from mout.web.de ([212.227.15.3]:58699 "EHLO mout.web.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727412AbgBDVWn (ORCPT ); Tue, 4 Feb 2020 16:22:43 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=dbaedf251592; t=1580851356; bh=8JT4afp27C8mO92OF1egi7JO9F/1B7WdWcm0SouGrDY=; h=X-UI-Sender-Class:Subject:From:To:Cc:References:Date:In-Reply-To; b=YJL+DP3s0STU8l4bsTEP4g9l8VQ7h4EwwIqQT0c56SMyTwoQ4wAPJWTEXsWNiElyP 027NiDMOP1uqxfAHVbPOmVzhcTLo4Kn4ZNtRDOlVntZAfRen5tHBM/iz76GcLzn3bX jlFe5517PeSLlMV1UkYxjzuVHr5aQoyxNVEoqf2M= X-UI-Sender-Class: c548c8c5-30a9-4db5-a2e7-cb6cb037b8f9 Received: from [192.168.178.26] ([91.47.145.153]) by smtp.web.de (mrweb003 [213.165.67.108]) with ESMTPSA (Nemesis) id 0LhNmy-1jKvwi2o88-00mceF; Tue, 04 Feb 2020 22:22:36 +0100 Subject: [PATCH 06/10] name-rev: put struct rev_name into commit slab From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git Mailing List Cc: =?utf-8?q?SZEDER_G=C3=A1bor?= , =?utf-8?q?Martin_?= =?utf-8?q?=C3=85gren?= , Junio C Hamano References: <084909f8-fefa-1fe0-b2ce-74eff47c4972@web.de> Message-ID: Date: Tue, 4 Feb 2020 22:22:36 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: <084909f8-fefa-1fe0-b2ce-74eff47c4972@web.de> Content-Language: en-US X-Provags-ID: V03:K1:5XxnMg4aEM/YN4BsbRLohtQtfc49hEKAaqEqiWAyGla0NHYypca mPp4MOxEdEttcIojRVUHLGGrsaWhgTkq3G2W/4upMxDLZe9jZqkZaFnp1DN+6VWJH9kKj05 SlZ1+MqEFb7wg1pzeA2ttIdUo4IenQuffYD613wNb8b1t5g1lUyJSf0gADg204kOoEgHV7H JCOaESDZo+0Utq131KjMA== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:SQmLENH48TA=:7ZRmjO4wvHBRLLlnCC/XcM bkXsdgd7tzi12rPyj2lvPZ1Mx/x5SZ5/EOYRmFR6nYelCTNejtbkVwLYV/wU92L+dN8f2pddL NXyXP1El08Np5+L0DHz57MkE++1uLSgfb8ILRdZZAFRNGUShlHTzWSTmabypEdCNmxXxzaHjh 3FOB+/pXa2judeY56F/EV8Ic8LzTQdYp4G+FSffxfy5GJXWC1ozEh/xK5Q1JgoCMYNH6OJg0p mgcP56tuD8Hk0Z62adak791jH74gOharS5KVhb0eFGhXsRMvIlJ7A2h1d7u1ogj6gCXeu8uj0 Qi6RWJIU3aBli8yeYEQ1w2XSj0PhmE6TEuiTCyl5aJqdu2J6qGI3hQHUGvIgb3tGm9sdzvjj7 aQpWj2kCMYdAFAqDSbtfc2FYD/btBfB3ok4DLD6/dVg8l+z3zywbyiq95rvb1NE4d8lMK3LU2 ItgBPmYmyVFBpgzWJSfR1RCj+wcJVrwJZWAdqmNXashe9NASMc5FQeVka0PHyDn8jgOzGAlQT 4/tJPQtNr1orUHumYMgoavh8/2Lc3ZO9kwKd3fOzLtTd8nmFP5/+rqdv2KWO9frFz6S7XMCB2 AqnE1eAz6WA/fbCcYNr5dBwJLBT+Lw87HLsFYa13tV4JmayRjfO9xKwZeQ+CMBJ06Eh3LjwZj b9b+5PD1X5npG1uOI7W2YIDLlUkl2YQr34+bL5lZ07Xuh9raZVG2otAkxqOCENEEK/WQJPlKI awcqje7QgWDzGKOUZBjnkqPug7tZdgW/KcrtCrMkSD5AcarjsD0wtIEyKE0lnc+/8zuV/O87F GDDSxQVMj6pXo2SaV6V4bChVouLoL4ZjfznQQgWvxBtGxrpZ3WLaDNHhXI+q59xvvmKXcFmDk IhZC4Zf0D4dBO08UrO2PVyXeWGoaEDbqwbv0C6Wdpy66TFHx5rtcWEgZI8YbFJE17OqQeMTFc 4uk9C/nlXEbbYe3/VIeLuOYRztmKfNixCLO6b8PE55zu0RuM4LWvOlzoGC7J60gNmNi7AXFhy da+DOEvfNKwIq9dbX68x/pdze2oWbhiC9uCJKvZ+/ru1MlFlAOoVegPewbUwm4TRbxbyLQDYh I/weKPge0JEHjZktkYLDhkV0fBFg7ADO99D9w0B/d0DSfbQ6ff2wHf51XFuNK3AxeihrumV7D KAGv2TuyqZ+1jaAML89WmNaj7YhXdlLp227nnjACbT+7Hi0Nv2b/TU2ws0n3iph01C12/0vC/ 783qwgWNSXCwaDESA Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org The commit slab commit_rev_name contains a pointer to a struct rev_name, and the actual struct is allocated separatly. Avoid that allocation and pointer indirection by storing the full struct in the commit slab. Use the tip_name member pointer to determine if the returned struct is initialized. Performance in the Linux repository measured with hyperfine before: Benchmark #1: ./git -C ../linux/ name-rev --all Time (mean ± σ): 953.5 ms ± 6.3 ms [User: 901.2 ms, System: 52.1 ms] Range (min … max): 945.2 ms … 968.5 ms 10 runs ... and with this patch: Benchmark #1: ./git -C ../linux/ name-rev --all Time (mean ± σ): 851.0 ms ± 3.1 ms [User: 807.4 ms, System: 43.6 ms] Range (min … max): 846.7 ms … 857.0 ms 10 runs Signed-off-by: René Scharfe --- builtin/name-rev.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) -- 2.25.0 diff --git a/builtin/name-rev.c b/builtin/name-rev.c index 41aed436ca..14381a3c64 100644 --- a/builtin/name-rev.c +++ b/builtin/name-rev.c @@ -24,7 +24,7 @@ struct rev_name { int from_tag; }; -define_commit_slab(commit_rev_name, struct rev_name *); +define_commit_slab(commit_rev_name, struct rev_name); static timestamp_t cutoff = TIME_MAX; static struct commit_rev_name rev_names; @@ -32,11 +32,16 @@ static struct commit_rev_name rev_names; /* How many generations are maximally preferred over _one_ merge traversal? */ #define MERGE_TRAVERSAL_WEIGHT 65535 +static int is_valid_rev_name(const struct rev_name *name) +{ + return name && name->tip_name; +} + static struct rev_name *get_commit_rev_name(const struct commit *commit) { - struct rev_name **slot = commit_rev_name_peek(&rev_names, commit); + struct rev_name *name = commit_rev_name_peek(&rev_names, commit); - return slot ? *slot : NULL; + return is_valid_rev_name(name) ? name : NULL; } static int is_better_name(struct rev_name *name, @@ -81,15 +86,12 @@ static struct rev_name *create_or_update_name(struct commit *commit, int generation, int distance, int from_tag) { - struct rev_name **slot = commit_rev_name_at(&rev_names, commit); - struct rev_name *name = *slot; + struct rev_name *name = commit_rev_name_at(&rev_names, commit); - if (name && !is_better_name(name, taggerdate, distance, from_tag)) + if (is_valid_rev_name(name) && + !is_better_name(name, taggerdate, distance, from_tag)) return NULL; - if (!name) - name = *slot = xmalloc(sizeof(*name)); - name->tip_name = tip_name; name->taggerdate = taggerdate; name->generation = generation; From patchwork Tue Feb 4 21:23:29 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ren=C3=A9_Scharfe?= X-Patchwork-Id: 11365269 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8FDAE14B4 for ; Tue, 4 Feb 2020 21:23:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6E72921741 for ; Tue, 4 Feb 2020 21:23:34 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=web.de header.i=@web.de header.b="LCDVB679" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727607AbgBDVXd (ORCPT ); Tue, 4 Feb 2020 16:23:33 -0500 Received: from mout.web.de ([212.227.15.3]:34363 "EHLO mout.web.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727389AbgBDVXd (ORCPT ); Tue, 4 Feb 2020 16:23:33 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=dbaedf251592; t=1580851410; bh=ISlDNV6/USXUyAwE8XS3v+S1ixiS0mw7BGPxS8KsPh0=; h=X-UI-Sender-Class:Subject:From:To:Cc:References:Date:In-Reply-To; b=LCDVB679ZRJIE/ZYWhWl5G7HZpPSITG2wakpguAdzzx1k7hfmvc3vo35x9aU+QCjS Qf1hYLfDgN+Teo4qcixljMgiAZcOwr0DoBSS2AQ0k5Kqu4iKTZxSLy9ebjR5zivqxu HLV85n8UTXvU33EbBepIPzF0PQl9irnJEpb2i6VA= X-UI-Sender-Class: c548c8c5-30a9-4db5-a2e7-cb6cb037b8f9 Received: from [192.168.178.26] ([91.47.145.153]) by smtp.web.de (mrweb001 [213.165.67.108]) with ESMTPSA (Nemesis) id 0MZleQ-1jG5M40ECk-00LSvy; Tue, 04 Feb 2020 22:23:30 +0100 Subject: [PATCH 07/10] name-rev: factor out get_parent_name() From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git Mailing List Cc: =?utf-8?q?SZEDER_G=C3=A1bor?= , =?utf-8?q?Martin_?= =?utf-8?q?=C3=85gren?= , Junio C Hamano References: <084909f8-fefa-1fe0-b2ce-74eff47c4972@web.de> Message-ID: <1d7f3936-20e0-2452-15bc-2b58c71c57ed@web.de> Date: Tue, 4 Feb 2020 22:23:29 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: <084909f8-fefa-1fe0-b2ce-74eff47c4972@web.de> Content-Language: en-US X-Provags-ID: V03:K1:FB1sTi6rQ2M6M3k7dlZbB33CvjKAbtrKo7BGz/btGmOZHmmJubO zIvLrocN4m4pmI+Piqm+oLw+4kENioX4ZEQuSyt7nJyod8E+7BL9+v3I/Wfyt1XHwca3jm0 4H2tP5nSYCI6HzSOwrI3LFhOFxIlP1mJIM/r4dZA7zptta5dyxzPL4Cj7jY31hr5Q2a/Sp/ hcRVKgue3t5vI0UGqDLvQ== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:QB1X+Mex878=:TsdPyJ35MbD0lfW2mFl9N7 PJAN5Kg2EspxjiU9kIk/wMhnDt2xBDbsBv6YSJmUOUYFqFZIFPQPYnqki4N84qg4Fy4uCimgz 9GZ8kokYsDGQfEy/M+nhGVW4LvRgAnCgVl8T+1n/VjOWo05r7WtSoNapUqx6gw4V3v6XbBu5j XiS29sek7Nlb62+NHqPQR1m9K31gLPgtwo8Sui/sKUhWXMno7GOUOBDNAvkt1EoiV2qW+32S8 PNVrBpCQOXsLoKthN62Vuq0fVEkzYXUM6W7gc3SaQX3Tgpv/Ym4Dzht4SgcPlTZ5rSxxZzVhu w6l9DhNtEhGcvDC26bfikK7kBk6ABf6tctR2DsB0053Mv4A5OicjzGY2gHZ1nUEfHG+x5ZCmt P00T18ZAc4tOaXcZjcNhWya22tvC2CFxzK0sNRFj+acnvmHG5Pn3UstLFAfcLWyDeBhDuB1nK EF/n++TTA44qiNYsfVhHiO1I4TlkrYHOn3NCw0ZokAxiG7d8pUcahWYNfdmLAOn0vyoh2xLyi xPqbSmDbc26CNB+9Ony70RDHFBPyKV2qRFlp+DIRmceGordPTmM1QepS4r7pgNQ5rR5TWHQcC vI/BZLRfa7xrjhvT7dmXB3aQ0YA03kfaT0F1zY9LSGThsyr43umDPF2ac1hgyIpMnlT4GDH0I CD0eWMReEyvH/MTRtfxuAy1cAx3rziN16vsBu5EkErcT3Bsloeuqi+L1HN/5YkU9oB+h/aWim XxZ8mWd6hNB8iwIwfafcusS5m0D0nhUVHBc14l/sx+U8y60NciIMp5naRLhfui1rI8xZmB/Ww mdzwl4Ybz5gByqVnNdkHiuSsDar2dcuaT6lOyCyO93cMPYyTaVA/jZWJir+HlcYK0oOIqXNVE b+zh153m2HjcC7+EF1wqtZZC3rt4ge50AgvJFblbjallBkrIeRY2H785k5HR6RXUaipWb6Y5O O9FIfWt4N8a9Y5FHJfKgovcVKPQuyhEoaBRCafcOqqEm5EP0XhczM4dpYHj7NCwhwZlXVFMMD ohj8KvIac6A7buxWxPzPMig2dRpKmIOCHb7zKUhp24flgwujpDcG6V2xPd6mtENyFTqcIb3Ff qZ91Zf17qWVpb5oybIok5iUl6o7AEmXRFavC1T+FTfOFfR2oyErLQ4+ws+d9KShrtdD3K9Uz6 xyvCrZ+wptXIi9gW7RBkjdROG03YFihw/BrQqxUfDm1jw6fsED+zbTMSDiCl9qoEf1tLvNmbk YFkhq8a2fhn6tl7+y Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Reduce nesting by moving code to come up with a name for the parent into its own function. Signed-off-by: René Scharfe --- builtin/name-rev.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) -- 2.25.0 diff --git a/builtin/name-rev.c b/builtin/name-rev.c index 14381a3c64..6701fb1569 100644 --- a/builtin/name-rev.c +++ b/builtin/name-rev.c @@ -101,6 +101,19 @@ static struct rev_name *create_or_update_name(struct commit *commit, return name; } +static char *get_parent_name(const struct rev_name *name, int parent_number) +{ + size_t len; + + strip_suffix(name->tip_name, "^0", &len); + if (name->generation > 0) + return xstrfmt("%.*s~%d^%d", (int)len, name->tip_name, + name->generation, parent_number); + else + return xstrfmt("%.*s^%d", (int)len, name->tip_name, + parent_number); +} + static void name_rev(struct commit *start_commit, const char *tip_name, timestamp_t taggerdate, int from_tag, int deref) @@ -148,19 +161,7 @@ static void name_rev(struct commit *start_commit, continue; if (parent_number > 1) { - size_t len; - - strip_suffix(name->tip_name, "^0", &len); - if (name->generation > 0) - new_name = xstrfmt("%.*s~%d^%d", - (int)len, - name->tip_name, - name->generation, - parent_number); - else - new_name = xstrfmt("%.*s^%d", (int)len, - name->tip_name, - parent_number); + new_name = get_parent_name(name, parent_number); generation = 0; distance = name->distance + MERGE_TRAVERSAL_WEIGHT; } else { From patchwork Tue Feb 4 21:24:24 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ren=C3=A9_Scharfe?= X-Patchwork-Id: 11365271 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3658B1398 for ; Tue, 4 Feb 2020 21:24:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 135572166E for ; Tue, 4 Feb 2020 21:24:30 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=web.de header.i=@web.de header.b="U43L8kHp" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727494AbgBDVY3 (ORCPT ); Tue, 4 Feb 2020 16:24:29 -0500 Received: from mout.web.de ([212.227.15.14]:55301 "EHLO mout.web.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727389AbgBDVY2 (ORCPT ); Tue, 4 Feb 2020 16:24:28 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=dbaedf251592; t=1580851464; bh=s8saV2LHQ3fZw6rOPr7IeIhg4dla46uNi1w9FtxC+9o=; h=X-UI-Sender-Class:Subject:From:To:Cc:References:Date:In-Reply-To; b=U43L8kHpwkjeTHEUBRtRDVDNnhO4BV1xqw+w8asV2iG803vGvbXjkUbcmMdKWO5Qs 3mI6tdlO4oPL04KEVhM2mB16e1bxO4QTcB6eDhANp6dtpfW8NqcdN7xXSTGVG89qDG 7xlnCIzWbOVaGV1vbzwvIEjmn69ob2we2ZvoJ8RM= X-UI-Sender-Class: c548c8c5-30a9-4db5-a2e7-cb6cb037b8f9 Received: from [192.168.178.26] ([91.47.145.153]) by smtp.web.de (mrweb004 [213.165.67.108]) with ESMTPSA (Nemesis) id 0Lu1Be-1jiVLC3EiU-011PsK; Tue, 04 Feb 2020 22:24:24 +0100 Subject: [PATCH 08/10] name-rev: pre-size buffer in get_parent_name() From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git Mailing List Cc: =?utf-8?q?SZEDER_G=C3=A1bor?= , =?utf-8?q?Martin_?= =?utf-8?q?=C3=85gren?= , Junio C Hamano References: <084909f8-fefa-1fe0-b2ce-74eff47c4972@web.de> Message-ID: Date: Tue, 4 Feb 2020 22:24:24 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: <084909f8-fefa-1fe0-b2ce-74eff47c4972@web.de> Content-Language: en-US X-Provags-ID: V03:K1:Fq2dItztj2NSkE7GEYcTVDDWCBqTMWR0TUERuJ+hXzwrBaktP2S f4g1CYTcKzolWdW4j0PcNkqFmd+kUH8/Oz8RSV+5vLfIh7Nh6fNa5j5Mg2ImT/Zy4aPo4m2 DYLhctk0wVQO79ZI4Sphd0G1PeHshS0Nx44xaRng5h8V/VKyfpZ55F3GU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HCLL0MJZ5pB6Q/hW83WLx3gHOQWkAcGcZTenRDrrnD73SSnSZ8WhmZ1ULdeHiKs0F/s5W NQaFb1mzuoZmf7QlLqhEr1aoVqJZXVkT3wBjfsfgIF8h/W2XyrXZm0DRyW9mU4Bi/i1Z13lnz 1NtUmZoYqAjN5IrIdVeseTJcTwnAvyeEIkZzHhJUqGalze2CLISV8ZZKswTO4wFV81sQkAV8t /Nmj0mOf1O180RR4iDnRmJaexjVg3Yt10/f+D3/YzWj44fuM9TbBsuKFZG3baCQoG2I5Y= Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org We can calculate the size of new name easily and precisely. Open-code the xstrfmt() calls and grow the buffers as needed before filling them. This provides a surprisingly large benefit when working with the Chromium repository; here are the numbers measured using hyperfine before: Benchmark #1: ./git -C ../chromium/src name-rev --all Time (mean ± σ): 5.822 s ± 0.013 s [User: 5.304 s, System: 0.516 s] Range (min … max): 5.803 s … 5.837 s 10 runs ... and with this patch: Benchmark #1: ./git -C ../chromium/src name-rev --all Time (mean ± σ): 1.527 s ± 0.003 s [User: 1.015 s, System: 0.511 s] Range (min … max): 1.524 s … 1.535 s 10 runs Signed-off-by: René Scharfe --- builtin/name-rev.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) -- 2.25.0 diff --git a/builtin/name-rev.c b/builtin/name-rev.c index 6701fb1569..793356edd1 100644 --- a/builtin/name-rev.c +++ b/builtin/name-rev.c @@ -103,15 +103,23 @@ static struct rev_name *create_or_update_name(struct commit *commit, static char *get_parent_name(const struct rev_name *name, int parent_number) { + struct strbuf sb = STRBUF_INIT; size_t len; strip_suffix(name->tip_name, "^0", &len); - if (name->generation > 0) - return xstrfmt("%.*s~%d^%d", (int)len, name->tip_name, - name->generation, parent_number); - else - return xstrfmt("%.*s^%d", (int)len, name->tip_name, - parent_number); + if (name->generation > 0) { + strbuf_grow(&sb, len + + 1 + decimal_width(name->generation) + + 1 + decimal_width(parent_number)); + strbuf_addf(&sb, "%.*s~%d^%d", (int)len, name->tip_name, + name->generation, parent_number); + } else { + strbuf_grow(&sb, len + + 1 + decimal_width(parent_number)); + strbuf_addf(&sb, "%.*s^%d", (int)len, name->tip_name, + parent_number); + } + return strbuf_detach(&sb, NULL); } static void name_rev(struct commit *start_commit, From patchwork Tue Feb 4 21:25:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ren=C3=A9_Scharfe?= X-Patchwork-Id: 11365273 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3D95F1398 for ; Tue, 4 Feb 2020 21:25:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1C77E21741 for ; Tue, 4 Feb 2020 21:25:40 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=web.de header.i=@web.de header.b="haByjRRs" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727451AbgBDVZj (ORCPT ); Tue, 4 Feb 2020 16:25:39 -0500 Received: from mout.web.de ([212.227.15.4]:33067 "EHLO mout.web.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727389AbgBDVZi (ORCPT ); Tue, 4 Feb 2020 16:25:38 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=dbaedf251592; t=1580851535; bh=BJsBSpW611sD9eUxzu0eJTtNDf9J/bJ1qXmW28W5Gns=; h=X-UI-Sender-Class:Subject:From:To:Cc:References:Date:In-Reply-To; b=haByjRRsz+HnSwgx4Sioo6S//BsSzhv/If0qRi4TgN5KWZVBZYMgcgXLnVkhocbun 9vVUfZ88S5O20vrLjvJaUViK/KSSJ2rOvIuo9XimRcs5OSB5seKAXpFiKn7dDT0ufm ke3x7O9ISx+yBe22hsYQrd/lNkCVphNC0YbaSsQI= X-UI-Sender-Class: c548c8c5-30a9-4db5-a2e7-cb6cb037b8f9 Received: from [192.168.178.26] ([91.47.145.153]) by smtp.web.de (mrweb003 [213.165.67.108]) with ESMTPSA (Nemesis) id 0LcgYZ-1jQhM80CMZ-00k89v; Tue, 04 Feb 2020 22:25:35 +0100 Subject: [PATCH 09/10] name-rev: generate name strings only if they are better From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git Mailing List Cc: =?utf-8?q?SZEDER_G=C3=A1bor?= , =?utf-8?q?Martin_?= =?utf-8?q?=C3=85gren?= , Junio C Hamano References: <084909f8-fefa-1fe0-b2ce-74eff47c4972@web.de> Message-ID: <77d1d053-8680-5cbe-9182-b6aec9e9b446@web.de> Date: Tue, 4 Feb 2020 22:25:34 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: <084909f8-fefa-1fe0-b2ce-74eff47c4972@web.de> Content-Language: en-US X-Provags-ID: V03:K1:LXo0WO379o7q4PAhSL9IHZ6gr4oNTzLWZ6S8FdKLme3oK9q3yKd oZ92wMH2sP9a8DkeGHrcMsmfXPjOobEWTHfgxtOJhBrbpOyLRMMc6m0RcQDs6mSTO+ZHpGS cwAgWYeuKXXJBlMTV40c4x8Dxj/5S0UG2BvpgEINb+teGrH+2WXsgsmkSHHoySTrY3uAMV0 UjUT405ib9nNjkAfEguMQ== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:LJM3S5/NY/k=:ewv9SnbYTAq/YaY+xJrHiU t0/5pxhcssWfu9mQlzyEpYZwvn5Cs94tuotJOEX++t3lKnnUsODe8O3RjGckHYCGn+AVjNeiV C8VRO3rSvseJqn3M+JabvVix4P1DK8wMNfCG4Dg0fL+Sa2Sc2omBxWBONWXU7/BdXkGnHSZ/e yJWm1Ok08RBw//d6KONNDghco6JnA048MxGBbgT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yDV9RU91J Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Leave setting the tip_name member of struct rev_name to callers of create_or_update_name(). This avoids allocations for names that are rejected by that function. Here's how this affects the runtime when working with a fresh clone of Git's own repository; performance numbers by hyperfine before: Benchmark #1: ./git -C ../git-pristine/ name-rev --all Time (mean ± σ): 437.8 ms ± 4.0 ms [User: 422.5 ms, System: 15.2 ms] Range (min … max): 432.8 ms … 446.3 ms 10 runs ... and with this patch: Benchmark #1: ./git -C ../git-pristine/ name-rev --all Time (mean ± σ): 408.5 ms ± 1.4 ms [User: 387.2 ms, System: 21.2 ms] Range (min … max): 407.1 ms … 411.7 ms 10 runs Signed-off-by: René Scharfe --- builtin/name-rev.c | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) -- 2.25.0 diff --git a/builtin/name-rev.c b/builtin/name-rev.c index 793356edd1..98f55bcea9 100644 --- a/builtin/name-rev.c +++ b/builtin/name-rev.c @@ -81,7 +81,6 @@ static int is_better_name(struct rev_name *name, } static struct rev_name *create_or_update_name(struct commit *commit, - const char *tip_name, timestamp_t taggerdate, int generation, int distance, int from_tag) @@ -92,7 +91,6 @@ static struct rev_name *create_or_update_name(struct commit *commit, !is_better_name(name, taggerdate, distance, from_tag)) return NULL; - name->tip_name = tip_name; name->taggerdate = taggerdate; name->generation = generation; name->distance = distance; @@ -130,22 +128,20 @@ static void name_rev(struct commit *start_commit, struct commit *commit; struct commit **parents_to_queue = NULL; size_t parents_to_queue_nr, parents_to_queue_alloc = 0; - char *to_free = NULL; + struct rev_name *start_name; parse_commit(start_commit); if (start_commit->date < cutoff) return; + start_name = create_or_update_name(start_commit, taggerdate, 0, 0, + from_tag); + if (!start_name) + return; if (deref) - tip_name = to_free = xstrfmt("%s^0", tip_name); + start_name->tip_name = xstrfmt("%s^0", tip_name); else - tip_name = to_free = xstrdup(tip_name); - - if (!create_or_update_name(start_commit, tip_name, taggerdate, 0, 0, - from_tag)) { - free(to_free); - return; - } + start_name->tip_name = xstrdup(tip_name); memset(&queue, 0, sizeof(queue)); /* Use the prio_queue as LIFO */ prio_queue_put(&queue, start_commit); @@ -161,7 +157,7 @@ static void name_rev(struct commit *start_commit, parents; parents = parents->next, parent_number++) { struct commit *parent = parents->item; - const char *new_name; + struct rev_name *parent_name; int generation, distance; parse_commit(parent); @@ -169,18 +165,23 @@ static void name_rev(struct commit *start_commit, continue; if (parent_number > 1) { - new_name = get_parent_name(name, parent_number); generation = 0; distance = name->distance + MERGE_TRAVERSAL_WEIGHT; } else { - new_name = name->tip_name; generation = name->generation + 1; distance = name->distance + 1; } - if (create_or_update_name(parent, new_name, taggerdate, - generation, distance, - from_tag)) { + parent_name = create_or_update_name(parent, taggerdate, + generation, + distance, from_tag); + if (parent_name) { + if (parent_number > 1) + parent_name->tip_name = + get_parent_name(name, + parent_number); + else + parent_name->tip_name = name->tip_name; ALLOC_GROW(parents_to_queue, parents_to_queue_nr + 1, parents_to_queue_alloc); From patchwork Tue Feb 4 21:26:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ren=C3=A9_Scharfe?= X-Patchwork-Id: 11365275 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 47A21138D for ; Tue, 4 Feb 2020 21:26:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1D63B21744 for ; Tue, 4 Feb 2020 21:26:25 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=web.de header.i=@web.de header.b="V1fvnuDa" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727519AbgBDV0Y (ORCPT ); Tue, 4 Feb 2020 16:26:24 -0500 Received: from mout.web.de ([212.227.15.14]:50643 "EHLO mout.web.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727389AbgBDV0Y (ORCPT ); Tue, 4 Feb 2020 16:26:24 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=dbaedf251592; t=1580851579; bh=+uV7DwFH2+4zlrqPt4rScavXprpVXgv/PZeWYEmHxYY=; h=X-UI-Sender-Class:Subject:From:To:Cc:References:Date:In-Reply-To; b=V1fvnuDawCY3Okh/zJOdmtrOnuQa3n5GfM4plXw+8YQb3cEFMV94vcTQyJoXZt//2 rG1O79vG3cstbUxHJeCgtjjYVucPiQLGzvnEgo9LLvjlf9ySeMh/W+gg+EXHHTDvz7 Vmz4aBqs7zE6zVZfwSDP9m/9cEPVtnPqHto3vwjQ= X-UI-Sender-Class: c548c8c5-30a9-4db5-a2e7-cb6cb037b8f9 Received: from [192.168.178.26] ([91.47.145.153]) by smtp.web.de (mrweb003 [213.165.67.108]) with ESMTPSA (Nemesis) id 0LgYRZ-1jLlOx3Dq1-00o09A; Tue, 04 Feb 2020 22:26:18 +0100 Subject: [PATCH 10/10] name-rev: release unused name strings From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git Mailing List Cc: =?utf-8?q?SZEDER_G=C3=A1bor?= , =?utf-8?q?Martin_?= =?utf-8?q?=C3=85gren?= , Junio C Hamano References: <084909f8-fefa-1fe0-b2ce-74eff47c4972@web.de> Message-ID: <4eddc458-6294-9b9c-857b-50ba484a7168@web.de> Date: Tue, 4 Feb 2020 22:26:18 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: <084909f8-fefa-1fe0-b2ce-74eff47c4972@web.de> Content-Language: en-US X-Provags-ID: V03:K1:oTQsin1l7Ui7l4xE4Sloza/yltC55JaRlpLXlb2zib5h6t7qDCU Df5yer00Jlt1LOv6iZU3OlERb9um9i+3rDAH7g4wDGgiMoLs9VXIiPtA51TAY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K1b4sT+QUvT+WCM+WuZZuj+K8Cp7G0iStOEWa7hZY4FypQ5aOEg04uovymrIr6uxx xr9kqpEELPf3mMbOkPNK/iYLZ/eBvsI2pRPaisV6b43ADw5hWNuMi3TWQ9Zh3rRYqCxiS/ErC shSJ8/iLC86awGXZJRIcP+9IpJPhtXtFKAjx8twDkaspM6LkVm3y9TwHZMfEwsg8z/TBLaAag oznUYPdIWVorwsYIA5bRVBpASD0d5KeaygvnbfYfIf1sJ/D8LBvIl1ExZgSVat85kMxEDPjro FU5uQcpTwekq/QvKCe8MfqDeRy2OGD017toZDQeGPf4ljcpbpyqxd9g04O02nkhsPa8O0i8sp LwpckaOk9qRxhQ/fS Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org name_rev() assigns a name to a commit and its parents and grandparents and so on. Commits share their name string with their first parent, which in turn does the same, recursively to the root. That saves a lot of allocations. When a better name is found, the old name is replaced, but its memory is not released. That leakage can become significant. Can we release these old strings exactly once even though they are referenced multiple times? Yes, indeed -- we can make use of the fact that name_rev() visits the ancestors of a commit after it set a new name for it and tries to update their names as well. Members of the first ancestral line have the same taggerdate and from_tag values, but a higher distance value than their child commit at generation 0. These are the only criteria used by is_better_name(). Lower distance values are considered better, so a name that is better for a child will also be better for its parent and grandparent etc. That means we can free(3) an inferior name at generation 0 and rely on name_rev() to replace all references in ancestors as well. If we do that then we need to stop using the string pointer alone to distinguish new empty rev_name slots from initialized ones, though, as it technically becomes invalid after the free(3) call -- even though its value is still different from NULL. We can check the generation value first, as empty slots will have it initialized to 0, and for the actual generation 0 we'll set a new valid name right after the create_or_update_name() call that releases the string. For the Chromium repo, releasing superceded names reduces the memory footprint of name-rev --all significantly. Here's the output of GNU time before: 0.98user 0.48system 0:01.46elapsed 99%CPU (0avgtext+0avgdata 2601812maxresident)k 0inputs+0outputs (0major+571470minor)pagefaults 0swaps ... and with this patch: 1.01user 0.26system 0:01.28elapsed 100%CPU (0avgtext+0avgdata 1559196maxresident)k 0inputs+0outputs (0major+314370minor)pagefaults 0swaps It also gets faster; hyperfine before: Benchmark #1: ./git -C ../chromium/src name-rev --all Time (mean ± σ): 1.534 s ± 0.006 s [User: 1.039 s, System: 0.494 s] Range (min … max): 1.522 s … 1.542 s 10 runs ... and with this patch: Benchmark #1: ./git -C ../chromium/src name-rev --all Time (mean ± σ): 1.338 s ± 0.006 s [User: 1.047 s, System: 0.291 s] Range (min … max): 1.327 s … 1.346 s 10 runs For the Linux repo it doesn't pay off; memory usage only gets down from: 0.76user 0.03system 0:00.80elapsed 99%CPU (0avgtext+0avgdata 292848maxresident)k 0inputs+0outputs (0major+44579minor)pagefaults 0swaps ... to: 0.78user 0.03system 0:00.81elapsed 100%CPU (0avgtext+0avgdata 284696maxresident)k 0inputs+0outputs (0major+44892minor)pagefaults 0swaps The runtime actually increases slightly from: Benchmark #1: ./git -C ../linux/ name-rev --all Time (mean ± σ): 828.8 ms ± 5.0 ms [User: 797.2 ms, System: 31.6 ms] Range (min … max): 824.1 ms … 838.9 ms 10 runs ... to: Benchmark #1: ./git -C ../linux/ name-rev --all Time (mean ± σ): 847.6 ms ± 3.4 ms [User: 807.9 ms, System: 39.6 ms] Range (min … max): 843.4 ms … 854.3 ms 10 runs Why is that? In the Chromium repo, ca. 44000 free(3) calls in create_or_update_name() release almost 1GB, while in the Linux repo 240000+ calls release a bit more than 5MB, so the average discarded name is ca. 1000x longer in the latter. Overall I think it's the right tradeoff to make, as it helps curb the memory usage in repositories with big discarded names, and the added overhead is small. Signed-off-by: René Scharfe --- builtin/name-rev.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) -- 2.25.0 diff --git a/builtin/name-rev.c b/builtin/name-rev.c index 98f55bcea9..23a639ff30 100644 --- a/builtin/name-rev.c +++ b/builtin/name-rev.c @@ -17,7 +17,7 @@ #define CUTOFF_DATE_SLOP 86400 struct rev_name { - const char *tip_name; + char *tip_name; timestamp_t taggerdate; int generation; int distance; @@ -34,7 +34,7 @@ static struct commit_rev_name rev_names; static int is_valid_rev_name(const struct rev_name *name) { - return name && name->tip_name; + return name && (name->generation || name->tip_name); } static struct rev_name *get_commit_rev_name(const struct commit *commit) @@ -87,9 +87,20 @@ static struct rev_name *create_or_update_name(struct commit *commit, { struct rev_name *name = commit_rev_name_at(&rev_names, commit); - if (is_valid_rev_name(name) && - !is_better_name(name, taggerdate, distance, from_tag)) - return NULL; + if (is_valid_rev_name(name)) { + if (!is_better_name(name, taggerdate, distance, from_tag)) + return NULL; + + /* + * This string might still be shared with ancestors + * (generation > 0). We can release it here regardless, + * because the new name that has just won will be better + * for them as well, so name_rev() will replace these + * stale pointers when it processes the parents. + */ + if (!name->generation) + free(name->tip_name); + } name->taggerdate = taggerdate; name->generation = generation; From patchwork Wed Feb 5 17:50:23 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ren=C3=A9_Scharfe?= X-Patchwork-Id: 11366857 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0605E14D5 for ; Wed, 5 Feb 2020 17:50:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id CE9F9217F4 for ; Wed, 5 Feb 2020 17:50:32 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=web.de header.i=@web.de header.b="ATloB8YO" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727389AbgBERua (ORCPT ); Wed, 5 Feb 2020 12:50:30 -0500 Received: from mout.web.de ([212.227.17.12]:49315 "EHLO mout.web.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726957AbgBERua (ORCPT ); Wed, 5 Feb 2020 12:50:30 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=dbaedf251592; t=1580925023; bh=l36ob/0OrXSOgC9js48GRTpcvC/6vJjC8ujxPLG7NjA=; h=X-UI-Sender-Class:Subject:From:To:Cc:References:Date:In-Reply-To; b=ATloB8YO5vzZBceoAK7KqWcLZHVk4nSlaqia1BDu43JN5FwG+JTJy8sh4mrWRIwmG wCUnxZwzUZkGiKmJUTr6u+pHZRE03V1rSEajLNh5okQY6Dv98ysVeIs2+LRWjuoF/q 8sim0Jzz/3TQtT89fyg9y1nhdV5XcKGGQEBIt5xM= X-UI-Sender-Class: c548c8c5-30a9-4db5-a2e7-cb6cb037b8f9 Received: from [192.168.178.26] ([91.47.145.153]) by smtp.web.de (mrweb102 [213.165.67.124]) with ESMTPSA (Nemesis) id 0MGzXS-1ilhhl2Gna-00DsYl; Wed, 05 Feb 2020 18:50:23 +0100 Subject: [PATCH 11/10] name-rev: sort tip names before applying From: =?utf-8?q?Ren=C3=A9_Scharfe?= To: Git Mailing List Cc: =?utf-8?q?SZEDER_G=C3=A1bor?= , =?utf-8?q?Martin_?= =?utf-8?q?=C3=85gren?= , Junio C Hamano , Derrick Stolee References: <084909f8-fefa-1fe0-b2ce-74eff47c4972@web.de> Message-ID: <6f1a2547-ad0d-942e-b4a0-c614da1973fd@web.de> Date: Wed, 5 Feb 2020 18:50:23 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: <084909f8-fefa-1fe0-b2ce-74eff47c4972@web.de> Content-Language: en-US X-Provags-ID: V03:K1:GqHZhjTqm/gS3FYLmlDqw0Ei8B6VRpcJ881L57UesyosRzWeFbK EjbDt5DsrdSI0Ju8VBsHUCG90zcL7mPi77smn+3/+ICL3R7cZd43ZU7+nhRuokmgny8IQ66 a29mFErggOlUqhN3Zyuq5XDIBFs029WbF5QBk2wuTBLJaXaBPTIO/XVFI4+zS0HdD637kyl Aomay4VMwWN22p7sVQwqQ== X-Spam-Flag: NO X-UI-Out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DgC+oWqXKuv35 UcvYOeO22BjsYLjNq96hb4DcJ85ACCCgIyGBH0n7TYI71otPFZzJA8FmMrzi3NCjK1b+mjAP2 bOazRwgkTZ+brHAR9lfBkcDoyzeJnjz2983nO4tAxbMMoKIYAvLk3oazH2OEhQphPXhJfWSvW VCXkndSgEoDdo3Ds5AJ0NDH6SXr+i3hZABz46kUTp8dR4qNV9DMEODxiLHIg+I0WOO3HVI49b dRHbaHxhDHHketWP2 Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org name_ref() is called for each ref and checks if its a better name for the referenced commit. If that's the case it remembers it and checks if a name based on it is better for its ancestors as well. This in done in the the order for_each_ref() imposes on us. That might not be optimal. If bad names happen to be encountered first (as defined by is_better_name()), names derived from them may spread to a lot of commits, only to be replaced by better names later. Setting better names first can avoid that. is_better_name() prefers tags, short distances and old references. The distance is a measure that we need to calculate for each candidate commit, but the other two properties are not dependent on the relationships of commits. Sorting the refs by them should yield better performance than the essentially random order we currently use. And applying older references first should also help to reduce rework due to the fact that older commits have less ancestors than newer ones. So add all details of names to the tip table first, then sort them to prefer tags and older references and then apply them in this order. Here's the performance as measures by hyperfine for the Linux repo before: Benchmark #1: ./git -C ../linux/ name-rev --all Time (mean ± σ): 851.1 ms ± 4.5 ms [User: 806.7 ms, System: 44.4 ms] Range (min … max): 845.9 ms … 859.5 ms 10 runs ... and with this patch: Benchmark #1: ./git -C ../linux/ name-rev --all Time (mean ± σ): 736.2 ms ± 8.7 ms [User: 688.4 ms, System: 47.5 ms] Range (min … max): 726.0 ms … 755.2 ms 10 runs Signed-off-by: René Scharfe --- builtin/name-rev.c | 60 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 8 deletions(-) -- 2.25.0 diff --git a/builtin/name-rev.c b/builtin/name-rev.c index 23a639ff30..a9dcd25e46 100644 --- a/builtin/name-rev.c +++ b/builtin/name-rev.c @@ -247,6 +247,10 @@ static struct tip_table { struct tip_table_entry { struct object_id oid; const char *refname; + struct commit *commit; + timestamp_t taggerdate; + unsigned int from_tag:1; + unsigned int deref:1; } *table; int nr; int alloc; @@ -254,13 +258,18 @@ static struct tip_table { } tip_table; static void add_to_tip_table(const struct object_id *oid, const char *refname, - int shorten_unambiguous) + int shorten_unambiguous, struct commit *commit, + timestamp_t taggerdate, int from_tag, int deref) { refname = name_ref_abbrev(refname, shorten_unambiguous); ALLOC_GROW(tip_table.table, tip_table.nr + 1, tip_table.alloc); oidcpy(&tip_table.table[tip_table.nr].oid, oid); tip_table.table[tip_table.nr].refname = xstrdup(refname); + tip_table.table[tip_table.nr].commit = commit; + tip_table.table[tip_table.nr].taggerdate = taggerdate; + tip_table.table[tip_table.nr].from_tag = from_tag; + tip_table.table[tip_table.nr].deref = deref; tip_table.nr++; tip_table.sorted = 0; } @@ -271,12 +280,30 @@ static int tipcmp(const void *a_, const void *b_) return oidcmp(&a->oid, &b->oid); } +static int cmp_by_tag_and_age(const void *a_, const void *b_) +{ + const struct tip_table_entry *a = a_, *b = b_; + int cmp; + + /* Prefer tags. */ + cmp = b->from_tag - a->from_tag; + if (cmp) + return cmp; + + /* Older is better. */ + if (a->taggerdate < b->taggerdate) + return -1; + return a->taggerdate != b->taggerdate; +} + static int name_ref(const char *path, const struct object_id *oid, int flags, void *cb_data) { struct object *o = parse_object(the_repository, oid); struct name_ref_data *data = cb_data; int can_abbreviate_output = data->tags_only && data->name_only; int deref = 0; + int from_tag = 0; + struct commit *commit = NULL; timestamp_t taggerdate = TIME_MAX; if (data->tags_only && !starts_with(path, "refs/tags/")) @@ -325,8 +352,6 @@ static int name_ref(const char *path, const struct object_id *oid, int flags, vo return 0; } - add_to_tip_table(oid, path, can_abbreviate_output); - while (o && o->type == OBJ_TAG) { struct tag *t = (struct tag *) o; if (!t->tagged) @@ -336,17 +361,35 @@ static int name_ref(const char *path, const struct object_id *oid, int flags, vo taggerdate = t->date; } if (o && o->type == OBJ_COMMIT) { - struct commit *commit = (struct commit *)o; - int from_tag = starts_with(path, "refs/tags/"); - + commit = (struct commit *)o; + from_tag = starts_with(path, "refs/tags/"); if (taggerdate == TIME_MAX) taggerdate = commit->date; - path = name_ref_abbrev(path, can_abbreviate_output); - name_rev(commit, path, taggerdate, from_tag, deref); } + + add_to_tip_table(oid, path, can_abbreviate_output, commit, taggerdate, + from_tag, deref); return 0; } +static void name_tips(void) +{ + int i; + + /* + * Try to set better names first, so that worse ones spread + * less. + */ + QSORT(tip_table.table, tip_table.nr, cmp_by_tag_and_age); + for (i = 0; i < tip_table.nr; i++) { + struct tip_table_entry *e = &tip_table.table[i]; + if (e->commit) { + name_rev(e->commit, e->refname, e->taggerdate, + e->from_tag, e->deref); + } + } +} + static const unsigned char *nth_tip_table_ent(size_t ix, void *table_) { struct tip_table_entry *table = table_; @@ -559,6 +602,7 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix) cutoff = TIME_MIN; } for_each_ref(name_ref, &data); + name_tips(); if (transform_stdin) { char buffer[2048];