Git development
 help / color / mirror / Atom feed
From: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
To: Patrick Steinhardt <ps@pks.im>
Cc: git@vger.kernel.org
Subject: Re: [PATCH 4/6] odb/source-loose: deduplicate logic to mmap objects
Date: Tue, 10 Nov 2026 19:45:58 +0530	[thread overview]
Message-ID: <9794ae30-d6a4-478a-aa25-88f8d9e02810@gmail.com> (raw)
In-Reply-To: <20261102-pks-odb-loose-cleanups-v1-4-fbbc86c29693@pks.im>

On 11/2/26 16:50, Patrick Steinhardt wrote:
> The logic to mmap objects is duplicated across `map_fd()` and
> `odb_source_loose_map_object()`. The only difference is that the former
> takes an arbitrary path, whereas the latter derives the path from an
> object ID.
> 
> Deduplicate the logic.
> 

Makes sense.

> Signed-off-by: Patrick Steinhardt <ps@pks.im>
> ---
>   odb/source-loose.c | 23 +++--------------------
>   1 file changed, 3 insertions(+), 20 deletions(-)
> 
> diff --git a/odb/source-loose.c b/odb/source-loose.c
> index a90268e200a..5cd679e802c 100644
> --- a/odb/source-loose.c
> +++ b/odb/source-loose.c
> @@ -544,28 +544,11 @@ static void *odb_source_loose_map_object(struct odb_source_loose *loose,
>   					 const struct object_id *oid,
>   					 unsigned long *size)
>   {
> -	const char *p;
> -	int fd = open_loose_object(loose, oid, &p);
> -	void *map = NULL;
> -	struct stat st;
> -
> +	const char *path;
> +	int fd = open_loose_object(loose, oid, &path);
>   	if (fd < 0)
>   		return NULL;
> -
> -	if (!fstat(fd, &st)) {
> -		*size = xsize_t(st.st_size);
> -		if (!*size) {
> -			/* mmap() is forbidden on empty files */
> -			error(_("object file %s is empty"), p);
> -			goto out;
> -		}
> -
> -		map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
> -	}
> -
> -out:
> -	close(fd);
> -	return map;
> +	return map_fd(fd, path, size);

I just noticed that map_fd could follow the goto pattern followed above 
and avoid the redundant close() call. Something like:

diff --git a/odb/source-loose.c b/odb/source-loose.c
index 737d583f03..4ace94084f 100644
--- a/odb/source-loose.c
+++ b/odb/source-loose.c
@@ -532,11 +532,12 @@ static void *map_fd(int fd, const char *path, 
unsigned long *size)
  		if (!*size) {
  			/* mmap() is forbidden on empty files */
  			error(_("object file %s is empty"), path);
-			close(fd);
-			return NULL;
+			goto out;
  		}
  		map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0);
  	}
+
+out:
  	close(fd);
  	return map;
  }

Of course, this isn't really a show-stopper for this patch. Just another 
relevant cleanup I noticed.

-- 
Sivaraam


  reply	other threads:[~2026-11-10 14:16 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-02 11:20 [PATCH 0/6] object-file: move logic related to the loose backend Patrick Steinhardt
2026-11-02 11:20 ` [PATCH 1/6] object-file: move logic to read loose objects into backend Patrick Steinhardt
2026-11-10 13:10   ` Kaartic Sivaraam
2026-11-11 13:38     ` Patrick Steinhardt
2026-11-02 11:20 ` [PATCH 2/6] object-file: move logic to derive loose object paths " Patrick Steinhardt
2026-11-10 13:28   ` Kaartic Sivaraam
2026-11-11 13:40     ` Patrick Steinhardt
2026-11-02 11:20 ` [PATCH 3/6] object-file: move iteration over loose files " Patrick Steinhardt
2026-11-10 13:57   ` Kaartic Sivaraam
2026-11-02 11:20 ` [PATCH 4/6] odb/source-loose: deduplicate logic to mmap objects Patrick Steinhardt
2026-11-10 14:15   ` Kaartic Sivaraam [this message]
2026-11-11 13:40     ` Patrick Steinhardt
2026-11-02 11:20 ` [PATCH 5/6] object-file: stop including "odb/source-loose.h" Patrick Steinhardt
2026-11-10 16:13   ` Kaartic Sivaraam
2026-11-11 13:40     ` Patrick Steinhardt
2026-11-02 11:20 ` [PATCH 6/6] object-file: stop including "git-zlib.h" Patrick Steinhardt
2026-11-10 16:13   ` Kaartic Sivaraam

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9794ae30-d6a4-478a-aa25-88f8d9e02810@gmail.com \
    --to=kaartic.sivaraam@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=ps@pks.im \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox