From patchwork Mon Jul 15 00:35:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Wong X-Patchwork-Id: 13732817 Received: from dcvr.yhbt.net (dcvr.yhbt.net [173.255.242.215]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 655BE360 for ; Mon, 15 Jul 2024 00:35:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=173.255.242.215 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721003734; cv=none; b=pb8ZiZfEnD13KWy9Ro9HLIpL38ujhUGRd8QJz29F5R5r/InSm3nqu1G+gIY2KvkKDc+RBMDA4qo6D+zLbyIEuYWxVaiVSlpo5D8Qz05rchf+jeQALcemRgijnfxZaeJoUosHmUNCY5J0PjeuD+mvRxigfaxhus5NzZDKatMxxPQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721003734; c=relaxed/simple; bh=mQudGzNO+h8/lc43I6VsFekp2tx9bSkvLl4h0JG336Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=elnESpsv9+VzdPmUY97QncWWSIDyf6to+394AEO2XXN+x3VAnbz64R28q1pxDryLRFMGjGYFpK0OuA1bILp3dvH3a1Yn/fAb/ReDHVOVs5v02dUfTEVZGM8WXht2cYv4MK/CwX/d6KGHhLevXo+ErMPUsWTlp27L/iqy/hxgcsc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=80x24.org; spf=pass smtp.mailfrom=80x24.org; dkim=pass (1024-bit key) header.d=80x24.org header.i=@80x24.org header.b=mdjyel7H; arc=none smtp.client-ip=173.255.242.215 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=80x24.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=80x24.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=80x24.org header.i=@80x24.org header.b="mdjyel7H" Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 01F921F5A4; Mon, 15 Jul 2024 00:35:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1721003720; bh=mQudGzNO+h8/lc43I6VsFekp2tx9bSkvLl4h0JG336Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mdjyel7Hex4cJIL91XPtPU3uAUTF/KSk8Q6yg1X8SuJcqS3UwWa99V/Db8vL32WNG BTuxDH9cDQ9hF60dqlLPmX5GcdO/RpxO6pW1nJYdRmItxGA5cwB2BhoMOzieNmuteL BBOeqEp3iznKMHy+nXQPQx+uH5Xk4Vivs5ZhN0a8= From: Eric Wong To: git@vger.kernel.org Cc: Jeff King Subject: [PATCH v1 01/10] packfile: move sizep computation Date: Mon, 15 Jul 2024 00:35:10 +0000 Message-ID: <20240715003519.2671385-2-e@80x24.org> In-Reply-To: <20240715003519.2671385-1-e@80x24.org> References: <20240715003519.2671385-1-e@80x24.org> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Jeff King This makes the next commit to avoid redundant object info lookups easier to understand. [ew: commit message] Signed-off-by: Jeff King Signed-off-by: Eric Wong --- packfile.c | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/packfile.c b/packfile.c index 813584646f..e547522e3d 100644 --- a/packfile.c +++ b/packfile.c @@ -1527,7 +1527,8 @@ int packed_object_info(struct repository *r, struct packed_git *p, /* * We always get the representation type, but only convert it to - * a "real" type later if the caller is interested. + * a "real" type later if the caller is interested. Likewise... + * tbd. */ if (oi->contentp) { *oi->contentp = cache_or_unpack_entry(r, p, obj_offset, oi->sizep, @@ -1536,24 +1537,24 @@ int packed_object_info(struct repository *r, struct packed_git *p, type = OBJ_BAD; } else { type = unpack_object_header(p, &w_curs, &curpos, &size); - } - if (!oi->contentp && oi->sizep) { - if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) { - off_t tmp_pos = curpos; - off_t base_offset = get_delta_base(p, &w_curs, &tmp_pos, - type, obj_offset); - if (!base_offset) { - type = OBJ_BAD; - goto out; + if (oi->sizep) { + if (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) { + off_t tmp_pos = curpos; + off_t base_offset = get_delta_base(p, &w_curs, &tmp_pos, + type, obj_offset); + if (!base_offset) { + type = OBJ_BAD; + goto out; + } + *oi->sizep = get_size_from_delta(p, &w_curs, tmp_pos); + if (*oi->sizep == 0) { + type = OBJ_BAD; + goto out; + } + } else { + *oi->sizep = size; } - *oi->sizep = get_size_from_delta(p, &w_curs, tmp_pos); - if (*oi->sizep == 0) { - type = OBJ_BAD; - goto out; - } - } else { - *oi->sizep = size; } } From patchwork Mon Jul 15 00:35:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Wong X-Patchwork-Id: 13732818 Received: from dcvr.yhbt.net (dcvr.yhbt.net [173.255.242.215]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C269C4C6D for ; Mon, 15 Jul 2024 00:35:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=173.255.242.215 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721003741; cv=none; b=Y+jajjQvY15HLbhutkXjxlSt6hb5VjDEvwWNl+RRwQJEwmHAgCX66T1jaQUIwbUDLlMg+BUUM64YLQB+qOuk01ZdfPl5S0vslO9TMGLMVekHdVuIwSyxU9dPpEIvSs45j+12te3hNRuv4yyO74EXlp8CG77hG8jaKPrXg5GZA8I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721003741; c=relaxed/simple; bh=avaCAlocWs3hEew496DjcxwDlUSY/cPYu07TNipBK1Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tpqw01SWjN8vaUlNBIW6LqhL6VfSRhUFTs8Mwwr3NH7XLh22MwZGeIrnf4jhO1g/NVMDWM5HT0h6MzGUhBNgncgIyFbxJbPAKRr+o/L8WTCzldN6ZmBl0Cxxoeqjb2oBW+OXGkNFM2Zj9cqeH60DKp+USaeUNZvdA+Qg8DN56XI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=80x24.org; spf=pass smtp.mailfrom=80x24.org; dkim=pass (1024-bit key) header.d=80x24.org header.i=@80x24.org header.b=YkosBdUt; arc=none smtp.client-ip=173.255.242.215 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=80x24.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=80x24.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=80x24.org header.i=@80x24.org header.b="YkosBdUt" Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 255F71F5A7; Mon, 15 Jul 2024 00:35:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1721003720; bh=avaCAlocWs3hEew496DjcxwDlUSY/cPYu07TNipBK1Y=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YkosBdUt3g1n717g9ul/aEfV3XQjnQN5Htp7wimxP8K7b0UOOnNP9W2Gbup9Q0ssP HGGQuYvvFMEl1S3YXF4uUQmtcU6Lk9POwTXe8xmmOcqirb9Wr+uW7iHXbdryy+XRrF oa7VfbUUoa7nD2GjE/Xx9BHrdIV16+A6oRZAOFU4= From: Eric Wong To: git@vger.kernel.org Cc: Jeff King Subject: [PATCH v1 02/10] packfile: allow content-limit for cat-file Date: Mon, 15 Jul 2024 00:35:11 +0000 Message-ID: <20240715003519.2671385-3-e@80x24.org> In-Reply-To: <20240715003519.2671385-1-e@80x24.org> References: <20240715003519.2671385-1-e@80x24.org> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Jeff King This avoids unnecessary round trips to the object store to speed up cat-file contents retrievals. The majority of packed objects don't benefit from the streaming interface at all and we end up having to load them in core anyways to satisfy our streaming API. This drops the runtime of `git cat-file --batch-all-objects --unordered --batch' from ~7.1s to ~6.1s on Jeff's machine. [ew: commit message] Signed-off-by: Jeff King Signed-off-by: Eric Wong --- builtin/cat-file.c | 17 +++++++++++++++-- object-file.c | 6 ++++++ object-store-ll.h | 1 + packfile.c | 13 ++++++++++++- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/builtin/cat-file.c b/builtin/cat-file.c index 18fe58d6b8..bc4bb89610 100644 --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@ -280,6 +280,7 @@ struct expand_data { off_t disk_size; const char *rest; struct object_id delta_base_oid; + void *content; /* * If mark_query is true, we do not expand anything, but rather @@ -383,7 +384,10 @@ static void print_object_or_die(struct batch_options *opt, struct expand_data *d assert(data->info.typep); - if (data->type == OBJ_BLOB) { + if (data->content) { + batch_write(opt, data->content, data->size); + FREE_AND_NULL(data->content); + } else if (data->type == OBJ_BLOB) { if (opt->buffer_output) fflush(stdout); if (opt->transform_mode) { @@ -801,9 +805,18 @@ static int batch_objects(struct batch_options *opt) /* * If we are printing out the object, then always fill in the type, * since we will want to decide whether or not to stream. + * + * Likewise, grab the content in the initial request if it's small + * and we're not planning to filter it. */ - if (opt->batch_mode == BATCH_MODE_CONTENTS) + if (opt->batch_mode == BATCH_MODE_CONTENTS) { data.info.typep = &data.type; + if (!opt->transform_mode) { + data.info.sizep = &data.size; + data.info.contentp = &data.content; + data.info.content_limit = big_file_threshold; + } + } if (opt->all_objects) { struct object_cb_data cb; diff --git a/object-file.c b/object-file.c index 065103be3e..1cc29c3c58 100644 --- a/object-file.c +++ b/object-file.c @@ -1492,6 +1492,12 @@ static int loose_object_info(struct repository *r, if (!oi->contentp) break; + if (oi->content_limit && *oi->sizep > oi->content_limit) { + git_inflate_end(&stream); + oi->contentp = NULL; + goto cleanup; + } + *oi->contentp = unpack_loose_rest(&stream, hdr, *oi->sizep, oid); if (*oi->contentp) goto cleanup; diff --git a/object-store-ll.h b/object-store-ll.h index c5f2bb2fc2..b71a15f590 100644 --- a/object-store-ll.h +++ b/object-store-ll.h @@ -289,6 +289,7 @@ struct object_info { struct object_id *delta_base_oid; struct strbuf *type_name; void **contentp; + size_t content_limit; /* Response */ enum { diff --git a/packfile.c b/packfile.c index e547522e3d..54b9d46928 100644 --- a/packfile.c +++ b/packfile.c @@ -1530,7 +1530,7 @@ int packed_object_info(struct repository *r, struct packed_git *p, * a "real" type later if the caller is interested. Likewise... * tbd. */ - if (oi->contentp) { + if (oi->contentp && !oi->content_limit) { *oi->contentp = cache_or_unpack_entry(r, p, obj_offset, oi->sizep, &type); if (!*oi->contentp) @@ -1556,6 +1556,17 @@ int packed_object_info(struct repository *r, struct packed_git *p, *oi->sizep = size; } } + + if (oi->contentp) { + if (oi->sizep && *oi->sizep < oi->content_limit) { + *oi->contentp = cache_or_unpack_entry(r, p, obj_offset, + oi->sizep, &type); + if (!*oi->contentp) + type = OBJ_BAD; + } else { + *oi->contentp = NULL; + } + } } if (oi->disk_sizep) { From patchwork Mon Jul 15 00:35:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Wong X-Patchwork-Id: 13732819 Received: from dcvr.yhbt.net (dcvr.yhbt.net [173.255.242.215]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 18B42360 for ; Mon, 15 Jul 2024 00:35:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=173.255.242.215 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721003748; cv=none; b=H2zRm1zYaCCv+ZBVrEdmFYCEgb1jtBpdYyAsvd81XbuJ3HNKlnH5ydGfikiVJ9gy7LzU/m0S60mwWF8V2ND/YxMzs/hggTIYepGfb6Vp9KzjK4vdUawFj9/wP9VmFBdLsU15NiMSjYKCUpDL9IsEQ0YXxtFqMuxpeHykUARXzVs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721003748; c=relaxed/simple; bh=78C0BLyFrigKYnHJcc6TtTsjlkmLDcEhL0fk29JbaTE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bfdqUqQKQ6BX4HFdLS2WfsdD7EevOOMpoLTMws8AF3CKcpp4dDaxN2RwwuMOG8OkB1oQHXrYkhn6f2Tib41T+iDt7fzsHuhQWHX5T6aFPHsydndra0vI3jkzJ7Abh4JoVarW9RtMyKzxasnSYOajF7nDkwcFy2OlyXBy4eKW3ow= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=80x24.org; spf=pass smtp.mailfrom=80x24.org; dkim=pass (1024-bit key) header.d=80x24.org header.i=@80x24.org header.b=xDY1IFxm; arc=none smtp.client-ip=173.255.242.215 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=80x24.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=80x24.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=80x24.org header.i=@80x24.org header.b="xDY1IFxm" Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 4B54F1F5B1; Mon, 15 Jul 2024 00:35:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1721003720; bh=78C0BLyFrigKYnHJcc6TtTsjlkmLDcEhL0fk29JbaTE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=xDY1IFxmyqyS7lDQboeyAefNBG4JClpy5i71Jyt44cQdUTLhpqlbMAU3+1Jonv1OR 2/57JIIMy8MyEUojDm5+0DaUBzYDU2fTmAEOfa7DdXRPpU02hrMdAa5tYMiFpQxW6u TKS/biay4WrCQ0xgg/ez/c3ScNkD0MCzfhcrD0cY= From: Eric Wong To: git@vger.kernel.org Cc: Jeff King Subject: [PATCH v1 03/10] packfile: fix off-by-one in content_limit comparison Date: Mon, 15 Jul 2024 00:35:12 +0000 Message-ID: <20240715003519.2671385-4-e@80x24.org> In-Reply-To: <20240715003519.2671385-1-e@80x24.org> References: <20240715003519.2671385-1-e@80x24.org> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 object-file.c::loose_object_info() accepts objects matching content_limit exactly, so it follows packfile handling allows slurping objects which match loose object handling and slurp objects with size matching the content_limit exactly. Signed-off-by: Eric Wong --- packfile.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packfile.c b/packfile.c index 54b9d46928..371da96cdb 100644 --- a/packfile.c +++ b/packfile.c @@ -1558,7 +1558,7 @@ int packed_object_info(struct repository *r, struct packed_git *p, } if (oi->contentp) { - if (oi->sizep && *oi->sizep < oi->content_limit) { + if (oi->sizep && *oi->sizep <= oi->content_limit) { *oi->contentp = cache_or_unpack_entry(r, p, obj_offset, oi->sizep, &type); if (!*oi->contentp) From patchwork Mon Jul 15 00:35:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Wong X-Patchwork-Id: 13732820 Received: from dcvr.yhbt.net (dcvr.yhbt.net [173.255.242.215]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A40614C6D for ; Mon, 15 Jul 2024 00:35:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=173.255.242.215 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721003755; cv=none; b=Ge7cIAzIjuzAeTuXtuY3ED36Tbu0Vp4S6P+wxv5MJ2XHXKI/kbjHCd696bCcWUsTFWr9FFW7B+JcPdmg7INC71KJrzH0t/6wz0X7O+0OyAptgdvpB4n1+WXh/1TT1kgQQ019nb3r7dxVVl7oYvhwuXuOMvRefFz18KIbS4OhdZ0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721003755; c=relaxed/simple; bh=WZBQ3mGXGyaF2er44iQ0z9ZuQHs0WIHcufbfV4YYIrs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=b4KWC9SHlfqWBsKYxfe4tbRZmqhaeXxH5QnUBH40abh1Ha/O4KqEaeqRDpQpqjFdIJSyzMwhgJBbBmHhP+bxR8BF+CgZA9chGkChiZUM8ZOEwZKiLP096FLNUWcgE0he84GZjmOK+pw55Z4XnXp1kEVvuyCDhes5N31+aluyIFw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=80x24.org; spf=pass smtp.mailfrom=80x24.org; dkim=pass (1024-bit key) header.d=80x24.org header.i=@80x24.org header.b=FsVYGROG; arc=none smtp.client-ip=173.255.242.215 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=80x24.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=80x24.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=80x24.org header.i=@80x24.org header.b="FsVYGROG" Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 6F2511F5B6; Mon, 15 Jul 2024 00:35:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1721003720; bh=WZBQ3mGXGyaF2er44iQ0z9ZuQHs0WIHcufbfV4YYIrs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FsVYGROGOHqn47+bd7wNHBiENILQP48BOF0YazcZxVvbbyz9w/ak7scMdjSTTT+7l Kx8gDIlPaGsdaimxVCLavo6NsmJ3qTZ2tnC2C9KIuz1tyMP1UVDTEneO4HsVZY6Cy7 udnb2CDAfV2J/VdBp2zlsLAuMA5BjM0Q/OWFo2IM= From: Eric Wong To: git@vger.kernel.org Cc: Jeff King Subject: [PATCH v1 04/10] packfile: inline cache_or_unpack_entry Date: Mon, 15 Jul 2024 00:35:13 +0000 Message-ID: <20240715003519.2671385-5-e@80x24.org> In-Reply-To: <20240715003519.2671385-1-e@80x24.org> References: <20240715003519.2671385-1-e@80x24.org> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 We need to check delta_base_cache anyways to fill in the `whence' field in `struct object_info'. Inlining cache_or_unpack_entry() makes it easier to only do the hashmap lookup once and avoid a redundant lookup later on. This code reorganization will also make an optimization to use the cache entry directly easier to implement. Signed-off-by: Eric Wong --- packfile.c | 48 +++++++++++++++++++++--------------------------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/packfile.c b/packfile.c index 371da96cdb..1a409ec142 100644 --- a/packfile.c +++ b/packfile.c @@ -1444,23 +1444,6 @@ static void detach_delta_base_cache_entry(struct delta_base_cache_entry *ent) free(ent); } -static void *cache_or_unpack_entry(struct repository *r, struct packed_git *p, - off_t base_offset, unsigned long *base_size, - enum object_type *type) -{ - struct delta_base_cache_entry *ent; - - ent = get_delta_base_cache_entry(p, base_offset); - if (!ent) - return unpack_entry(r, p, base_offset, type, base_size); - - if (type) - *type = ent->type; - if (base_size) - *base_size = ent->size; - return xmemdupz(ent->data, ent->size); -} - static inline void release_delta_base_cache(struct delta_base_cache_entry *ent) { free(ent->data); @@ -1521,21 +1504,36 @@ int packed_object_info(struct repository *r, struct packed_git *p, off_t obj_offset, struct object_info *oi) { struct pack_window *w_curs = NULL; - unsigned long size; off_t curpos = obj_offset; enum object_type type; + struct delta_base_cache_entry *ent; /* * We always get the representation type, but only convert it to * a "real" type later if the caller is interested. Likewise... * tbd. */ - if (oi->contentp && !oi->content_limit) { - *oi->contentp = cache_or_unpack_entry(r, p, obj_offset, oi->sizep, - &type); + oi->whence = OI_PACKED; + ent = get_delta_base_cache_entry(p, obj_offset); + if (ent) { + oi->whence = OI_DBCACHED; + type = ent->type; + if (oi->sizep) + *oi->sizep = ent->size; + if (oi->contentp) { + if (!oi->content_limit || + ent->size <= oi->content_limit) + *oi->contentp = xmemdupz(ent->data, ent->size); + else + *oi->contentp = NULL; /* caller must stream */ + } + } else if (oi->contentp && !oi->content_limit) { + *oi->contentp = unpack_entry(r, p, obj_offset, &type, + oi->sizep); if (!*oi->contentp) type = OBJ_BAD; } else { + unsigned long size; type = unpack_object_header(p, &w_curs, &curpos, &size); if (oi->sizep) { @@ -1559,8 +1557,8 @@ int packed_object_info(struct repository *r, struct packed_git *p, if (oi->contentp) { if (oi->sizep && *oi->sizep <= oi->content_limit) { - *oi->contentp = cache_or_unpack_entry(r, p, obj_offset, - oi->sizep, &type); + *oi->contentp = unpack_entry(r, p, obj_offset, + &type, oi->sizep); if (!*oi->contentp) type = OBJ_BAD; } else { @@ -1609,10 +1607,6 @@ int packed_object_info(struct repository *r, struct packed_git *p, } else oidclr(oi->delta_base_oid, the_repository->hash_algo); } - - oi->whence = in_delta_base_cache(p, obj_offset) ? OI_DBCACHED : - OI_PACKED; - out: unuse_pack(&w_curs); return type; From patchwork Mon Jul 15 00:35:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Wong X-Patchwork-Id: 13732821 Received: from dcvr.yhbt.net (dcvr.yhbt.net [173.255.242.215]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F103A4C62 for ; Mon, 15 Jul 2024 00:36:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=173.255.242.215 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721003762; cv=none; b=u7x/mhENn4Jat33tAuSddl+UVj7pZpOp+i6NuhjD85pv0khya5y76JG+af4l8LghRzUO+o7pxN6mfmRg+JaL9JP56MWD2u4Ylfv32ivIOwheeCkvKyuj1j/4FQh0uyCeD2r/QRNOlEdEqgp9ztHrBxOWbOEbMLY7syrPDpi8GpQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721003762; c=relaxed/simple; bh=6yiKnYC0VYfUAp1XQEs77kquAENxw9r4poLIMaZwh04=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GO1sKvwxylTOnCHlJNjrgs4gOE0G+0y60wrfamWGicJLW9dEtDzNeapYz2JRXvE8ZjsnnBIPO1Y8NApbITkIG58gLlQMDUWQXo7IeLGIVgTb4RQEdqVW7NJIAVXAvXJb694I6ycZ/YLEvbT4LWeHNzXOCxWPnPRD3afkLM2zjWo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=80x24.org; spf=pass smtp.mailfrom=80x24.org; dkim=pass (1024-bit key) header.d=80x24.org header.i=@80x24.org header.b=BF+4yOvv; arc=none smtp.client-ip=173.255.242.215 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=80x24.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=80x24.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=80x24.org header.i=@80x24.org header.b="BF+4yOvv" Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 92D5A1F5B7; Mon, 15 Jul 2024 00:35:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1721003720; bh=6yiKnYC0VYfUAp1XQEs77kquAENxw9r4poLIMaZwh04=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BF+4yOvvujZ3BA44F1x3cYdKUKFKGa9uIdlXbQsnDGFJkpNnsjvzZI6oZW99VLr1s DgnUHCpqQBTa1uRZpG+jxBcr5NCJC/KNc7J3gU9N9Sr67bC/oBoe6s8bD4uj5fO54D +Pi9TMOUXzkzPxwExr6DY33bXAm29tec+Km1uIxQ= From: Eric Wong To: git@vger.kernel.org Cc: Jeff King Subject: [PATCH v1 05/10] cat-file: use delta_base_cache entries directly Date: Mon, 15 Jul 2024 00:35:14 +0000 Message-ID: <20240715003519.2671385-6-e@80x24.org> In-Reply-To: <20240715003519.2671385-1-e@80x24.org> References: <20240715003519.2671385-1-e@80x24.org> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 For objects already in the delta_base_cache, we can safely use them directly to avoid the malloc+memcpy+free overhead. While only 2-7% of objects are delta bases in repos I've looked at, this avoids up to 96MB of duplicated memory in the worst case with the default git config. For a more reasonable 1MB delta base object, this eliminates the speed penalty of duplicating large objects into memory and speeds up those 1MB delta base cached content retrievals by roughly 30%. The new delta_base_cache_lock is a simple single-threaded assertion to ensure cat-file is the exclusive user of the delta_base_cache. Signed-off-by: Eric Wong --- builtin/cat-file.c | 15 ++++++++++++++- object-file.c | 5 +++++ object-store-ll.h | 7 +++++++ packfile.c | 28 +++++++++++++++++++++++++--- packfile.h | 4 ++++ 5 files changed, 55 insertions(+), 4 deletions(-) diff --git a/builtin/cat-file.c b/builtin/cat-file.c index bc4bb89610..769c8b48d2 100644 --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@ -24,6 +24,7 @@ #include "promisor-remote.h" #include "mailmap.h" #include "write-or-die.h" +#define USE_DIRECT_CACHE 1 enum batch_mode { BATCH_MODE_CONTENTS, @@ -386,7 +387,18 @@ static void print_object_or_die(struct batch_options *opt, struct expand_data *d if (data->content) { batch_write(opt, data->content, data->size); - FREE_AND_NULL(data->content); + switch (data->info.whence) { + case OI_CACHED: BUG("FIXME OI_CACHED support not done"); + case OI_LOOSE: + case OI_PACKED: + FREE_AND_NULL(data->content); + break; + case OI_DBCACHED: + if (USE_DIRECT_CACHE) + unlock_delta_base_cache(); + else + FREE_AND_NULL(data->content); + } } else if (data->type == OBJ_BLOB) { if (opt->buffer_output) fflush(stdout); @@ -815,6 +827,7 @@ static int batch_objects(struct batch_options *opt) data.info.sizep = &data.size; data.info.contentp = &data.content; data.info.content_limit = big_file_threshold; + data.info.direct_cache = USE_DIRECT_CACHE; } } diff --git a/object-file.c b/object-file.c index 1cc29c3c58..19100e823d 100644 --- a/object-file.c +++ b/object-file.c @@ -1586,6 +1586,11 @@ static int do_oid_object_info_extended(struct repository *r, oidclr(oi->delta_base_oid, the_repository->hash_algo); if (oi->type_name) strbuf_addstr(oi->type_name, type_name(co->type)); + /* + * Currently `blame' is the only command which creates + * OI_CACHED, and direct_cache is only used by `cat-file'. + */ + assert(!oi->direct_cache); if (oi->contentp) *oi->contentp = xmemdupz(co->buf, co->size); oi->whence = OI_CACHED; diff --git a/object-store-ll.h b/object-store-ll.h index b71a15f590..50c5219308 100644 --- a/object-store-ll.h +++ b/object-store-ll.h @@ -298,6 +298,13 @@ struct object_info { OI_PACKED, OI_DBCACHED } whence; + + /* + * set if caller is able to use OI_DBCACHED entries without copying + * TODO OI_CACHED if its use goes beyond blame + */ + unsigned direct_cache:1; + union { /* * struct { diff --git a/packfile.c b/packfile.c index 1a409ec142..b2660e14f9 100644 --- a/packfile.c +++ b/packfile.c @@ -1362,6 +1362,9 @@ static enum object_type packed_to_object_type(struct repository *r, static struct hashmap delta_base_cache; static size_t delta_base_cached; +/* ensures oi->direct_cache is used properly */ +static int delta_base_cache_lock; + static LIST_HEAD(delta_base_cache_lru); struct delta_base_cache_key { @@ -1444,6 +1447,18 @@ static void detach_delta_base_cache_entry(struct delta_base_cache_entry *ent) free(ent); } +static void lock_delta_base_cache(void) +{ + delta_base_cache_lock++; + assert(delta_base_cache_lock == 1); +} + +void unlock_delta_base_cache(void) +{ + delta_base_cache_lock--; + assert(delta_base_cache_lock == 0); +} + static inline void release_delta_base_cache(struct delta_base_cache_entry *ent) { free(ent->data); @@ -1453,6 +1468,7 @@ static inline void release_delta_base_cache(struct delta_base_cache_entry *ent) void clear_delta_base_cache(void) { struct list_head *lru, *tmp; + assert(!delta_base_cache_lock); list_for_each_safe(lru, tmp, &delta_base_cache_lru) { struct delta_base_cache_entry *entry = list_entry(lru, struct delta_base_cache_entry, lru); @@ -1466,6 +1482,7 @@ static void add_delta_base_cache(struct packed_git *p, off_t base_offset, struct delta_base_cache_entry *ent; struct list_head *lru, *tmp; + assert(!delta_base_cache_lock); /* * Check required to avoid redundant entries when more than one thread * is unpacking the same object, in unpack_entry() (since its phases I @@ -1521,11 +1538,16 @@ int packed_object_info(struct repository *r, struct packed_git *p, if (oi->sizep) *oi->sizep = ent->size; if (oi->contentp) { - if (!oi->content_limit || - ent->size <= oi->content_limit) + /* ignore content_limit if avoiding copy from cache */ + if (oi->direct_cache) { + lock_delta_base_cache(); + *oi->contentp = ent->data; + } else if (!oi->content_limit || + ent->size <= oi->content_limit) { *oi->contentp = xmemdupz(ent->data, ent->size); - else + } else { *oi->contentp = NULL; /* caller must stream */ + } } } else if (oi->contentp && !oi->content_limit) { *oi->contentp = unpack_entry(r, p, obj_offset, &type, diff --git a/packfile.h b/packfile.h index eb18ec15db..94941bbe80 100644 --- a/packfile.h +++ b/packfile.h @@ -210,4 +210,8 @@ int is_promisor_object(const struct object_id *oid); int load_idx(const char *path, const unsigned int hashsz, void *idx_map, size_t idx_size, struct packed_git *p); +/* + * release lock acquired via oi->direct_cache + */ +void unlock_delta_base_cache(void); #endif From patchwork Mon Jul 15 00:35:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Wong X-Patchwork-Id: 13732822 Received: from dcvr.yhbt.net (dcvr.yhbt.net [173.255.242.215]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D11304C62 for ; Mon, 15 Jul 2024 00:36:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=173.255.242.215 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721003769; cv=none; b=Wn94w3t7htrKxPPPB7W/dAUZXxa0oOj1Mpx5pE0Ei92iRXl6MXoJ6AT8I1ZbV5o49gyRxBUTfbF9KAxuAYNrQXeogTKYBfAlwsxqlwRJyynHlRxXu+XB5r6OICqijduQlvlMrlJUmfH4EfL5yAfrDEqx/ADBzTF/reizYzxwb4c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721003769; c=relaxed/simple; bh=JOhPhjCzcf5BarZEBp2Wfhcc1L/NxMLH9Uc/P1tnxCQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qGCTBO26Bu1Fy3KU6PtBh8+DxzHQS5a9JiJ5CX0xRfmoaPItbH9zztRWxoGbgYG5Sydz37FyArYJCJH3bSwQhnWiztbheNAkRoXOifZJy4uE/ZbqybrmoDzRNh80tY0mSk27qnBICsLk8RgW4BM0UzsmqI0Th7u/hMzkAvW1OYE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=80x24.org; spf=pass smtp.mailfrom=80x24.org; dkim=pass (1024-bit key) header.d=80x24.org header.i=@80x24.org header.b=k93l/Cxy; arc=none smtp.client-ip=173.255.242.215 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=80x24.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=80x24.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=80x24.org header.i=@80x24.org header.b="k93l/Cxy" Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id BD3401F5B8; Mon, 15 Jul 2024 00:35:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1721003720; bh=JOhPhjCzcf5BarZEBp2Wfhcc1L/NxMLH9Uc/P1tnxCQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=k93l/CxyxU2REnocMNvlrLqPqlJ2YJo0Gyds17uMrf2ra+njPmIgCJ+1PIUF+g6x0 euQzzIDquoxKZKhKCiwaQxx8LboXitvv4GRIx538I3Xd1fYn79He3hWJhGI5w7lB6F ccEYYRzonRsMfr84u9/F7Fptf+4twIJyK2xKKvfo= From: Eric Wong To: git@vger.kernel.org Cc: Jeff King Subject: [PATCH v1 06/10] packfile: packed_object_info avoids packed_to_object_type Date: Mon, 15 Jul 2024 00:35:15 +0000 Message-ID: <20240715003519.2671385-7-e@80x24.org> In-Reply-To: <20240715003519.2671385-1-e@80x24.org> References: <20240715003519.2671385-1-e@80x24.org> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 For calls the delta base cache, packed_to_object_type calls can be omitted. This prepares us to bypass content_limit for non-blob types in the following commit. Signed-off-by: Eric Wong --- packfile.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/packfile.c b/packfile.c index b2660e14f9..c2ba6ab203 100644 --- a/packfile.c +++ b/packfile.c @@ -1522,7 +1522,7 @@ int packed_object_info(struct repository *r, struct packed_git *p, { struct pack_window *w_curs = NULL; off_t curpos = obj_offset; - enum object_type type; + enum object_type type, final_type = OBJ_BAD; struct delta_base_cache_entry *ent; /* @@ -1534,7 +1534,7 @@ int packed_object_info(struct repository *r, struct packed_git *p, ent = get_delta_base_cache_entry(p, obj_offset); if (ent) { oi->whence = OI_DBCACHED; - type = ent->type; + final_type = type = ent->type; if (oi->sizep) *oi->sizep = ent->size; if (oi->contentp) { @@ -1552,6 +1552,7 @@ int packed_object_info(struct repository *r, struct packed_git *p, } else if (oi->contentp && !oi->content_limit) { *oi->contentp = unpack_entry(r, p, obj_offset, &type, oi->sizep); + final_type = type; if (!*oi->contentp) type = OBJ_BAD; } else { @@ -1581,6 +1582,7 @@ int packed_object_info(struct repository *r, struct packed_git *p, if (oi->sizep && *oi->sizep <= oi->content_limit) { *oi->contentp = unpack_entry(r, p, obj_offset, &type, oi->sizep); + final_type = type; if (!*oi->contentp) type = OBJ_BAD; } else { @@ -1602,17 +1604,17 @@ int packed_object_info(struct repository *r, struct packed_git *p, } if (oi->typep || oi->type_name) { - enum object_type ptot; - ptot = packed_to_object_type(r, p, obj_offset, - type, &w_curs, curpos); + if (final_type < 0) + final_type = packed_to_object_type(r, p, obj_offset, + type, &w_curs, curpos); if (oi->typep) - *oi->typep = ptot; + *oi->typep = final_type; if (oi->type_name) { - const char *tn = type_name(ptot); + const char *tn = type_name(final_type); if (tn) strbuf_addstr(oi->type_name, tn); } - if (ptot < 0) { + if (final_type < 0) { type = OBJ_BAD; goto out; } From patchwork Mon Jul 15 00:35:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Wong X-Patchwork-Id: 13732823 Received: from dcvr.yhbt.net (dcvr.yhbt.net [173.255.242.215]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 103F84C62 for ; Mon, 15 Jul 2024 00:36:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=173.255.242.215 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721003777; cv=none; b=ObIEKn3UYeVrfSNBNU1C4wM/G62NzCnAkmlmC8YDVH7kqWoCp0vET3MIhyvY48rhiVaxvpT2BvY1CBWIttWGaLpGgIw6fYTuXlWsi3KDsbpljfLMUDT8rfNLPVX8ARVuyqeF+EFP3LjKyj450cEi3KQya/Mgfs/JWWxzR/mIjGE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721003777; c=relaxed/simple; bh=TVjtD0jkM2pCp+tx52kmK9b+zEKH319AJPyaDHiN4HU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JPApfdJLRzY+HHC1c8XXzxi2IGpdebY8U8q0WbUk0Y5erdFXGKXpKq4C7m4fEwbWtBNQevuMvgrhMIDGYDoNeMPWU9BygzLd9h8ubzp8H+DVs3z5x2Cw483q+ArmII22sqswPNGwBB0i7o2YGQyFmWMiXNo9VLy3l9/jlb07Q+c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=80x24.org; spf=pass smtp.mailfrom=80x24.org; dkim=pass (1024-bit key) header.d=80x24.org header.i=@80x24.org header.b=UpttgpnT; arc=none smtp.client-ip=173.255.242.215 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=80x24.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=80x24.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=80x24.org header.i=@80x24.org header.b="UpttgpnT" Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id E065B1F5B9; Mon, 15 Jul 2024 00:35:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1721003720; bh=TVjtD0jkM2pCp+tx52kmK9b+zEKH319AJPyaDHiN4HU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UpttgpnT/3Sntz6LotMJ8XwRzYluxzXpJjD7ku+GFScMPD1YcTbTQmJA8v7awN6xZ bPaNhiZHD37wkkMHXp0rhNpehx1P7hNxh+OpW4+/MBfPUrzu+VOw7BRcCPqtsBNKeR V3Pz2cUBCvVznYfTHy4LhY/J3C7yyQObuFq6bbBI= From: Eric Wong To: git@vger.kernel.org Cc: Jeff King Subject: [PATCH v1 07/10] object_info: content_limit only applies to blobs Date: Mon, 15 Jul 2024 00:35:16 +0000 Message-ID: <20240715003519.2671385-8-e@80x24.org> In-Reply-To: <20240715003519.2671385-1-e@80x24.org> References: <20240715003519.2671385-1-e@80x24.org> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Streaming is only supported for blobs, so we'd end up having to slurp all the other object types into memory regardless. So slurp all the non-blob types up front when requesting content since we always handle them in-core, anyways. Signed-off-by: Eric Wong --- builtin/cat-file.c | 51 +++++++++++++++++++++------------------------- object-file.c | 3 ++- packfile.c | 8 +++++--- 3 files changed, 30 insertions(+), 32 deletions(-) diff --git a/builtin/cat-file.c b/builtin/cat-file.c index 769c8b48d2..0752ff7a74 100644 --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@ -386,20 +386,39 @@ static void print_object_or_die(struct batch_options *opt, struct expand_data *d assert(data->info.typep); if (data->content) { - batch_write(opt, data->content, data->size); + void *content = data->content; + unsigned long size = data->size; + + data->content = NULL; + if (use_mailmap && (data->type == OBJ_COMMIT || + data->type == OBJ_TAG)) { + size_t s = size; + + if (USE_DIRECT_CACHE && + data->info.whence == OI_DBCACHED) { + content = xmemdupz(content, s); + data->info.whence = OI_PACKED; + } + + content = replace_idents_using_mailmap(content, &s); + size = cast_size_t_to_ulong(s); + } + + batch_write(opt, content, size); switch (data->info.whence) { case OI_CACHED: BUG("FIXME OI_CACHED support not done"); case OI_LOOSE: case OI_PACKED: - FREE_AND_NULL(data->content); + free(content); break; case OI_DBCACHED: if (USE_DIRECT_CACHE) unlock_delta_base_cache(); else - FREE_AND_NULL(data->content); + free(content); } - } else if (data->type == OBJ_BLOB) { + } else { + assert(data->type == OBJ_BLOB); if (opt->buffer_output) fflush(stdout); if (opt->transform_mode) { @@ -434,30 +453,6 @@ static void print_object_or_die(struct batch_options *opt, struct expand_data *d stream_blob(oid); } } - else { - enum object_type type; - unsigned long size; - void *contents; - - contents = repo_read_object_file(the_repository, oid, &type, - &size); - if (!contents) - die("object %s disappeared", oid_to_hex(oid)); - - if (use_mailmap) { - size_t s = size; - contents = replace_idents_using_mailmap(contents, &s); - size = cast_size_t_to_ulong(s); - } - - if (type != data->type) - die("object %s changed type!?", oid_to_hex(oid)); - if (data->info.sizep && size != data->size && !use_mailmap) - die("object %s changed size!?", oid_to_hex(oid)); - - batch_write(opt, contents, size); - free(contents); - } } static void print_default_format(struct strbuf *scratch, struct expand_data *data, diff --git a/object-file.c b/object-file.c index 19100e823d..59842cfe1b 100644 --- a/object-file.c +++ b/object-file.c @@ -1492,7 +1492,8 @@ static int loose_object_info(struct repository *r, if (!oi->contentp) break; - if (oi->content_limit && *oi->sizep > oi->content_limit) { + if (oi->content_limit && *oi->typep == OBJ_BLOB && + *oi->sizep > oi->content_limit) { git_inflate_end(&stream); oi->contentp = NULL; goto cleanup; diff --git a/packfile.c b/packfile.c index c2ba6ab203..01ce3a49db 100644 --- a/packfile.c +++ b/packfile.c @@ -1542,7 +1542,7 @@ int packed_object_info(struct repository *r, struct packed_git *p, if (oi->direct_cache) { lock_delta_base_cache(); *oi->contentp = ent->data; - } else if (!oi->content_limit || + } else if (type != OBJ_BLOB || !oi->content_limit || ent->size <= oi->content_limit) { *oi->contentp = xmemdupz(ent->data, ent->size); } else { @@ -1579,10 +1579,12 @@ int packed_object_info(struct repository *r, struct packed_git *p, } if (oi->contentp) { - if (oi->sizep && *oi->sizep <= oi->content_limit) { + final_type = packed_to_object_type(r, p, obj_offset, + type, &w_curs, curpos); + if (final_type != OBJ_BLOB || (oi->sizep && + *oi->sizep <= oi->content_limit)) { *oi->contentp = unpack_entry(r, p, obj_offset, &type, oi->sizep); - final_type = type; if (!*oi->contentp) type = OBJ_BAD; } else { From patchwork Mon Jul 15 00:35:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Wong X-Patchwork-Id: 13732824 Received: from dcvr.yhbt.net (dcvr.yhbt.net [173.255.242.215]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 764494C62 for ; Mon, 15 Jul 2024 00:36:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=173.255.242.215 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721003783; cv=none; b=nQ5IH/THxmBxize46Ycd3lUz2JO8UYgp7vwBk+dIYpTv0jUqMU0v9H//h0xFEz5wUSn8/ITnGR9YUQz92QCA0OoASzDfxI+bc0Zy7RLkztaY50bHfRKqPhech8sjCuGsfM2IdxRs0kBF50jlzzJYlDMbvN+0/tbsZb9HCmlPJqc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721003783; c=relaxed/simple; bh=kFW1zCBR8NfwsOKyKBr5hnsC3QkrlJ9a7h03TapSl20=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ssjBngXvk/vqsZlrlGhzYLIHnBd/ie6fTOk/T3svjyYrO8FDa5NQCNYMchAwV9LvD+BJPYqAMId9XbKELtQ8/6hVRBVJf4/ZTU0ZzyrATPkcRdhHMLkoA2Gv5cz9XI6+dxZ1macPxhOV5zziEiRnaoYgR9Ch/SUxXVhSFR5Xb50= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=80x24.org; spf=pass smtp.mailfrom=80x24.org; dkim=pass (1024-bit key) header.d=80x24.org header.i=@80x24.org header.b=T6+gu/kU; arc=none smtp.client-ip=173.255.242.215 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=80x24.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=80x24.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=80x24.org header.i=@80x24.org header.b="T6+gu/kU" Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 0E3821F5BA; Mon, 15 Jul 2024 00:35:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1721003721; bh=kFW1zCBR8NfwsOKyKBr5hnsC3QkrlJ9a7h03TapSl20=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=T6+gu/kUfDCrJwpGmIFU5mRsL7Iir86/xxiMxXToTNwC9EmuOMlVGPKuIcRVLvtUX CDmfEy5D7Dgrl+pwnMaB+AKvuIJJbNe0w7k2J+8QkyainIoptnhPRPiBpsIyppzWWS TO3LOwWFb38qDB2Ad+D1m3ogH62HLUlPLIWIW8+g= From: Eric Wong To: git@vger.kernel.org Cc: Jeff King Subject: [PATCH v1 08/10] cat-file: batch-command uses content_limit Date: Mon, 15 Jul 2024 00:35:17 +0000 Message-ID: <20240715003519.2671385-9-e@80x24.org> In-Reply-To: <20240715003519.2671385-1-e@80x24.org> References: <20240715003519.2671385-1-e@80x24.org> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 As with the normal `--batch' mode, we can use the content_limit round trip optimization to avoid a redundant lookup. The only tricky thing here is we need to enable/disable setting the object_info.contentp field depending on whether we hit an `info' or `contents' command. t1006 is updated to ensure we can switch back and forth between `info' and `contents' commands without problems. Signed-off-by: Eric Wong --- builtin/cat-file.c | 5 ++++- t/t1006-cat-file.sh | 19 ++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/builtin/cat-file.c b/builtin/cat-file.c index 0752ff7a74..c4c28236db 100644 --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@ -666,6 +666,7 @@ static void parse_cmd_contents(struct batch_options *opt, struct expand_data *data) { opt->batch_mode = BATCH_MODE_CONTENTS; + data->info.contentp = &data->content; batch_one_object(line, output, opt, data); } @@ -675,6 +676,7 @@ static void parse_cmd_info(struct batch_options *opt, struct expand_data *data) { opt->batch_mode = BATCH_MODE_INFO; + data->info.contentp = NULL; batch_one_object(line, output, opt, data); } @@ -816,7 +818,8 @@ static int batch_objects(struct batch_options *opt) * Likewise, grab the content in the initial request if it's small * and we're not planning to filter it. */ - if (opt->batch_mode == BATCH_MODE_CONTENTS) { + if ((opt->batch_mode == BATCH_MODE_CONTENTS) || + (opt->batch_mode == BATCH_MODE_QUEUE_AND_DISPATCH)) { data.info.typep = &data.type; if (!opt->transform_mode) { data.info.sizep = &data.size; diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh index ff9bf213aa..841e8567e9 100755 --- a/t/t1006-cat-file.sh +++ b/t/t1006-cat-file.sh @@ -622,20 +622,33 @@ test_expect_success 'confirm that neither loose blob is a delta' ' test_cmp expect actual ' +test_expect_success 'setup delta base tests' ' + foo="$(git rev-parse HEAD:foo)" && + foo_plus="$(git rev-parse HEAD:foo-plus)" && + git repack -ad +' + # To avoid relying too much on the current delta heuristics, # we will check only that one of the two objects is a delta # against the other, but not the order. We can do so by just # asking for the base of both, and checking whether either # oid appears in the output. test_expect_success '%(deltabase) reports packed delta bases' ' - git repack -ad && git cat-file --batch-check="%(deltabase)" actual && { - grep "$(git rev-parse HEAD:foo)" actual || - grep "$(git rev-parse HEAD:foo-plus)" actual + grep "$foo" actual || grep "$foo_plus" actual } ' +test_expect_success 'delta base direct cache use succeeds w/o asserting' ' + commands="info $foo +info $foo_plus +contents $foo_plus +contents $foo" && + echo "$commands" >in && + git cat-file --batch-command out +' + test_expect_success 'setup bogus data' ' bogus_short_type="bogus" && bogus_short_content="bogus" && From patchwork Mon Jul 15 00:35:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Wong X-Patchwork-Id: 13732825 Received: from dcvr.yhbt.net (dcvr.yhbt.net [173.255.242.215]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 88D1A4C66 for ; Mon, 15 Jul 2024 00:36:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=173.255.242.215 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721003789; cv=none; b=AHHOZBTsqfZQlf1sbdo+P2Ne7H8vg5bDsXcjYz2m2lALLDhfWo8jgxUhWziS4dhcB51qF+orXgXgUsmcZKAzZ7+HP8CnLr/zOn4fyh3W65p8zM4a5UDLjcJjjIkQtv+rRrQCeN17Dx3Y+5o58G9SDqgTpXDBvgLDHcy5QF4EAjI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721003789; c=relaxed/simple; bh=2s4jc2uIvjx9nV/oRLdP8XcShrteL9+cYU2LpNVK030=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NG4ARMm/0h2P5HDHdNkxxlUP9MDnOKuov8/AKpkVEbNtFyDjaW0i0VcazOUVDwBk8Hfzw3JnMmU40x0mfGuTuw86Ak24/vpWjtYEH0oQbQuDci0vKse+dJGEg3gZB47b6d18Owwjlkikswk4Mqubl3WgUl6SoGnOQue922WQ1MY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=80x24.org; spf=pass smtp.mailfrom=80x24.org; dkim=pass (1024-bit key) header.d=80x24.org header.i=@80x24.org header.b=KrRrNMte; arc=none smtp.client-ip=173.255.242.215 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=80x24.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=80x24.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=80x24.org header.i=@80x24.org header.b="KrRrNMte" Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 312651F5BB; Mon, 15 Jul 2024 00:35:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1721003721; bh=2s4jc2uIvjx9nV/oRLdP8XcShrteL9+cYU2LpNVK030=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KrRrNMteueH8M9nAMuDbsvd0Hg1P5ng7+U3f+bVdMntLrIJaxg2Cp0/jgf5ClJly0 yY3XBBiPU7FK8EvZ7cXRizKAqtHLS8iJ6jwowR9Nimj0l6PwENs5KquSpTKXiN/b0M QGr7WPx6IbAn+r+6srRZakS6/yphM8+RI8+haxZY= From: Eric Wong To: git@vger.kernel.org Cc: Jeff King Subject: [PATCH v1 09/10] cat-file: batch_write: use size_t for length Date: Mon, 15 Jul 2024 00:35:18 +0000 Message-ID: <20240715003519.2671385-10-e@80x24.org> In-Reply-To: <20240715003519.2671385-1-e@80x24.org> References: <20240715003519.2671385-1-e@80x24.org> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 fwrite(3) and write(2), and all of our wrappers for them use size_t while object size is `unsigned long', so there's no excuse to use a potentially smaller representation. Signed-off-by: Eric Wong --- builtin/cat-file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builtin/cat-file.c b/builtin/cat-file.c index c4c28236db..efc0df760c 100644 --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@ -370,7 +370,7 @@ static void expand_format(struct strbuf *sb, const char *start, } } -static void batch_write(struct batch_options *opt, const void *data, int len) +static void batch_write(struct batch_options *opt, const void *data, size_t len) { if (opt->buffer_output) { if (fwrite(data, 1, len, stdout) != len) From patchwork Mon Jul 15 00:35:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Wong X-Patchwork-Id: 13732826 Received: from dcvr.yhbt.net (dcvr.yhbt.net [173.255.242.215]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 288184C62 for ; Mon, 15 Jul 2024 00:36:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=173.255.242.215 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721003796; cv=none; b=J0EqVSftdVPX4K2tRSFAlj7uMCxoQlhzeMf1uH40JQxEZl8eNXAsXan+EqwuLvVBtJpWCTK2aKi7uRhNVjEQOsIH9sc8dfkb6ay1UFZUG4p6XCkvZMDNJSlyCmcT5FQjGTTj/f8nZs0ERkTokRvjYiPRoP7X/PnjebaBbEsbbLE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721003796; c=relaxed/simple; bh=cSq70DKbuFektOuV8tRuZxbCUIlcBznkzHXmZLZXZA0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kM0tINMiEieuWhmYYmFzITKRsZtAxBVD7Qtnob0ibblG3Kv5cXESH4lWoU2YBjOPkoHZ4Ba06KtMyr4QHy/MZSX3JvJ9Y9ZU0nGh5gJvy51qb2F9RmOIb4webJmB986C8Oei3wJcoOS4+H5iEytSQtc+75NyQ2KjFLhps3+8lUg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=80x24.org; spf=pass smtp.mailfrom=80x24.org; dkim=pass (1024-bit key) header.d=80x24.org header.i=@80x24.org header.b=WAlkWVkp; arc=none smtp.client-ip=173.255.242.215 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=80x24.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=80x24.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=80x24.org header.i=@80x24.org header.b="WAlkWVkp" Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 502441F5BC; Mon, 15 Jul 2024 00:35:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1721003721; bh=cSq70DKbuFektOuV8tRuZxbCUIlcBznkzHXmZLZXZA0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WAlkWVkp7p8bW2tOa9SZk7p/kOxFMBjyaoiDkIioJMbG8IkvyfOvhJUjMw6pe3NjX JIZJKkHqMg2EtMn7v2Bli7Zjyp7u5DPGPkbyYJuutTmm5q8osGaPVP7lZdzA9rx+8F EcAD4pPE1e2Pa7HR4IP9Y9eH9fNXRV6r3n7VZe5U= From: Eric Wong To: git@vger.kernel.org Cc: Jeff King Subject: [PATCH v1 10/10] cat-file: use writev(2) if available Date: Mon, 15 Jul 2024 00:35:19 +0000 Message-ID: <20240715003519.2671385-11-e@80x24.org> In-Reply-To: <20240715003519.2671385-1-e@80x24.org> References: <20240715003519.2671385-1-e@80x24.org> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Using writev here is can be 20-40% faster than three write syscalls in succession for smaller (1-10k) objects in the delta base cache. This advantage decreases as object sizes approach pipe size (64k on Linux). This reduces wakeups and syscalls on the read side, as well, especially if the reader is relying on non-blocking I/O. Unfortunately, this turns into a small (1-3%) slowdown for gigantic objects of a megabyte or more even with after increasing pipe size to 1MB via the F_SETPIPE_SZ fcntl(2) op. This slowdown is acceptable to me since the vast majority of objects are 64K or less for projects I've looked at. Relying on stdio buffering and fflush(3) after each response was considered for users without --buffer, but historically cat-file defaults to being compatible with non-blocking stdout and able to poll(2) after hitting EAGAIN on write(2). Using stdio on files with the O_NONBLOCK flag is (AFAIK) unspecified and likely subject to portability problems. Signed-off-by: Eric Wong --- Makefile | 3 +++ builtin/cat-file.c | 62 ++++++++++++++++++++++++++++++------------- config.mak.uname | 5 ++++ git-compat-util.h | 10 +++++++ wrapper.c | 18 +++++++++++++ wrapper.h | 1 + write-or-die.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++ write-or-die.h | 2 ++ 8 files changed, 149 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 3eab701b10..c7a062de00 100644 --- a/Makefile +++ b/Makefile @@ -1844,6 +1844,9 @@ ifdef NO_PREAD COMPAT_CFLAGS += -DNO_PREAD COMPAT_OBJS += compat/pread.o endif +ifdef HAVE_WRITEV + COMPAT_CFLAGS += -DHAVE_WRITEV +endif ifdef NO_FAST_WORKING_DIRECTORY BASIC_CFLAGS += -DNO_FAST_WORKING_DIRECTORY endif diff --git a/builtin/cat-file.c b/builtin/cat-file.c index efc0df760c..0a448e82a7 100644 --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@ -281,7 +281,7 @@ struct expand_data { off_t disk_size; const char *rest; struct object_id delta_base_oid; - void *content; + struct git_iovec iov[3]; /* * If mark_query is true, we do not expand anything, but rather @@ -379,17 +379,42 @@ static void batch_write(struct batch_options *opt, const void *data, size_t len) write_or_die(1, data, len); } -static void print_object_or_die(struct batch_options *opt, struct expand_data *data) +static void batch_writev(struct batch_options *opt, struct expand_data *data, + const struct strbuf *hdr, size_t size) +{ + data->iov[0].iov_base = hdr->buf; + data->iov[0].iov_len = hdr->len; + data->iov[1].iov_len = size; + + /* + * Copying a (8|16)-byte iovec for a single byte is gross, but my + * attempt to stuff output_delim into the trailing NUL byte of + * iov[1].iov_base (and restoring it after writev(2) for the + * OI_DBCACHED case) to drop iovcnt from 3->2 wasn't faster. + */ + data->iov[2].iov_base = &opt->output_delim; + data->iov[2].iov_len = 1; + + if (opt->buffer_output) + fwritev_or_die(stdout, data->iov, 3); + else + writev_or_die(1, data->iov, 3); + + /* writev_or_die may move iov[1].iov_base, so it's invalid */ + data->iov[1].iov_base = NULL; +} + +static void print_object_or_die(struct batch_options *opt, + struct expand_data *data, struct strbuf *hdr) { const struct object_id *oid = &data->oid; assert(data->info.typep); - if (data->content) { - void *content = data->content; + if (data->iov[1].iov_base) { + void *content = data->iov[1].iov_base; unsigned long size = data->size; - data->content = NULL; if (use_mailmap && (data->type == OBJ_COMMIT || data->type == OBJ_TAG)) { size_t s = size; @@ -401,10 +426,10 @@ static void print_object_or_die(struct batch_options *opt, struct expand_data *d } content = replace_idents_using_mailmap(content, &s); + data->iov[1].iov_base = content; size = cast_size_t_to_ulong(s); } - - batch_write(opt, content, size); + batch_writev(opt, data, hdr, size); switch (data->info.whence) { case OI_CACHED: BUG("FIXME OI_CACHED support not done"); case OI_LOOSE: @@ -419,8 +444,6 @@ static void print_object_or_die(struct batch_options *opt, struct expand_data *d } } else { assert(data->type == OBJ_BLOB); - if (opt->buffer_output) - fflush(stdout); if (opt->transform_mode) { char *contents; unsigned long size; @@ -447,10 +470,15 @@ static void print_object_or_die(struct batch_options *opt, struct expand_data *d oid_to_hex(oid), data->rest); } else BUG("invalid transform_mode: %c", opt->transform_mode); - batch_write(opt, contents, size); + data->iov[1].iov_base = contents; + batch_writev(opt, data, hdr, size); free(contents); } else { + batch_write(opt, hdr->buf, hdr->len); + if (opt->buffer_output) + fflush(stdout); stream_blob(oid); + batch_write(opt, &opt->output_delim, 1); } } } @@ -519,12 +547,10 @@ static void batch_object_write(const char *obj_name, strbuf_addch(scratch, opt->output_delim); } - batch_write(opt, scratch->buf, scratch->len); - - if (opt->batch_mode == BATCH_MODE_CONTENTS) { - print_object_or_die(opt, data); - batch_write(opt, &opt->output_delim, 1); - } + if (opt->batch_mode == BATCH_MODE_CONTENTS) + print_object_or_die(opt, data, scratch); + else + batch_write(opt, scratch->buf, scratch->len); } static void batch_one_object(const char *obj_name, @@ -666,7 +692,7 @@ static void parse_cmd_contents(struct batch_options *opt, struct expand_data *data) { opt->batch_mode = BATCH_MODE_CONTENTS; - data->info.contentp = &data->content; + data->info.contentp = &data->iov[1].iov_base; batch_one_object(line, output, opt, data); } @@ -823,7 +849,7 @@ static int batch_objects(struct batch_options *opt) data.info.typep = &data.type; if (!opt->transform_mode) { data.info.sizep = &data.size; - data.info.contentp = &data.content; + data.info.contentp = &data.iov[1].iov_base; data.info.content_limit = big_file_threshold; data.info.direct_cache = USE_DIRECT_CACHE; } diff --git a/config.mak.uname b/config.mak.uname index 85d63821ec..8ce8776657 100644 --- a/config.mak.uname +++ b/config.mak.uname @@ -69,6 +69,7 @@ ifeq ($(uname_S),Linux) BASIC_CFLAGS += -std=c99 endif LINK_FUZZ_PROGRAMS = YesPlease + HAVE_WRITEV = YesPlease endif ifeq ($(uname_S),GNU/kFreeBSD) HAVE_ALLOCA_H = YesPlease @@ -77,6 +78,7 @@ ifeq ($(uname_S),GNU/kFreeBSD) DIR_HAS_BSD_GROUP_SEMANTICS = YesPlease LIBC_CONTAINS_LIBINTL = YesPlease FREAD_READS_DIRECTORIES = UnfortunatelyYes + HAVE_WRITEV = YesPlease endif ifeq ($(uname_S),UnixWare) CC = cc @@ -292,6 +294,7 @@ ifeq ($(uname_S),FreeBSD) PAGER_ENV = LESS=FRX LV=-c MORE=FRX FREAD_READS_DIRECTORIES = UnfortunatelyYes FILENO_IS_A_MACRO = UnfortunatelyYes + HAVE_WRITEV = YesPlease endif ifeq ($(uname_S),OpenBSD) NO_STRCASESTR = YesPlease @@ -307,6 +310,7 @@ ifeq ($(uname_S),OpenBSD) PROCFS_EXECUTABLE_PATH = /proc/curproc/file FREAD_READS_DIRECTORIES = UnfortunatelyYes FILENO_IS_A_MACRO = UnfortunatelyYes + HAVE_WRITEV = YesPlease endif ifeq ($(uname_S),MirBSD) NO_STRCASESTR = YesPlease @@ -329,6 +333,7 @@ ifeq ($(uname_S),NetBSD) HAVE_BSD_KERN_PROC_SYSCTL = YesPlease CSPRNG_METHOD = arc4random PROCFS_EXECUTABLE_PATH = /proc/curproc/exe + HAVE_WRITEV = YesPlease endif ifeq ($(uname_S),AIX) DEFAULT_PAGER = more diff --git a/git-compat-util.h b/git-compat-util.h index ca7678a379..afde8abc99 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -388,6 +388,16 @@ static inline int git_setitimer(int which UNUSED, #define setitimer(which,value,ovalue) git_setitimer(which,value,ovalue) #endif +#ifdef HAVE_WRITEV +#include +#define git_iovec iovec +#else /* !HAVE_WRITEV */ +struct git_iovec { + void *iov_base; + size_t iov_len; +}; +#endif /* !HAVE_WRITEV */ + #ifndef NO_LIBGEN_H #include #else diff --git a/wrapper.c b/wrapper.c index f87d90bf57..066c772145 100644 --- a/wrapper.c +++ b/wrapper.c @@ -262,6 +262,24 @@ ssize_t xwrite(int fd, const void *buf, size_t len) } } +#ifdef HAVE_WRITEV +ssize_t xwritev(int fd, const struct iovec *iov, int iovcnt) +{ + while (1) { + ssize_t nr = writev(fd, iov, iovcnt); + + if (nr < 0) { + if (errno == EINTR) + continue; + if (handle_nonblock(fd, POLLOUT, errno)) + continue; + } + + return nr; + } +} +#endif /* !HAVE_WRITEV */ + /* * xpread() is the same as pread(), but it automatically restarts pread() * operations with a recoverable error (EAGAIN and EINTR). xpread() DOES diff --git a/wrapper.h b/wrapper.h index 1b2b047ea0..3d33c63d4f 100644 --- a/wrapper.h +++ b/wrapper.h @@ -16,6 +16,7 @@ void *xmmap_gently(void *start, size_t length, int prot, int flags, int fd, off_ int xopen(const char *path, int flags, ...); ssize_t xread(int fd, void *buf, size_t len); ssize_t xwrite(int fd, const void *buf, size_t len); +ssize_t xwritev(int fd, const struct git_iovec *, int iovcnt); ssize_t xpread(int fd, void *buf, size_t len, off_t offset); int xdup(int fd); FILE *xfopen(const char *path, const char *mode); diff --git a/write-or-die.c b/write-or-die.c index 01a9a51fa2..227b051165 100644 --- a/write-or-die.c +++ b/write-or-die.c @@ -107,3 +107,69 @@ void fflush_or_die(FILE *f) if (fflush(f)) die_errno("fflush error"); } + +void fwritev_or_die(FILE *fp, const struct git_iovec *iov, int iovcnt) +{ + int i; + + for (i = 0; i < iovcnt; i++) { + size_t n = iov[i].iov_len; + + if (fwrite(iov[i].iov_base, 1, n, fp) != n) + die_errno("unable to write to FD=%d", fileno(fp)); + } +} + +/* + * note: we don't care about atomicity from writev(2) right now. + * The goal is to avoid allocations+copies in the writer and + * reduce wakeups+syscalls in the reader. + * n.b. @iov is not const since we modify it to avoid allocating + * on partial write. + */ +#ifdef HAVE_WRITEV +void writev_or_die(int fd, struct git_iovec *iov, int iovcnt) +{ + int i; + + while (iovcnt > 0) { + ssize_t n = xwritev(fd, iov, iovcnt); + + /* EINVAL happens when sum of iov_len exceeds SSIZE_MAX */ + if (n < 0 && errno == EINVAL) + n = xwrite(fd, iov[0].iov_base, iov[0].iov_len); + if (n < 0) { + check_pipe(errno); + die_errno("writev error"); + } else if (!n) { + errno = ENOSPC; + die_errno("writev_error"); + } + /* skip fully written iovs, retry from the first partial iov */ + for (i = 0; i < iovcnt; i++) { + if (n >= iov[i].iov_len) { + n -= iov[i].iov_len; + } else { + iov[i].iov_len -= n; + iov[i].iov_base = (char *)iov[i].iov_base + n; + break; + } + } + iovcnt -= i; + iov += i; + } +} +#else /* !HAVE_WRITEV */ + +/* + * n.b. don't use stdio fwrite here even if it's faster, @fd may be + * non-blocking and stdio isn't equipped for EAGAIN + */ +void writev_or_die(int fd, struct git_iovec *iov, int iovcnt) +{ + int i; + + for (i = 0; i < iovcnt; i++) + write_or_die(fd, iov[i].iov_base, iov[i].iov_len); +} +#endif /* !HAVE_WRITEV */ diff --git a/write-or-die.h b/write-or-die.h index 65a5c42a47..20abec211c 100644 --- a/write-or-die.h +++ b/write-or-die.h @@ -7,6 +7,8 @@ void fprintf_or_die(FILE *, const char *fmt, ...); void fwrite_or_die(FILE *f, const void *buf, size_t count); void fflush_or_die(FILE *f); void write_or_die(int fd, const void *buf, size_t count); +void writev_or_die(int fd, struct git_iovec *, int iovcnt); +void fwritev_or_die(FILE *, const struct git_iovec *, int iovcnt); /* * These values are used to help identify parts of a repository to fsync.