From patchwork Tue Aug 17 08:41:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: ZheNing Hu X-Patchwork-Id: 12440927 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2999AC4320E for ; Tue, 17 Aug 2021 08:41:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 125AD60F39 for ; Tue, 17 Aug 2021 08:41:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239352AbhHQImS (ORCPT ); Tue, 17 Aug 2021 04:42:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239296AbhHQImR (ORCPT ); Tue, 17 Aug 2021 04:42:17 -0400 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46A67C0613CF for ; Tue, 17 Aug 2021 01:41:44 -0700 (PDT) Received: by mail-wr1-x430.google.com with SMTP id u16so10266472wrn.5 for ; Tue, 17 Aug 2021 01:41:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=+9WPc7uXYwJoHuuTJOk0eAugjjy3PRlOC+2S+kz5SvQ=; b=il3Y85l4RH42CoJwa0ab8L8ztwnt0+qcEHa+9Rtzfkzidx9Jxh3uQyt/cSuzmceHih 3rJ/Z1M4QIAHEcGJRDvTklmqH/Mahq98rxr+gPCKg6BpS4eKr1l4Q+y6r+WwEFYLJg9E SGRQ9LKMHWGt8oIAJ2ovWGeUr4DEaZjPxzRQHtxNSz9GCmmxURCMokSTRp7gBQhKVrMu dB2RUp3AFpUykl2zBV2Iv6Dsxs5oRvKSBE094xmZDQSTA+YkrVFD1k1DlrDyEuctx8Ul y7kBEMpaEY1jVA20ui5N1yGJ14d924TLu5pRKNNUFerG2cVMmUBDHwc8NxOYWiWfL0RA OZAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=+9WPc7uXYwJoHuuTJOk0eAugjjy3PRlOC+2S+kz5SvQ=; b=l1WRiyA69K6waXzeZ+EGdOtGph4aoMkIuOUADO49f/ZuhOyh4CQUJYBDAgVRjU3tOi q3YpwGkS5oysxA4ehKXClcKBRSjVDeVneJkHbrNnpVl18J9696Oail9HexLycdpQnwKi 1hSyLGwBirGwH+UvAZYrmiULvZmMaMaVg7WQKwbaIYNruHP7s5Too5VwzQ9896TXmkPg Kx28Aaju04xuLo7EqkNuN3CsYOYMiSHmcZlAFBpY4GvckWDNhdetS2cHtH5Lhh8WSlP9 TnC7AdCv4F34o+Vop5YbEeHuDbs8si7ksdQCdLvyrupUgf2v5MIDau7Uyn+FB9rqu0lf hHYQ== X-Gm-Message-State: AOAM530pubpAimSgE3NSD5TQLOLy3WUsjCzx2YAd31mrva6iJGh4VdhR IER6nnH26e4ob5+YhgufaOZn9elC6Wo= X-Google-Smtp-Source: ABdhPJxo09qzR6kXTB1nA0ukVd0b6K8eEa43NUcDJmn0nzLodQX96LJEuXJSx/HUYvGt2aqtWk0sPg== X-Received: by 2002:a05:6000:1187:: with SMTP id g7mr2551189wrx.280.1629189702845; Tue, 17 Aug 2021 01:41:42 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id d4sm1598399wrz.35.2021.08.17.01.41.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Aug 2021 01:41:42 -0700 (PDT) Message-Id: <14c8a0bdb8051737bfade9c761a647daac7ea12a.1629189701.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 17 Aug 2021 08:41:34 +0000 Subject: [PATCH 1/8] [GSOC] ref-filter: remove grab_oid() function Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Junio C Hamano , Christian Couder , Hariom Verma , Bagas Sanjaya , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Eric Sunshine , Philip Oakley , ZheNing Hu , ZheNing Hu Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: ZheNing Hu From: ZheNing Hu Because "atom_type == ATOM_OBJECTNAME" implies the condition of `starts_with(name, "objectname")`, "atom_type == ATOM_TREE" implies the condition of `starts_with(name, "tree")`, so the check for `starts_with(name, field)` in grab_oid() is redundant. Remove the grab_oid() from ref-filter, to reduce repeated check. Mentored-by: Christian Couder Mentored-by: Hariom Verma Signed-off-by: ZheNing Hu --- ref-filter.c | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/ref-filter.c b/ref-filter.c index 93ce2a6ef2e..e38046ca141 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -1065,16 +1065,6 @@ static const char *do_grab_oid(const char *field, const struct object_id *oid, } } -static int grab_oid(const char *name, const char *field, const struct object_id *oid, - struct atom_value *v, struct used_atom *atom) -{ - if (starts_with(name, field)) { - v->s = xstrdup(do_grab_oid(field, oid, atom)); - return 1; - } - return 0; -} - /* See grab_values */ static void grab_common_values(struct atom_value *val, int deref, struct expand_data *oi) { @@ -1100,8 +1090,9 @@ static void grab_common_values(struct atom_value *val, int deref, struct expand_ } } else if (atom_type == ATOM_DELTABASE) v->s = xstrdup(oid_to_hex(&oi->delta_base_oid)); - else if (atom_type == ATOM_OBJECTNAME && deref) - grab_oid(name, "objectname", &oi->oid, v, &used_atom[i]); + else if (atom_type == ATOM_OBJECTNAME && deref) { + v->s = xstrdup(do_grab_oid("objectname", &oi->oid, &used_atom[i])); + } } } @@ -1142,9 +1133,10 @@ static void grab_commit_values(struct atom_value *val, int deref, struct object continue; if (deref) name++; - if (atom_type == ATOM_TREE && - grab_oid(name, "tree", get_commit_tree_oid(commit), v, &used_atom[i])) + if (atom_type == ATOM_TREE) { + v->s = xstrdup(do_grab_oid("tree", get_commit_tree_oid(commit), &used_atom[i])); continue; + } if (atom_type == ATOM_NUMPARENT) { v->value = commit_list_count(commit->parents); v->s = xstrfmt("%lu", (unsigned long)v->value); @@ -1917,9 +1909,9 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err) v->s = xstrdup(buf + 1); } continue; - } else if (!deref && atom_type == ATOM_OBJECTNAME && - grab_oid(name, "objectname", &ref->objectname, v, atom)) { - continue; + } else if (!deref && atom_type == ATOM_OBJECTNAME) { + v->s = xstrdup(do_grab_oid("objectname", &ref->objectname, atom)); + continue; } else if (atom_type == ATOM_HEAD) { if (atom->u.head && !strcmp(ref->refname, atom->u.head)) v->s = xstrdup("*"); From patchwork Tue Aug 17 08:41:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: ZheNing Hu X-Patchwork-Id: 12440929 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C8B81C4338F for ; Tue, 17 Aug 2021 08:41:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B13B560FA0 for ; Tue, 17 Aug 2021 08:41:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239355AbhHQImS (ORCPT ); Tue, 17 Aug 2021 04:42:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60870 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239336AbhHQImR (ORCPT ); Tue, 17 Aug 2021 04:42:17 -0400 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD86EC061796 for ; Tue, 17 Aug 2021 01:41:44 -0700 (PDT) Received: by mail-wr1-x42f.google.com with SMTP id q10so27424501wro.2 for ; Tue, 17 Aug 2021 01:41:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=R8s6iduAzpHv2zHqb74v5m/FmCTqBSmo4RcXr5WjrbM=; b=vdFWcwLaG4UUDFKllIwOQ9K74PD6zq1SATBDWS27+rkn9Hi7oMuxdy0kFk5qFa7guW j66iq7+UDWX/9MM0MbI/1hRdIim0U/9lFcRYUOAy2v/MhSAU61IYqzKd7Mj/uIVBaDT/ Yv0HqqDhmFVBOycM4QrzMmcFFk2fyAUvfADwWsQIqS5Vnsh7Bc6WaGD7zd8NMOGmN/c7 9HljK8ER2SIWZyJ59v4EsbtRZwVUO3mwwToLsKh/13n3hjo/hg6TNNdJwHOwzuOglZwx jwhtu80NQWOlvpWHDqI7gEWtInB4hDUGKW3RxJHC4puqEppt8DtiCKKgnHH/fnHNtU2x qX9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=R8s6iduAzpHv2zHqb74v5m/FmCTqBSmo4RcXr5WjrbM=; b=g90xo+pjFgG+TjZuvRtRNZznLHdbhAX4ZDOZsUWB6roiKMvpGNZTQxTeemtZAoJvne SyHvAJ5BgGqGV4HtNSPASulhEKBpmB4DzPouy03RjIFwUWwNrlMrVfvOTyRSSvxjK9EQ eyEsWzfyl8BmjjZzGR98mAXy1W/hJgP4juwH8cqWYChoKKxPot28/YevqScL8mU2wzdR qQ5qWU8GscpuJ4gieApEjOQeJFJEJPIKKRS8YT/BYEF5UMhJpZWi1vHXQgneu0a9zDeX 8amAc1sc4DZf3ISEVulYD+nIeIQCGGdq7VVsHaka66U4/2HWS90B8L4Kaj+NTajWWXdL DKGQ== X-Gm-Message-State: AOAM533Coo3enOQA2pfQCaBqixEkBJvR8sbO03JHJiWI9ELjLzvySeas 9c6lUoeuXdzTOXZtgyDh3OPTO8u0GmI= X-Google-Smtp-Source: ABdhPJwGnFfvQSN4mWUblp2JUqZbaD+Y/sBuH+QOlPBXkd5pqtrCwf2/RSDhmW3uJpXghDI9Z9O/Lg== X-Received: by 2002:adf:9d86:: with SMTP id p6mr2644866wre.156.1629189703459; Tue, 17 Aug 2021 01:41:43 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id r1sm1652381wrt.24.2021.08.17.01.41.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Aug 2021 01:41:43 -0700 (PDT) Message-Id: <9ff78d17f3839acd24df5439fac74ba5064579c8.1629189701.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 17 Aug 2021 08:41:35 +0000 Subject: [PATCH 2/8] [GSOC] ref-filter: merge two for loop in grab_person Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Junio C Hamano , Christian Couder , Hariom Verma , Bagas Sanjaya , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Eric Sunshine , Philip Oakley , ZheNing Hu , ZheNing Hu Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: ZheNing Hu From: ZheNing Hu grab_person() uses string for atom type comparison, this is very inefficient. After the introduction of atom_type, we can use atom_type to represent the atom type. So modify the parameter type of grab_person() to atom_type, and take use of the characteristics of ATOM_AUTHOR == ATOM_AUTHORNAME - 1 == ATOM_AUTHOREMAIL - 2 == ATOM_AUTHORDATE - 3 to check which author atom type is. ATOM_COMMITTER, ATOM_TAGGER are same too. At the same time, merge the for loop which handles %(creator*) in grab_person() into the previous for loop which handles %(author*), %(committer*),%(tagger*). This can reduce unnecessary traversal of the used_atom list. Mentored-by: Christian Couder Mentored-by: Hariom Verma Signed-off-by: ZheNing Hu --- ref-filter.c | 57 +++++++++++++++++----------------------------------- 1 file changed, 18 insertions(+), 39 deletions(-) diff --git a/ref-filter.c b/ref-filter.c index e38046ca141..592b8d9bd0a 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -1275,63 +1275,42 @@ static void grab_date(const char *buf, struct atom_value *v, const char *atomnam } /* See grab_values */ -static void grab_person(const char *who, struct atom_value *val, int deref, void *buf) +static void grab_person(enum atom_type type, struct atom_value *val, int deref, void *buf) { int i; + const char *who = valid_atom[type].name; int wholen = strlen(who); const char *wholine = NULL; for (i = 0; i < used_atom_cnt; i++) { const char *name = used_atom[i].name; + enum atom_type atom_type = used_atom[i].atom_type; struct atom_value *v = &val[i]; if (!!deref != (*name == '*')) continue; if (deref) name++; - if (strncmp(who, name, wholen)) - continue; - if (name[wholen] != 0 && - strcmp(name + wholen, "name") && - !starts_with(name + wholen, "email") && - !starts_with(name + wholen, "date")) + if ((atom_type < type || atom_type > type + 3) && + /* + * For a tag or a commit object, if "creator" or "creatordate" is + * requested, do something special. + */ + ((atom_type != ATOM_CREATOR && atom_type != ATOM_CREATORDATE) || + ((atom_type == ATOM_CREATOR || atom_type == ATOM_CREATORDATE) && + type != ATOM_TAGGER && type != ATOM_COMMITTER))) continue; if (!wholine) wholine = find_wholine(who, wholen, buf); if (!wholine) return; /* no point looking for it */ - if (name[wholen] == 0) + if (atom_type == type || atom_type == ATOM_CREATOR) v->s = copy_line(wholine); - else if (!strcmp(name + wholen, "name")) + else if (atom_type == type + 1) v->s = copy_name(wholine); - else if (starts_with(name + wholen, "email")) + else if (atom_type == type + 2) v->s = copy_email(wholine, &used_atom[i]); - else if (starts_with(name + wholen, "date")) - grab_date(wholine, v, name); - } - - /* - * For a tag or a commit object, if "creator" or "creatordate" is - * requested, do something special. - */ - if (strcmp(who, "tagger") && strcmp(who, "committer")) - return; /* "author" for commit object is not wanted */ - if (!wholine) - wholine = find_wholine(who, wholen, buf); - if (!wholine) - return; - for (i = 0; i < used_atom_cnt; i++) { - const char *name = used_atom[i].name; - enum atom_type atom_type = used_atom[i].atom_type; - struct atom_value *v = &val[i]; - if (!!deref != (*name == '*')) - continue; - if (deref) - name++; - - if (atom_type == ATOM_CREATORDATE) + else if (atom_type == type + 3 || atom_type == ATOM_CREATORDATE) grab_date(wholine, v, name); - else if (atom_type == ATOM_CREATOR) - v->s = copy_line(wholine); } } @@ -1520,13 +1499,13 @@ static void grab_values(struct atom_value *val, int deref, struct object *obj, s case OBJ_TAG: grab_tag_values(val, deref, obj); grab_sub_body_contents(val, deref, data); - grab_person("tagger", val, deref, buf); + grab_person(ATOM_TAGGER, val, deref, buf); break; case OBJ_COMMIT: grab_commit_values(val, deref, obj); grab_sub_body_contents(val, deref, data); - grab_person("author", val, deref, buf); - grab_person("committer", val, deref, buf); + grab_person(ATOM_AUTHOR, val, deref, buf); + grab_person(ATOM_COMMITTER, val, deref, buf); break; case OBJ_TREE: /* grab_tree_values(val, deref, obj, buf, sz); */ From patchwork Tue Aug 17 08:41:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: ZheNing Hu X-Patchwork-Id: 12440931 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7B38BC432BE for ; Tue, 17 Aug 2021 08:41:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 685C760F39 for ; Tue, 17 Aug 2021 08:41:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239373AbhHQImU (ORCPT ); Tue, 17 Aug 2021 04:42:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60878 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239296AbhHQImS (ORCPT ); Tue, 17 Aug 2021 04:42:18 -0400 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7BD53C0613CF for ; Tue, 17 Aug 2021 01:41:45 -0700 (PDT) Received: by mail-wm1-x32d.google.com with SMTP id c8-20020a7bc008000000b002e6e462e95fso1291117wmb.2 for ; Tue, 17 Aug 2021 01:41:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=tNAa1sosZtO+JcxNmdcFZZbGrGnVZjLto8/IC2MAMQg=; b=jCh5Px79d5hklSrO41yTTwj1DmiSVdB7Yn3K7c5mjmQJeLeoM20L/wzjHVIhaxrqFA qN09BQdE3jCOBxbb2IkkgGOnXBw/We1YlCqsi4m08FvxA4SF45GjZ0D8SfrhEfbJ3INr 9cDzDCiMg/EnSkk6WBW10jkTSWrDJ+hGqOoawNToHja4EG7zGDxdOVxU99V/1NXo5Z7l 8JOoN5tC65ZsakJH99l3Vd5AHPS075bLW45WSLi51GjvbkAVWTDOx/mm4v5iPlDNMLFO /oytOcbyIRro/aJBARBNZ7Fpn57W4syrnE4Ch19VFbEK4ijpgB3Oanc/58ghsA4z53nM BI+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=tNAa1sosZtO+JcxNmdcFZZbGrGnVZjLto8/IC2MAMQg=; b=KLQt5HoIrgAx6UFlsDcRLXhc0rvXxtWtziQ2BWONM4NCvxCR05C1yz9A3RmOI0JAa5 HIjDz4eEA5a5/akr3/NIRAjDxBdjOmGkc8bGN8yUrT1A204bJIM86RCYqbZjt42/2LjU as78KrfO9Ot8Lmt/OXrl+gthdleCO4rPidR2HRXiyHpqP8g9+GSc3B+SOcn9cFL8IOPC QtcSm5vf/Edf8+1MD01prk8U8FKCsZRSIM4zUNvu5huFIJeKl4YpFRN7Jh62S52XR8kQ 35C37ZWTuXc256xf+W65Ipwdus6VqFXK+EHkEgVGt9XNHrDlSXWwG57iqtvdf7l4iBlH NnYQ== X-Gm-Message-State: AOAM530SoEnqa2jo/L47d0YSkdsprnXDi4FYnIe2HCcUs/X4JovP0TAs BwB0EvKAB32LnVeMNQB5P1tsJJpP3HU= X-Google-Smtp-Source: ABdhPJxUxRlFBqBSaj2U8BG5hLH/5qtcE2TJTscFPq/TFcOGNwgtzGLvz0oyZgQyplXC/LYmtRvHiw== X-Received: by 2002:a7b:c1cf:: with SMTP id a15mr2119363wmj.72.1629189704060; Tue, 17 Aug 2021 01:41:44 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id k1sm1618946wrz.61.2021.08.17.01.41.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Aug 2021 01:41:43 -0700 (PDT) Message-Id: In-Reply-To: References: Date: Tue, 17 Aug 2021 08:41:36 +0000 Subject: [PATCH 3/8] [GSOC] ref-filter: remove strlen from find_subpos Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Junio C Hamano , Christian Couder , Hariom Verma , Bagas Sanjaya , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Eric Sunshine , Philip Oakley , ZheNing Hu , ZheNing Hu Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: ZheNing Hu From: ZheNing Hu find_subpos() use strlen() to get the length of the object content, but in the caller of find_subpos(), grab_sub_body_contents(), we already get the length of the object content through data->size. So add a `size_t buf_size` in find_subpos(), and pass data->size to it. This can avoid unnecessary strlen() overhead. Signed-off-by: ZheNing Hu Mentored-by: Christian Couder Mentored-by: Hariom Verma --- ref-filter.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ref-filter.c b/ref-filter.c index 592b8d9bd0a..875c2e4c39c 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -1314,7 +1314,7 @@ static void grab_person(enum atom_type type, struct atom_value *val, int deref, } } -static void find_subpos(const char *buf, +static void find_subpos(const char *buf, size_t buf_size, const char **sub, size_t *sublen, const char **body, size_t *bodylen, size_t *nonsiglen, @@ -1322,12 +1322,12 @@ static void find_subpos(const char *buf, { struct strbuf payload = STRBUF_INIT; struct strbuf signature = STRBUF_INIT; + const char *begin = buf; const char *eol; - const char *end = buf + strlen(buf); const char *sigstart; /* parse signature first; we might not even have a subject line */ - parse_signature(buf, end - buf, &payload, &signature); + parse_signature(buf, buf_size, &payload, &signature); /* skip past header until we hit empty line */ while (*buf && *buf != '\n') { @@ -1340,7 +1340,7 @@ static void find_subpos(const char *buf, while (*buf == '\n') buf++; *sig = strbuf_detach(&signature, siglen); - sigstart = buf + parse_signed_buffer(buf, strlen(buf)); + sigstart = buf + parse_signed_buffer(buf, buf_size - (buf - begin)); /* subject is first non-empty line */ *sub = buf; @@ -1363,7 +1363,7 @@ static void find_subpos(const char *buf, while (*buf == '\n' || *buf == '\r') buf++; *body = buf; - *bodylen = strlen(buf); + *bodylen = buf_size - (buf - begin); *nonsiglen = sigstart - buf; } @@ -1430,7 +1430,7 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct exp !starts_with(name, "contents"))) continue; if (!subpos) - find_subpos(buf, + find_subpos(buf, data->size, &subpos, &sublen, &bodypos, &bodylen, &nonsiglen, &sigpos, &siglen); From patchwork Tue Aug 17 08:41:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: ZheNing Hu X-Patchwork-Id: 12440933 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.9 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNWANTED_LANGUAGE_BODY autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 979BBC4320E for ; Tue, 17 Aug 2021 08:41:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8001C60EFE for ; Tue, 17 Aug 2021 08:41:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239381AbhHQImV (ORCPT ); Tue, 17 Aug 2021 04:42:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60886 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239365AbhHQImT (ORCPT ); Tue, 17 Aug 2021 04:42:19 -0400 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 47656C0613C1 for ; Tue, 17 Aug 2021 01:41:46 -0700 (PDT) Received: by mail-wm1-x329.google.com with SMTP id w24so5414265wmi.5 for ; Tue, 17 Aug 2021 01:41:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=2LPza4Pdca2u9DgmCdoJioGdYNEprZiHckukoyYvxvU=; b=KTIZM+jLkxgd4IyEfZcXUay52v3aUGh79K1NadOhIbCaHT2vNTfoj86py0ZPEknHTo aqgMY+JkkQJ2G0i9/thUeMbLYd6y4d0LaxARLa92p9IHMym1/kOMHZjSHrVBeg6HsjZy GsnA5qmzFpJmycfWxVkE7+uMe0YBYDPw6sx0lrOaCPK45EYIzTt+7gOJlzBZVfDmIksU FIjYWNt7sroyGb8HyONU9F5ZIlCI7USH/hHyy9ONQie1/G8KTaJaAyJVM6uyEVXDhEH4 Q7xxxBVL+E0eaYj2qoUnqM8xlxneZKX3umNsFJkyNcBlAwWgerRFYnR0QtB5fc3wR7ZH p/Sg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=2LPza4Pdca2u9DgmCdoJioGdYNEprZiHckukoyYvxvU=; b=tX1PIggmJjpa1nDiUGUdlkLStNQWwRPaopByc+APZJ0GXG+JFSgnOUeOTr3ijMLoRE IMRZHHJ88XcR7lG6oBugDN83RrdEnoHL2nPTJFO33sZon8L/dUYwyVYSp4ARgOd/EOJX GwDbukRpkiaB/IzmCj+89lUfAGAZu9AjwraKjwq3WfH1tV3XV5Thg3F+YeYAzXYksAhw RgN53zvwE2yOWY0hLEpkuW6Kvuxm+iPA0teE1zQzYWJGXKtLzw1ILVocZO6L86cZ7Gci to74yfQsl+3mn9dW2mpp2Lyfck4mwF+3nyw2DUend9tiKNQ959sM0KWHGSOkCvFP43z3 fTtQ== X-Gm-Message-State: AOAM532hfPE2QRwW22dT0gQTyluVPOtxuMY889JyzLlthPunzJcaivYy 5P5savktqnwQokY/TBU0ryYbbEtmfrs= X-Google-Smtp-Source: ABdhPJyR0Q41mJ3qbxvo8sovipYS9FWTbCeN2yQozck8JK5w2CcsoA3jLGoJwXSfuMSukL7ti9A+Eg== X-Received: by 2002:a05:600c:1405:: with SMTP id g5mr2114125wmi.40.1629189704870; Tue, 17 Aug 2021 01:41:44 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id f2sm1579539wru.31.2021.08.17.01.41.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Aug 2021 01:41:44 -0700 (PDT) Message-Id: <38a36881f0a8db983be46c0c2bfe085d28225748.1629189701.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 17 Aug 2021 08:41:37 +0000 Subject: [PATCH 4/8] [GSOC] ref-filter: introducing xstrvfmt_len() and xstrfmt_len() Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Junio C Hamano , Christian Couder , Hariom Verma , Bagas Sanjaya , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Eric Sunshine , Philip Oakley , ZheNing Hu , ZheNing Hu Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: ZheNing Hu From: ZheNing Hu In the logic of ref-filter, if we already specified v->s_size (the length of the data), we can avoid using strlen() to calculate the length of the data in strbuf_addstr() or perl_quote_buf_with_len()... xstrfmt(), xstrvfmt(), they are functions used to add format strings to a dynamic allocated buffer. Because their implementation uses strbuf to store its buffer and its length, so we can easily pass its length to the caller, furthermore, in ref-filter, we can reduce the use of strlen(). So introduce xstrvfmt_len() and xstrfmt_len() which can fetch the length of the buffer through the parameter `len`. This can bring a slight performance improvement. Mentored-by: Christian Couder Mentored-by: Hariom Verma Signed-off-by: ZheNing Hu --- ref-filter.c | 73 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 48 insertions(+), 25 deletions(-) diff --git a/ref-filter.c b/ref-filter.c index 875c2e4c39c..3784c0af57c 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -228,6 +228,29 @@ static int strbuf_addf_ret(struct strbuf *sb, int ret, const char *fmt, ...) return ret; } +__attribute__((format (printf, 2, 0))) +static char *xstrvfmt_len(ssize_t *len, const char *fmt, va_list ap) +{ + struct strbuf buf = STRBUF_INIT; + strbuf_vaddf(&buf, fmt, ap); + if (len) + *len = buf.len; + return strbuf_detach(&buf, NULL); +} + +__attribute__((format (printf, 2, 3))) +static char *xstrfmt_len(ssize_t *len, const char *fmt, ...) +{ + va_list ap; + char *ret; + + va_start(ap, fmt); + ret = xstrvfmt_len(len ,fmt, ap); + va_end(ap); + + return ret; +} + static int color_atom_parser(struct ref_format *format, struct used_atom *atom, const char *color_value, struct strbuf *err) { @@ -1083,10 +1106,10 @@ static void grab_common_values(struct atom_value *val, int deref, struct expand_ else if (atom_type == ATOM_OBJECTSIZE) { if (used_atom[i].u.objectsize.option == O_SIZE_DISK) { v->value = oi->disk_size; - v->s = xstrfmt("%"PRIuMAX, (uintmax_t)oi->disk_size); + v->s = xstrfmt_len(&v->s_size, "%"PRIuMAX, (uintmax_t)oi->disk_size); } else if (used_atom[i].u.objectsize.option == O_SIZE) { v->value = oi->size; - v->s = xstrfmt("%"PRIuMAX , (uintmax_t)oi->size); + v->s = xstrfmt_len(&v->s_size, "%"PRIuMAX , (uintmax_t)oi->size); } } else if (atom_type == ATOM_DELTABASE) v->s = xstrdup(oid_to_hex(&oi->delta_base_oid)); @@ -1139,7 +1162,7 @@ static void grab_commit_values(struct atom_value *val, int deref, struct object } if (atom_type == ATOM_NUMPARENT) { v->value = commit_list_count(commit->parents); - v->s = xstrfmt("%lu", (unsigned long)v->value); + v->s = xstrfmt_len(&v->s_size, "%lu", (unsigned long)v->value); } else if (atom_type == ATOM_PARENT) { struct commit_list *parents; @@ -1417,7 +1440,7 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct exp v->s = xmemdupz(buf, buf_size); v->s_size = buf_size; } else if (atom->u.raw_data.option == RAW_LENGTH) { - v->s = xstrfmt("%"PRIuMAX, (uintmax_t)buf_size); + v->s = xstrfmt_len(&v->s_size, "%"PRIuMAX, (uintmax_t)buf_size); } continue; } @@ -1444,7 +1467,7 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct exp } else if (atom->u.contents.option == C_BODY_DEP) v->s = xmemdupz(bodypos, bodylen); else if (atom->u.contents.option == C_LENGTH) - v->s = xstrfmt("%"PRIuMAX, (uintmax_t)strlen(subpos)); + v->s = xstrfmt_len(&v->s_size, "%"PRIuMAX, (uintmax_t)strlen(subpos)); else if (atom->u.contents.option == C_BODY) v->s = xmemdupz(bodypos, nonsiglen); else if (atom->u.contents.option == C_SIG) @@ -1611,56 +1634,56 @@ static const char *show_ref(struct refname_atom *atom, const char *refname) } static void fill_remote_ref_details(struct used_atom *atom, const char *refname, - struct branch *branch, const char **s) + struct branch *branch, struct atom_value *v) { int num_ours, num_theirs; if (atom->u.remote_ref.option == RR_REF) - *s = show_ref(&atom->u.remote_ref.refname, refname); + v->s = show_ref(&atom->u.remote_ref.refname, refname); else if (atom->u.remote_ref.option == RR_TRACK) { if (stat_tracking_info(branch, &num_ours, &num_theirs, NULL, atom->u.remote_ref.push, AHEAD_BEHIND_FULL) < 0) { - *s = xstrdup(msgs.gone); + v->s = xstrdup(msgs.gone); } else if (!num_ours && !num_theirs) - *s = xstrdup(""); + v->s = xstrdup(""); else if (!num_ours) - *s = xstrfmt(msgs.behind, num_theirs); + v->s = xstrfmt_len(&v->s_size, msgs.behind, num_theirs); else if (!num_theirs) - *s = xstrfmt(msgs.ahead, num_ours); + v->s = xstrfmt_len(&v->s_size, msgs.ahead, num_ours); else - *s = xstrfmt(msgs.ahead_behind, + v->s= xstrfmt_len(&v->s_size, msgs.ahead_behind, num_ours, num_theirs); - if (!atom->u.remote_ref.nobracket && *s[0]) { - const char *to_free = *s; - *s = xstrfmt("[%s]", *s); + if (!atom->u.remote_ref.nobracket && v->s[0]) { + const char *to_free = v->s; + v->s = xstrfmt_len(&v->s_size, "[%s]", v->s); free((void *)to_free); } } else if (atom->u.remote_ref.option == RR_TRACKSHORT) { if (stat_tracking_info(branch, &num_ours, &num_theirs, NULL, atom->u.remote_ref.push, AHEAD_BEHIND_FULL) < 0) { - *s = xstrdup(""); + v->s = xstrdup(""); return; } if (!num_ours && !num_theirs) - *s = xstrdup("="); + v->s = xstrdup("="); else if (!num_ours) - *s = xstrdup("<"); + v->s = xstrdup("<"); else if (!num_theirs) - *s = xstrdup(">"); + v->s = xstrdup(">"); else - *s = xstrdup("<>"); + v->s = xstrdup("<>"); } else if (atom->u.remote_ref.option == RR_REMOTE_NAME) { int explicit; const char *remote = atom->u.remote_ref.push ? pushremote_for_branch(branch, &explicit) : remote_for_branch(branch, &explicit); - *s = xstrdup(explicit ? remote : ""); + v->s = xstrdup(explicit ? remote : ""); } else if (atom->u.remote_ref.option == RR_REMOTE_REF) { const char *merge; merge = remote_ref_for_branch(branch, atom->u.remote_ref.push); - *s = xstrdup(merge ? merge : ""); + v->s = xstrdup(merge ? merge : ""); } else BUG("unhandled RR_* enum"); } @@ -1849,7 +1872,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err) refname = branch_get_upstream(branch, NULL); if (refname) - fill_remote_ref_details(atom, refname, branch, &v->s); + fill_remote_ref_details(atom, refname, branch, v); else v->s = xstrdup(""); continue; @@ -1870,7 +1893,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err) } /* We will definitely re-init v->s on the next line. */ free((char *)v->s); - fill_remote_ref_details(atom, refname, branch, &v->s); + fill_remote_ref_details(atom, refname, branch, v); continue; } else if (atom_type == ATOM_COLOR) { v->s = xstrdup(atom->u.color); @@ -1933,7 +1956,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err) if (!deref) v->s = xstrdup(refname); else - v->s = xstrfmt("%s^{}", refname); + v->s = xstrfmt_len(&v->s_size, "%s^{}", refname); free((char *)refname); } From patchwork Tue Aug 17 08:41:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: ZheNing Hu X-Patchwork-Id: 12440935 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2C080C4338F for ; Tue, 17 Aug 2021 08:41:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 106ED60F39 for ; Tue, 17 Aug 2021 08:41:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239406AbhHQImW (ORCPT ); Tue, 17 Aug 2021 04:42:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60892 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239369AbhHQImU (ORCPT ); Tue, 17 Aug 2021 04:42:20 -0400 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E9B66C0613CF for ; Tue, 17 Aug 2021 01:41:46 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id k29so27376057wrd.7 for ; Tue, 17 Aug 2021 01:41:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=pQwHZkBnmk4qKEh1WbWdn5OKpeAhR7FE3BEP540mhCo=; b=FaF5idmIOlort9eO/OUrBd5MRTsLqJtF/p3A8R+OqTmcP/YqBTJm3Cs0drjfPYKEs9 dLDyW3zOEFNdgpJHqfHsGFcAJFhI7U1VRj1jyAlGCqUEvqYRx9dCMp/sceIC3GCIoPdx hj7dsdZcW862sRYbtmZ0MRl8oA+Rj20ggWkoFWUPHPQbsJeIBXG/HWnp29243Ydz/3Zo 7NyWq11IbywXM9mf5hw+q6VI9E5zRkzeZar1fgw6+zrltGlLYQnTTblSzHfPlkl9S2Hh e0jg0u38igweyxcg+R5tLb+dZuBWT3hTDehhetSI0ZUS35ldwRClQ51puO7MleTO8nR1 6r+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=pQwHZkBnmk4qKEh1WbWdn5OKpeAhR7FE3BEP540mhCo=; b=UIfa+KX/8c8uwOR5kgQVDOzjK2nJFY3RzfpSgYk9X16frVkmQu9zsuUJDYNsobetQ1 tEdSKVQluW3myBq5K9HWb3iFZjdoDPAJV2c3psLoq3Z/zinH88ZAEfTf+8SB8+kuu+Dg BcT2C057LQvYj9X7xaANLwXw8y5u/dT81x8pCV056KhZbf4Qu7ynJ4qRG4OLL60FvCF5 i3cXD0aty7S+jJftG7tCLf4Zy5u6vNq/D1jL8BG4ojaTSdhoHAlolUiZdq0BXYAd9iLo 0og30rRkxARGeBt7YLhH2uzbRlkMhCB2EZ9b2ljx6Zc3nF0rL39f7V9c8r9C+fZZp1G7 yNcg== X-Gm-Message-State: AOAM531d1xqabs9E1BeFLNB3jpvibwI788GeNQk2sW4Z/zmWLNnSbJJG Qtozt1r6folc/QvdjvrzzGyl2GWH9+o= X-Google-Smtp-Source: ABdhPJzz4XjXwq5/FSLCQKmWtlqNBmKd9qnpjIsKdBk+t+IhpL8jztiCMhxEFQXYLjGzbTYG9Rdv8w== X-Received: by 2002:adf:fc45:: with SMTP id e5mr2526051wrs.127.1629189705601; Tue, 17 Aug 2021 01:41:45 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id i14sm1250145wmq.40.2021.08.17.01.41.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Aug 2021 01:41:45 -0700 (PDT) Message-Id: <296dd2ae0119b9f465a534019089cd330afe5a2a.1629189701.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 17 Aug 2021 08:41:38 +0000 Subject: [PATCH 5/8] [GSOC] ref-filter: introduction ref_filter_slopbuf[1] Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Junio C Hamano , Christian Couder , Hariom Verma , Bagas Sanjaya , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Eric Sunshine , Philip Oakley , ZheNing Hu , ZheNing Hu Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: ZheNing Hu From: ZheNing Hu The ref-filter code uses xstrdup("") extensively to indicate null value. xstrdup("") calls strdup(""), which in glibc will call malloc(1), this means using xstrdup("") will cause unnecessary memory allocation. Introduce the global one-byte array variable ref_filter_slopbuf[1], which learn from strbuf_slopbuf[1] in strbuf.c. Using ref_filter_slopbuf instead of xstrdup(""), we can reduce some malloc/free overhead. Mentored-by: Christian Couder Mentored-by: Hariom Verma Signed-off-by: ZheNing Hu --- ref-filter.c | 53 ++++++++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/ref-filter.c b/ref-filter.c index 3784c0af57c..b74fcbb4806 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -24,6 +24,8 @@ #include "hashmap.h" #include "strvec.h" +static char ref_filter_slopbuf[1]; + static struct ref_msg { const char *gone; const char *ahead; @@ -1209,7 +1211,7 @@ static const char *copy_name(const char *buf) if (!strncmp(cp, " <", 2)) return xmemdupz(buf, cp - buf); } - return xstrdup(""); + return ref_filter_slopbuf; } static const char *copy_email(const char *buf, struct used_atom *atom) @@ -1217,7 +1219,7 @@ static const char *copy_email(const char *buf, struct used_atom *atom) const char *email = strchr(buf, '<'); const char *eoemail; if (!email) - return xstrdup(""); + return ref_filter_slopbuf; switch (atom->u.email_option.option) { case EO_RAW: eoemail = strchr(email, '>'); @@ -1239,7 +1241,7 @@ static const char *copy_email(const char *buf, struct used_atom *atom) } if (!eoemail) - return xstrdup(""); + return ref_filter_slopbuf; return xmemdupz(email, eoemail - email); } @@ -1293,7 +1295,7 @@ static void grab_date(const char *buf, struct atom_value *v, const char *atomnam v->value = timestamp; return; bad: - v->s = xstrdup(""); + v->s = ref_filter_slopbuf; v->value = 0; } @@ -1503,7 +1505,7 @@ static void fill_missing_values(struct atom_value *val) for (i = 0; i < used_atom_cnt; i++) { struct atom_value *v = &val[i]; if (v->s == NULL) - v->s = xstrdup(""); + v->s = ref_filter_slopbuf; } } @@ -1576,7 +1578,7 @@ static const char *lstrip_ref_components(const char *refname, int len) switch (*start++) { case '\0': free((char *)to_free); - return xstrdup(""); + return ref_filter_slopbuf; case '/': remaining--; break; @@ -1614,7 +1616,7 @@ static const char *rstrip_ref_components(const char *refname, int len) char *p = strrchr(start, '/'); if (p == NULL) { free((char *)to_free); - return xstrdup(""); + return ref_filter_slopbuf; } else p[0] = '\0'; } @@ -1645,7 +1647,7 @@ static void fill_remote_ref_details(struct used_atom *atom, const char *refname, AHEAD_BEHIND_FULL) < 0) { v->s = xstrdup(msgs.gone); } else if (!num_ours && !num_theirs) - v->s = xstrdup(""); + v->s = ref_filter_slopbuf; else if (!num_ours) v->s = xstrfmt_len(&v->s_size, msgs.behind, num_theirs); else if (!num_theirs) @@ -1662,7 +1664,7 @@ static void fill_remote_ref_details(struct used_atom *atom, const char *refname, if (stat_tracking_info(branch, &num_ours, &num_theirs, NULL, atom->u.remote_ref.push, AHEAD_BEHIND_FULL) < 0) { - v->s = xstrdup(""); + v->s = ref_filter_slopbuf; return; } if (!num_ours && !num_theirs) @@ -1721,7 +1723,7 @@ char *get_head_description(void) static const char *get_symref(struct used_atom *atom, struct ref_array_item *ref) { if (!ref->symref) - return xstrdup(""); + return ref_filter_slopbuf; else return show_ref(&atom->u.refname, ref->symref); } @@ -1805,7 +1807,7 @@ static char *get_worktree_path(const struct used_atom *atom, const struct ref_ar e = hashmap_get(&(ref_to_worktree_map.map), &entry, ref->refname); if (!e) - return xstrdup(""); + return ref_filter_slopbuf; lookup_result = container_of(e, struct ref_to_worktree_entry, ent); @@ -1827,7 +1829,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err) ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING, NULL, NULL); if (!ref->symref) - ref->symref = xstrdup(""); + ref->symref = ref_filter_slopbuf; } /* Fill in specials first */ @@ -1855,7 +1857,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err) if (ref->kind == FILTER_REFS_BRANCHES) v->s = get_worktree_path(atom, ref); else - v->s = xstrdup(""); + v->s = ref_filter_slopbuf; continue; } else if (atom_type == ATOM_SYMREF) @@ -1865,7 +1867,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err) /* only local branches may have an upstream */ if (!skip_prefix(ref->refname, "refs/heads/", &branch_name)) { - v->s = xstrdup(""); + v->s = ref_filter_slopbuf; continue; } branch = branch_get(branch_name); @@ -1874,11 +1876,11 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err) if (refname) fill_remote_ref_details(atom, refname, branch, v); else - v->s = xstrdup(""); + v->s = ref_filter_slopbuf; continue; } else if (atom_type == ATOM_PUSH && atom->u.remote_ref.push) { const char *branch_name; - v->s = xstrdup(""); + v->s = ref_filter_slopbuf; if (!skip_prefix(ref->refname, "refs/heads/", &branch_name)) continue; @@ -1891,8 +1893,6 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err) if (!refname) continue; } - /* We will definitely re-init v->s on the next line. */ - free((char *)v->s); fill_remote_ref_details(atom, refname, branch, v); continue; } else if (atom_type == ATOM_COLOR) { @@ -1905,7 +1905,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err) if (ref->flag & REF_ISPACKED) cp = copy_advance(cp, ",packed"); if (cp == buf) - v->s = xstrdup(""); + v->s = ref_filter_slopbuf; else { *cp = '\0'; v->s = xstrdup(buf + 1); @@ -1922,33 +1922,33 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err) continue; } else if (atom_type == ATOM_ALIGN) { v->handler = align_atom_handler; - v->s = xstrdup(""); + v->s = ref_filter_slopbuf; continue; } else if (atom_type == ATOM_END) { v->handler = end_atom_handler; - v->s = xstrdup(""); + v->s = ref_filter_slopbuf; continue; } else if (atom_type == ATOM_IF) { const char *s; if (skip_prefix(name, "if:", &s)) v->s = xstrdup(s); else - v->s = xstrdup(""); + v->s = ref_filter_slopbuf; v->handler = if_atom_handler; continue; } else if (atom_type == ATOM_THEN) { v->handler = then_atom_handler; - v->s = xstrdup(""); + v->s = ref_filter_slopbuf; continue; } else if (atom_type == ATOM_ELSE) { v->handler = else_atom_handler; - v->s = xstrdup(""); + v->s = ref_filter_slopbuf; continue; } else if (atom_type == ATOM_REST) { if (ref->rest) v->s = xstrdup(ref->rest); else - v->s = xstrdup(""); + v->s = ref_filter_slopbuf; continue; } else continue; @@ -2297,7 +2297,8 @@ static void free_array_item(struct ref_array_item *item) if (item->value) { int i; for (i = 0; i < used_atom_cnt; i++) - free((char *)item->value[i].s); + if (item->value[i].s != ref_filter_slopbuf) + free((char *)item->value[i].s); free(item->value); } free(item); From patchwork Tue Aug 17 08:41:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: ZheNing Hu X-Patchwork-Id: 12440937 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.9 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNWANTED_LANGUAGE_BODY autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DFC40C4338F for ; Tue, 17 Aug 2021 08:41:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C413B60F39 for ; Tue, 17 Aug 2021 08:41:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239423AbhHQImX (ORCPT ); Tue, 17 Aug 2021 04:42:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239383AbhHQImU (ORCPT ); Tue, 17 Aug 2021 04:42:20 -0400 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9EFB0C061796 for ; Tue, 17 Aug 2021 01:41:47 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id k5-20020a05600c1c85b02902e699a4d20cso1600979wms.2 for ; Tue, 17 Aug 2021 01:41:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=PKmZcXQH+W69ExH0bRTUl7cjmuufxLGZEjBt7W0TDX8=; b=rUYATxMTx2X3xxsU0C6vympp6DtUbAuKbGZEBDN6tyS7wZQ9+NpKQOlEDez4xcLrki JDhmvc4bReIzuVqWv01RTtlD7538ZRwKXueNtNAQWwWQ58IGgWNFLwvgWS1zbQAWMYqu 5ieqGJklWUiuWEH11p5+HrG3iQSb3kGfpZrH+7ktyz9ZuLBQMTHtC+XCcixul9TAC6s6 4638J4ACIkQA1sKYwebCJ1dgfTWT5tiQURioQ2PDjd/PKx2XrRAPa937MqVD5Zl2fYUK 9sTolRp0mKFzq7Xst1QUEF+BSBuiUDs0IgNkSqrw54kAERK1ZYsruvXfkdFDBOcP9abB lejg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=PKmZcXQH+W69ExH0bRTUl7cjmuufxLGZEjBt7W0TDX8=; b=Z9n8CCG9CqS3SPELZ2Ux5m0b8GH1yL+XZ9U62b7cxhqR2VPMUzmTHSkkHkfxoM+xSz W2dKluJ4KFv7UAGeW8D63k/FzCHovG2/o17Uulqjs6NbIy0x6xBMflM391x8DPqEOsdW 7HS0eDLfoui8bJ76xKf1oUXBEgF/FyrBdb/NEGJgFhsoR9nVkYRKWDVwfbN9zz6+FJer 7B5rjrGuaHl0BtKugn/pWHoEhLBui4pZWVQk3LHq5HXd27wuMJWrrOcI+YFdhkJJ7j3Z jtUGWPyQKSEx0GTMORVIT+X39W4gkJddX3ySaMjJf6WTxzNTYsQr7O5ZiBp51/uhsHdy qTnw== X-Gm-Message-State: AOAM533uGDYl5YcV9TdwuWHKPc3gBLP6yT88/WZ6UlLUy497OjUxKDqd odpBqCp9xANiMEvYcjGXrjHprmcra70= X-Google-Smtp-Source: ABdhPJypTloPGRGMXaK3ZjYbz4TVPMjPXFZ2QAmjNT3mLvzz7JfIziGjOk5707oNj9wUKGCbwp+eZg== X-Received: by 2002:a7b:ce0b:: with SMTP id m11mr2260126wmc.150.1629189706178; Tue, 17 Aug 2021 01:41:46 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id d4sm1598593wrz.35.2021.08.17.01.41.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Aug 2021 01:41:45 -0700 (PDT) Message-Id: <33b227e5fdf60c28392ef9660e9ae6f149a905bf.1629189701.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 17 Aug 2021 08:41:39 +0000 Subject: [PATCH 6/8] [GSOC] ref-filter: add deref member to struct used_atom Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Junio C Hamano , Christian Couder , Hariom Verma , Bagas Sanjaya , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Eric Sunshine , Philip Oakley , ZheNing Hu , ZheNing Hu Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: ZheNing Hu From: ZheNing Hu In the ref-filter logic, the dereference flag '*' is stored at the beginning of used_atom[i].name (e.g. '*objectname:short'), this leads us to make some check on used_atom[i].name[0] to know if the atom need to be dereferenced. This is not only cumbersome to use, but also poor in readability. So add `deref` flag to struct used_atom used to indicate whether the atom needs to be dereferenced and let used_atom record only the rest part (i.e. 'objectname:short'). This can make the logic of the program clearer and easy to use. Mentored-by: Christian Couder Mentored-by: Hariom Verma Signed-off-by: ZheNing Hu --- ref-filter.c | 55 ++++++++++++++++++++-------------------------------- 1 file changed, 21 insertions(+), 34 deletions(-) diff --git a/ref-filter.c b/ref-filter.c index b74fcbb4806..5abcbbf10ac 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -173,6 +173,7 @@ enum atom_type { * array. */ static struct used_atom { + unsigned int deref : 1; enum atom_type atom_type; const char *name; cmp_type type; @@ -343,7 +344,7 @@ static int objecttype_atom_parser(struct ref_format *format, struct used_atom *a { if (arg) return strbuf_addf_ret(err, -1, _("%%(objecttype) does not take arguments")); - if (*atom->name == '*') + if (atom->deref) oi_deref.info.typep = &oi_deref.type; else oi.info.typep = &oi.type; @@ -355,13 +356,13 @@ static int objectsize_atom_parser(struct ref_format *format, struct used_atom *a { if (!arg) { atom->u.objectsize.option = O_SIZE; - if (*atom->name == '*') + if (atom->deref) oi_deref.info.sizep = &oi_deref.size; else oi.info.sizep = &oi.size; } else if (!strcmp(arg, "disk")) { atom->u.objectsize.option = O_SIZE_DISK; - if (*atom->name == '*') + if (atom->deref) oi_deref.info.disk_sizep = &oi_deref.disk_size; else oi.info.disk_sizep = &oi.disk_size; @@ -375,7 +376,7 @@ static int deltabase_atom_parser(struct ref_format *format, struct used_atom *at { if (arg) return strbuf_addf_ret(err, -1, _("%%(deltabase) does not take arguments")); - if (*atom->name == '*') + if (atom->deref) oi_deref.info.delta_base_oid = &oi_deref.delta_base_oid; else oi.info.delta_base_oid = &oi.delta_base_oid; @@ -697,10 +698,13 @@ static int parse_ref_filter_atom(struct ref_format *format, const char *sp; const char *arg; int i, at, atom_len; + int deref = 0; sp = atom; - if (*sp == '*' && sp < ep) + if (*sp == '*' && sp < ep) { sp++; /* deref */ + deref = 1; + } if (ep <= sp) return strbuf_addf_ret(err, -1, _("malformed field name: %.*s"), (int)(ep-atom), atom); @@ -717,7 +721,7 @@ static int parse_ref_filter_atom(struct ref_format *format, /* Do we have the atom already used elsewhere? */ for (i = 0; i < used_atom_cnt; i++) { int len = strlen(used_atom[i].name); - if (len == ep - atom && !memcmp(used_atom[i].name, atom, len)) + if (len == ep - sp && !memcmp(used_atom[i].name, sp, len)) return i; } @@ -741,17 +745,18 @@ static int parse_ref_filter_atom(struct ref_format *format, used_atom_cnt++; REALLOC_ARRAY(used_atom, used_atom_cnt); used_atom[at].atom_type = i; - used_atom[at].name = xmemdupz(atom, ep - atom); + used_atom[at].deref = deref; + used_atom[at].name = xmemdupz(sp, ep - sp); used_atom[at].type = valid_atom[i].cmp_type; used_atom[at].source = valid_atom[i].source; if (used_atom[at].source == SOURCE_OBJ) { - if (*atom == '*') + if (deref) oi_deref.info.contentp = &oi_deref.content; else oi.info.contentp = &oi.content; } if (arg) { - arg = used_atom[at].name + (arg - atom) + 1; + arg = used_atom[at].name + (arg - sp) + 1; if (!*arg) { /* * Treat empty sub-arguments list as NULL (i.e., @@ -763,7 +768,7 @@ static int parse_ref_filter_atom(struct ref_format *format, memset(&used_atom[at].u, 0, sizeof(used_atom[at].u)); if (valid_atom[i].parser && valid_atom[i].parser(format, &used_atom[at], arg, err)) return -1; - if (*atom == '*') + if (deref) need_tagged = 1; if (i == ATOM_SYMREF) need_symref = 1; @@ -1096,13 +1101,10 @@ static void grab_common_values(struct atom_value *val, int deref, struct expand_ int i; for (i = 0; i < used_atom_cnt; i++) { - const char *name = used_atom[i].name; enum atom_type atom_type = used_atom[i].atom_type; struct atom_value *v = &val[i]; - if (!!deref != (*name == '*')) + if (!!deref != used_atom[i].deref) continue; - if (deref) - name++; if (atom_type == ATOM_OBJECTTYPE) v->s = xstrdup(type_name(oi->type)); else if (atom_type == ATOM_OBJECTSIZE) { @@ -1128,13 +1130,10 @@ static void grab_tag_values(struct atom_value *val, int deref, struct object *ob struct tag *tag = (struct tag *) obj; for (i = 0; i < used_atom_cnt; i++) { - const char *name = used_atom[i].name; enum atom_type atom_type = used_atom[i].atom_type; struct atom_value *v = &val[i]; - if (!!deref != (*name == '*')) + if (!!deref != used_atom[i].deref) continue; - if (deref) - name++; if (atom_type == ATOM_TAG) v->s = xstrdup(tag->tag); else if (atom_type == ATOM_TYPE && tag->tagged) @@ -1151,13 +1150,10 @@ static void grab_commit_values(struct atom_value *val, int deref, struct object struct commit *commit = (struct commit *) obj; for (i = 0; i < used_atom_cnt; i++) { - const char *name = used_atom[i].name; enum atom_type atom_type = used_atom[i].atom_type; struct atom_value *v = &val[i]; - if (!!deref != (*name == '*')) + if (!!deref != used_atom[i].deref) continue; - if (deref) - name++; if (atom_type == ATOM_TREE) { v->s = xstrdup(do_grab_oid("tree", get_commit_tree_oid(commit), &used_atom[i])); continue; @@ -1311,10 +1307,8 @@ static void grab_person(enum atom_type type, struct atom_value *val, int deref, const char *name = used_atom[i].name; enum atom_type atom_type = used_atom[i].atom_type; struct atom_value *v = &val[i]; - if (!!deref != (*name == '*')) + if (!!deref != used_atom[i].deref) continue; - if (deref) - name++; if ((atom_type < type || atom_type > type + 3) && /* * For a tag or a commit object, if "creator" or "creatordate" is @@ -1430,10 +1424,8 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct exp struct atom_value *v = &val[i]; enum atom_type atom_type = atom->atom_type; - if (!!deref != (*name == '*')) + if (!!deref != used_atom[i].deref) continue; - if (deref) - name++; if (atom_type == ATOM_RAW) { unsigned long buf_size = data->size; @@ -1838,7 +1830,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err) enum atom_type atom_type = atom->atom_type; const char *name = used_atom[i].name; struct atom_value *v = &ref->value[i]; - int deref = 0; + int deref = atom->deref; const char *refname; struct branch *branch = NULL; @@ -1846,11 +1838,6 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err) v->handler = append_atom; v->atom = atom; - if (*name == '*') { - deref = 1; - name++; - } - if (atom_type == ATOM_REFNAME) refname = get_refname(atom, ref); else if (atom_type == ATOM_WORKTREEPATH) { From patchwork Tue Aug 17 08:41:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: ZheNing Hu X-Patchwork-Id: 12440939 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9171DC432BE for ; Tue, 17 Aug 2021 08:41:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 79B9460F39 for ; Tue, 17 Aug 2021 08:41:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239461AbhHQIm0 (ORCPT ); Tue, 17 Aug 2021 04:42:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60902 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239336AbhHQImU (ORCPT ); Tue, 17 Aug 2021 04:42:20 -0400 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1872FC061764 for ; Tue, 17 Aug 2021 01:41:48 -0700 (PDT) Received: by mail-wm1-x32b.google.com with SMTP id x2-20020a1c7c02000000b002e6f1f69a1eso1270164wmc.5 for ; Tue, 17 Aug 2021 01:41:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=jcYu+aw6K++rHfhWMN4DQ874DwNUGIEgwUWYMEqbzcI=; b=gLGynOM41ZRt4KMdugBVem1HtBM9FehqoSMwiqQbviZveeKjgfrzVfm34lmDOiDOq0 CzBGO5GEqEfklnAulJMnumtqfenPMFV1J7/mvocjqMllmwlOf2jFQclqHGZ2v/l5BUGZ EwoXjFfEnbXP9ltEjs7DNAX4IKxW1qwo/rIsAOwhMH9qWyjipbZPbr21VD3gAJrkRikn 2DwP0RthjaUK8m87MDyLhr/S0zkNoXNfF7s1RUqAPpEh+URpeusVSwgQHSZOqhWDRk5v nyJ2wZRWS18pdRVBjxN/yUsDm+3+T0n5KYguLvssubY59K/KtBttYs+FL1pgjbGrbNtV 2aAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=jcYu+aw6K++rHfhWMN4DQ874DwNUGIEgwUWYMEqbzcI=; b=qGu7tWA5fK02HEql3jtusxyHZOsKT41l2tzZ3ZYA3j87bQXB+LGOOygFi+T3nqHxMC I46OthlnIE61r80dgvdYiM/sj/YOsxbfgx1wvlfoQCdsk99edgJeu9/xwSgIsACqQONH fX92boVDCPhTk7kV2pVG3Vm+n4cQeGPL6TvGY+5Pnct7HVZ4M0CU6qytnmdk8uDl1G3r X6F2o8+g1x1EbpCC4bp1E34zTnVpcJddMll8CYD/QVhCB77g12pjt21mo/6RcAQ2vBtX jCfoZ8AWYm0926TGkjzlD6wP1VerOc+29b1105iaCxmccdofAgFJIKyj6WbPhIEt6w9N PAnQ== X-Gm-Message-State: AOAM531vPlCYxDMUHHcaQNc6/RCjvLaux8lciQgkrWbBc3hIbcghSAMw VDtG5v/QdMtBLV/y78AcGtASfNiTe9s= X-Google-Smtp-Source: ABdhPJy9kEo7XCqg254imJVE9F929s1aIq+dl2dmobLZkXALHHyn676aAh1cLYPAgDl6i/xKsdZVOw== X-Received: by 2002:a1c:7506:: with SMTP id o6mr2265435wmc.112.1629189706793; Tue, 17 Aug 2021 01:41:46 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id p18sm1572619wrt.13.2021.08.17.01.41.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Aug 2021 01:41:46 -0700 (PDT) Message-Id: <61d452a6278abfe84680a11c28cde9d3a48d0848.1629189701.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 17 Aug 2021 08:41:40 +0000 Subject: [PATCH 7/8] [GSOC] ref-filter: introduce symref_atom_parser() Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Junio C Hamano , Christian Couder , Hariom Verma , Bagas Sanjaya , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Eric Sunshine , Philip Oakley , ZheNing Hu , ZheNing Hu Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: ZheNing Hu From: ZheNing Hu parse_ref_filter_atom() sets need_symref by checking whether the atom type is ATOM_SYMREF. When we are operating other atoms here, this step of checking is not necessary. So add the symref_atom_parser() specifically to parse the %(symref) atom, and set need_symref in it. This can make the program logic more concise. Mentored-by: Christian Couder Mentored-by: Hariom Verma Signed-off-by: ZheNing Hu --- ref-filter.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/ref-filter.c b/ref-filter.c index 5abcbbf10ac..f1c82e20e3d 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -503,6 +503,13 @@ static int person_email_atom_parser(struct ref_format *format, struct used_atom return 0; } +static int symref_atom_parser(struct ref_format *format, struct used_atom *atom, + const char *arg, struct strbuf *err) +{ + need_symref = 1; + return refname_atom_parser_internal(&atom->u.refname, arg, atom->name, err); +} + static int refname_atom_parser(struct ref_format *format, struct used_atom *atom, const char *arg, struct strbuf *err) { @@ -642,7 +649,7 @@ static struct { [ATOM_RAW] = { "raw", SOURCE_OBJ, FIELD_STR, raw_atom_parser }, [ATOM_UPSTREAM] = { "upstream", SOURCE_NONE, FIELD_STR, remote_ref_atom_parser }, [ATOM_PUSH] = { "push", SOURCE_NONE, FIELD_STR, remote_ref_atom_parser }, - [ATOM_SYMREF] = { "symref", SOURCE_NONE, FIELD_STR, refname_atom_parser }, + [ATOM_SYMREF] = { "symref", SOURCE_NONE, FIELD_STR, symref_atom_parser }, [ATOM_FLAG] = { "flag", SOURCE_NONE }, [ATOM_HEAD] = { "HEAD", SOURCE_NONE, FIELD_STR, head_atom_parser }, [ATOM_COLOR] = { "color", SOURCE_NONE, FIELD_STR, color_atom_parser }, @@ -770,8 +777,6 @@ static int parse_ref_filter_atom(struct ref_format *format, return -1; if (deref) need_tagged = 1; - if (i == ATOM_SYMREF) - need_symref = 1; return at; } From patchwork Tue Aug 17 08:41:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: ZheNing Hu X-Patchwork-Id: 12440941 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.9 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNWANTED_LANGUAGE_BODY autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D81F0C43214 for ; Tue, 17 Aug 2021 08:41:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id ADB9660F39 for ; Tue, 17 Aug 2021 08:41:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239415AbhHQIm1 (ORCPT ); Tue, 17 Aug 2021 04:42:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60902 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239310AbhHQImV (ORCPT ); Tue, 17 Aug 2021 04:42:21 -0400 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B8151C061796 for ; Tue, 17 Aug 2021 01:41:48 -0700 (PDT) Received: by mail-wm1-x32f.google.com with SMTP id c129-20020a1c35870000b02902e6b6135279so1625047wma.0 for ; Tue, 17 Aug 2021 01:41:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=9bWj2PH1+S+g2cTOYfOr+HqFSWSdUzasvDRTl6U/EZk=; b=WQ6H75ZjEeYqNRSxFck+ktKY1nDyx61Ju6/OxHe1Y1/IyzlF0zJiuDeOUsiXNJWviz ghd6Fd7bGyQWD1CfYlSu2bLFM5M+stJvA4CnMWr0rDyuL1LbXmSTP91eKtweXhQT2Lf2 IEHnnkxucS/JzX2pxcbyc6Dqd42a9rsEuusz13QEI7y64n6lJhYJOWwGSVdTL1F7yufn tN5o4qM6u7kz+XQ4tWIg3J6c3FB5DrMD9qoAx5JYL9+m57xVusUUId7mr+XGZREUSdt9 zE0qqvaOdweK86j9Dpo3256hPVBREMbdO9dvNnNGjmJlWkKInB0htlWZ/cTyv9u1M84l PvpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=9bWj2PH1+S+g2cTOYfOr+HqFSWSdUzasvDRTl6U/EZk=; b=KWCLKLxW91ThpzOoFKNux24s/pw7ZoGXFTljf6R2FluERJ6XEUEaQMGzV5R3uQcIrb mFTmV5sDGiAjasVFxiexGsv+RjL5biw6WCz3e7ocrUR+6Tprh40B8dK+K0jzG5d3fD20 Nn43GjrjMfi2mcp2yaxuHsqVmwyAjQJEAxXpFoc3v0BZwr1Vduu2DXReT/egZOWc88qe Pt0o6OyrsP8QMFn8Z0cIQkrgH7pjBF/nDQZcqcAHFeIDLqS5FkQfKCbSZT1PsBcOwMzM tz9GLErbpXqE7bkm5ddLJwSPKvnhPZOC+1NuKFBGKa+VJ5Mwv/E3pUgzw7GVmEXewmqM yyzg== X-Gm-Message-State: AOAM5301SnWBKEqE8eEi7qZ9LIS1REawCvvoMkZ+ILxGhchNl4onEF3I 27+vTvUSM6IdmSe+EzpH7wqDXc3pxR8= X-Google-Smtp-Source: ABdhPJx4au5vOi6GuOg4zQ0BrlymN5KG6rLJrwqLV6dGOCr6v97oZjNJdxgmkIeC1ApZeL1zUVCAEw== X-Received: by 2002:a05:600c:4fcc:: with SMTP id o12mr2194352wmq.0.1629189707377; Tue, 17 Aug 2021 01:41:47 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id t14sm1606675wmj.2.2021.08.17.01.41.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Aug 2021 01:41:47 -0700 (PDT) Message-Id: <2321b873d0c0223e553492d80ced2a51d8ce7281.1629189701.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 17 Aug 2021 08:41:41 +0000 Subject: [PATCH 8/8] [GSOC] ref-filter: use switch/case instead of if/else Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Junio C Hamano , Christian Couder , Hariom Verma , Bagas Sanjaya , Jeff King , =?utf-8?b?w4Z2YXIgQXJuZmrDtnLDsA==?= Bjarmason , Eric Sunshine , Philip Oakley , ZheNing Hu , ZheNing Hu Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: ZheNing Hu From: ZheNing Hu In the logic of ref-filter, if/else is used extensively to check the atom type. This is because before we introduce atom_type, the atom check is achieved through string comparison. Using switch/case can allow the compiler to reduce unnecessary branch checks and increase the readability of the code. Mentored-by: Christian Couder Mentored-by: Hariom Verma Signed-off-by: ZheNing Hu --- ref-filter.c | 163 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 109 insertions(+), 54 deletions(-) diff --git a/ref-filter.c b/ref-filter.c index f1c82e20e3d..c0b5d30ac57 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -1110,9 +1110,11 @@ static void grab_common_values(struct atom_value *val, int deref, struct expand_ struct atom_value *v = &val[i]; if (!!deref != used_atom[i].deref) continue; - if (atom_type == ATOM_OBJECTTYPE) + switch (atom_type) { + case ATOM_OBJECTTYPE : { v->s = xstrdup(type_name(oi->type)); - else if (atom_type == ATOM_OBJECTSIZE) { + break; + } case ATOM_OBJECTSIZE : { if (used_atom[i].u.objectsize.option == O_SIZE_DISK) { v->value = oi->disk_size; v->s = xstrfmt_len(&v->s_size, "%"PRIuMAX, (uintmax_t)oi->disk_size); @@ -1120,10 +1122,20 @@ static void grab_common_values(struct atom_value *val, int deref, struct expand_ v->value = oi->size; v->s = xstrfmt_len(&v->s_size, "%"PRIuMAX , (uintmax_t)oi->size); } - } else if (atom_type == ATOM_DELTABASE) + break; + } + case ATOM_DELTABASE: { v->s = xstrdup(oid_to_hex(&oi->delta_base_oid)); - else if (atom_type == ATOM_OBJECTNAME && deref) { - v->s = xstrdup(do_grab_oid("objectname", &oi->oid, &used_atom[i])); + break; + } + case ATOM_OBJECTNAME: { + if (deref) + v->s = xstrdup(do_grab_oid("objectname", &oi->oid, &used_atom[i])); + break; + } + default: { + break; + } } } } @@ -1138,13 +1150,22 @@ static void grab_tag_values(struct atom_value *val, int deref, struct object *ob enum atom_type atom_type = used_atom[i].atom_type; struct atom_value *v = &val[i]; if (!!deref != used_atom[i].deref) - continue; - if (atom_type == ATOM_TAG) + break; + switch (atom_type) { + case ATOM_TAG: v->s = xstrdup(tag->tag); - else if (atom_type == ATOM_TYPE && tag->tagged) - v->s = xstrdup(type_name(tag->tagged->type)); - else if (atom_type == ATOM_OBJECT && tag->tagged) - v->s = xstrdup(oid_to_hex(&tag->tagged->oid)); + break; + case ATOM_TYPE: + if (tag->tagged) + v->s = xstrdup(type_name(tag->tagged->type)); + break; + case ATOM_OBJECT: + if (tag->tagged) + v->s = xstrdup(oid_to_hex(&tag->tagged->oid)); + break; + default: + break; + } } } @@ -1159,15 +1180,17 @@ static void grab_commit_values(struct atom_value *val, int deref, struct object struct atom_value *v = &val[i]; if (!!deref != used_atom[i].deref) continue; - if (atom_type == ATOM_TREE) { + switch (atom_type) { + case ATOM_TREE: { v->s = xstrdup(do_grab_oid("tree", get_commit_tree_oid(commit), &used_atom[i])); - continue; + break; } - if (atom_type == ATOM_NUMPARENT) { + case ATOM_NUMPARENT: { v->value = commit_list_count(commit->parents); v->s = xstrfmt_len(&v->s_size, "%lu", (unsigned long)v->value); + break; } - else if (atom_type == ATOM_PARENT) { + case ATOM_PARENT: { struct commit_list *parents; struct strbuf s = STRBUF_INIT; for (parents = commit->parents; parents; parents = parents->next) { @@ -1177,6 +1200,11 @@ static void grab_commit_values(struct atom_value *val, int deref, struct object strbuf_addstr(&s, do_grab_oid("parent", oid, &used_atom[i])); } v->s = strbuf_detach(&s, NULL); + break; + } + default: { + break; + } } } } @@ -1843,24 +1871,40 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err) v->handler = append_atom; v->atom = atom; - if (atom_type == ATOM_REFNAME) + switch (atom_type) { + case ATOM_REFNAME: { refname = get_refname(atom, ref); - else if (atom_type == ATOM_WORKTREEPATH) { + if (!deref) + v->s = xstrdup(refname); + else + v->s = xstrfmt_len(&v->s_size, "%s^{}", refname); + free((char *)refname); + break; + } + case ATOM_WORKTREEPATH: { if (ref->kind == FILTER_REFS_BRANCHES) v->s = get_worktree_path(atom, ref); else v->s = ref_filter_slopbuf; - continue; + break; } - else if (atom_type == ATOM_SYMREF) + case ATOM_SYMREF: { refname = get_symref(atom, ref); - else if (atom_type == ATOM_UPSTREAM) { + if (!deref) + v->s = xstrdup(refname); + else + v->s = xstrfmt_len(&v->s_size, "%s^{}", refname); + if (refname != ref_filter_slopbuf) + free((char *)refname); + break; + } + case ATOM_UPSTREAM: { const char *branch_name; /* only local branches may have an upstream */ if (!skip_prefix(ref->refname, "refs/heads/", &branch_name)) { v->s = ref_filter_slopbuf; - continue; + break; } branch = branch_get(branch_name); @@ -1869,13 +1913,17 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err) fill_remote_ref_details(atom, refname, branch, v); else v->s = ref_filter_slopbuf; - continue; - } else if (atom_type == ATOM_PUSH && atom->u.remote_ref.push) { + break; + } + case ATOM_PUSH: { const char *branch_name; + + if (!atom->u.remote_ref.push) + break; v->s = ref_filter_slopbuf; if (!skip_prefix(ref->refname, "refs/heads/", &branch_name)) - continue; + break; branch = branch_get(branch_name); if (atom->u.remote_ref.push_remote) @@ -1883,14 +1931,16 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err) else { refname = branch_get_push(branch, NULL); if (!refname) - continue; + break; } fill_remote_ref_details(atom, refname, branch, v); - continue; - } else if (atom_type == ATOM_COLOR) { + break; + } + case ATOM_COLOR: { v->s = xstrdup(atom->u.color); continue; - } else if (atom_type == ATOM_FLAG) { + } + case ATOM_FLAG: { char buf[256], *cp = buf; if (ref->flag & REF_ISSYMREF) cp = copy_advance(cp, ",symref"); @@ -1902,54 +1952,59 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err) *cp = '\0'; v->s = xstrdup(buf + 1); } - continue; - } else if (!deref && atom_type == ATOM_OBJECTNAME) { - v->s = xstrdup(do_grab_oid("objectname", &ref->objectname, atom)); - continue; - } else if (atom_type == ATOM_HEAD) { + break; + } + case ATOM_OBJECTNAME: { + if (!deref) + v->s = xstrdup(do_grab_oid("objectname", &ref->objectname, atom)); + break; + } + case ATOM_HEAD: { if (atom->u.head && !strcmp(ref->refname, atom->u.head)) v->s = xstrdup("*"); else v->s = xstrdup(" "); - continue; - } else if (atom_type == ATOM_ALIGN) { + break; + } + case ATOM_ALIGN: { v->handler = align_atom_handler; v->s = ref_filter_slopbuf; - continue; - } else if (atom_type == ATOM_END) { + break; + } + case ATOM_END: { v->handler = end_atom_handler; v->s = ref_filter_slopbuf; - continue; - } else if (atom_type == ATOM_IF) { + break; + } + case ATOM_IF: { const char *s; if (skip_prefix(name, "if:", &s)) v->s = xstrdup(s); else v->s = ref_filter_slopbuf; v->handler = if_atom_handler; - continue; - } else if (atom_type == ATOM_THEN) { + break; + } + case ATOM_THEN: { v->handler = then_atom_handler; v->s = ref_filter_slopbuf; - continue; - } else if (atom_type == ATOM_ELSE) { + break; + } + case ATOM_ELSE: { v->handler = else_atom_handler; v->s = ref_filter_slopbuf; - continue; - } else if (atom_type == ATOM_REST) { + break; + } + case ATOM_REST: { if (ref->rest) v->s = xstrdup(ref->rest); else v->s = ref_filter_slopbuf; - continue; - } else - continue; - - if (!deref) - v->s = xstrdup(refname); - else - v->s = xstrfmt_len(&v->s_size, "%s^{}", refname); - free((char *)refname); + break; + } + default: + break; + } } for (i = 0; i < used_atom_cnt; i++) {