public inbox for git@vger.kernel.org
 help / color / mirror / Atom feed
From: Phillip Wood <phillip.wood123@gmail.com>
To: George Hu <integral@archlinux.org>, git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>,
	Johannes Schindelin <Johannes.Schindelin@gmx.de>
Subject: Re: [PATCH] copy.c: use `sendfile()` for in-kernel file copying on Linux
Date: Sat, 14 Feb 2026 16:43:27 +0000	[thread overview]
Message-ID: <bf0b3c41-9784-4494-a932-68abfa60cea6@gmail.com> (raw)
In-Reply-To: <20260213124656.218396-1-integral@archlinux.org>

On 13/02/2026 12:46, George Hu wrote:
> The `sendfile()` system call copies data between one file descriptor
> and another within the kernel, which is more efficient than the
> combination of `read()` and `write()`.

Does git copy any files big enough that this makes a noticeable difference?

>   int copy_fd(int ifd, int ofd)
>   {
> +#ifdef __linux__

Our normal practice when a function has platform specific 
implementations is to host those implementations under compat/<platform>
(see the implementations of trace2_collect_process_information() for an 
example)

> +	struct stat ifd_st;
> +	size_t ifd_len;
> +	ssize_t ret = 0;
> +
> +	fstat(ifd, &ifd_st);

What happens if fstat() fails?

> +	ifd_len = ifd_st.st_size;
> +
> +	while (ifd_len && (ret = sendfile(ofd, ifd, NULL, ifd_len)) > 0)
> +		ifd_len -= (size_t)ret;

This does not propagate errors to the caller, if sendfile() fails the 
function returns 0. write_in_full() handles non-blocking writes, we 
should do the same here if we see EAGAIN. The man page lists various 
restrictions on the file descriptors passed to sendfile() - I'm not sure 
that they affect the uses of copy_file() in git but to be safe we should 
fall back to the read()/write() loop if we see EINVAL.

Thanks

Phillip

> +#else
>   	while (1) {
>   		char buffer[8192];
>   		ssize_t len = xread(ifd, buffer, sizeof(buffer));
> @@ -19,6 +34,8 @@ int copy_fd(int ifd, int ofd)
>   		if (write_in_full(ofd, buffer, len) < 0)
>   			return COPY_WRITE_ERROR;
>   	}
> +#endif
> +
>   	return 0;
>   }
>   


  parent reply	other threads:[~2026-02-14 16:43 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-13 12:46 [PATCH] copy.c: use `sendfile()` for in-kernel file copying on Linux George Hu
2026-02-13 15:36 ` Chris Torek
2026-02-14  9:21   ` George Hu
2026-02-14 16:50     ` Chris Torek
2026-02-20 16:35       ` Ed Maste
2026-02-20 16:48         ` Collin Funk
2026-02-14 16:43 ` Phillip Wood [this message]
2026-02-15  6:23   ` George Hu
2026-02-15  7:43 ` Jeff King

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=bf0b3c41-9784-4494-a932-68abfa60cea6@gmail.com \
    --to=phillip.wood123@gmail.com \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=integral@archlinux.org \
    --cc=phillip.wood@dunelm.org.uk \
    /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