diff mbox series

[2/2] packfile: replace lseek+read with pread

Message ID 20191226104220.27325-3-e@80x24.org (mailing list archive)
State New, archived
Headers show
Series packfile: small syscall reductions | expand

Commit Message

Eric Wong Dec. 26, 2019, 10:42 a.m. UTC
We already have pread emulation for portability, so there's
there's no reason to make two syscalls where one suffices.

Furthermore, readers of the packfile will be using mmap
(or pread to emulate mmap), anyways, so the file description
offset does not matter in this case.

Signed-off-by: Eric Wong <e@80x24.org>
---
 packfile.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

Comments

Junio C Hamano Dec. 26, 2019, 6:23 p.m. UTC | #1
Eric Wong <e@80x24.org> writes:

> We already have pread emulation for portability, so there's
> there's no reason to make two syscalls where one suffices.
>
> Furthermore, readers of the packfile will be using mmap
> (or pread to emulate mmap), anyways, so the file description
> offset does not matter in this case.

s/description/descriptor/ probably.

After seeking to the packfile trailer and reading the pack id hash
using lseek+read, this helper function does not read from the file
descriptor, and the sole caller of it closes the file descriptor
immediately after it returns, which means the read file offset after
reading the packfile trailer does not matter.

So this conversion is correct.  Thanks for a careful analysis.

Will queue both patches.

> Signed-off-by: Eric Wong <e@80x24.org>
> ---
>  packfile.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
>
> diff --git a/packfile.c b/packfile.c
> index 1821cb7a3d..7e7c04e4d8 100644
> --- a/packfile.c
> +++ b/packfile.c
> @@ -576,9 +576,8 @@ static int open_packed_git_1(struct packed_git *p)
>  			     " while index indicates %"PRIu32" objects",
>  			     p->pack_name, ntohl(hdr.hdr_entries),
>  			     p->num_objects);
> -	if (lseek(p->pack_fd, p->pack_size - hashsz, SEEK_SET) == -1)
> -		return error("end of packfile %s is unavailable", p->pack_name);
> -	read_result = read_in_full(p->pack_fd, hash, hashsz);
> +	read_result = pread_in_full(p->pack_fd, hash, hashsz,
> +					p->pack_size - hashsz);
>  	if (read_result < 0)
>  		return error_errno("error reading from %s", p->pack_name);
>  	if (read_result != hashsz)
Junio C Hamano Dec. 26, 2019, 6:32 p.m. UTC | #2
Junio C Hamano <gitster@pobox.com> writes:

> Eric Wong <e@80x24.org> writes:
>
>> We already have pread emulation for portability, so there's
>> there's no reason to make two syscalls where one suffices.
>>
>> Furthermore, readers of the packfile will be using mmap
>> (or pread to emulate mmap), anyways, so the file description
>> offset does not matter in this case.
>
> s/description/descriptor/ probably.
>
> After seeking to the packfile trailer and reading the pack id hash
> using lseek+read, this helper function does not read from the file
> descriptor, and the sole caller of it closes the file descriptor
> immediately after it returns, which means the read file offset after
> reading the packfile trailer does not matter.

Oops, that was not right.  When we successfully open the packfile,
we leave the file descriptor open, so we do need the "we never read
using read(2) from the file descriptor" guarantee for this change to
be correct.

But we do have the guarantee, and existing code does depend on the
guarantee, so the patch is good.

Thanks.
Eric Wong Dec. 26, 2019, 6:59 p.m. UTC | #3
Junio C Hamano <gitster@pobox.com> wrote:
> Eric Wong <e@80x24.org> writes:
> >
> > Furthermore, readers of the packfile will be using mmap
> > (or pread to emulate mmap), anyways, so the file description
> > offset does not matter in this case.
> 
> s/description/descriptor/ probably.

No, I meant "description" :)  The offset is shared in case of dup{,2,3}
syscalls, which only creates a new descriptor, not a new description.
Both the Linux and POSIX lseek(2) manpages say "description".
diff mbox series

Patch

diff --git a/packfile.c b/packfile.c
index 1821cb7a3d..7e7c04e4d8 100644
--- a/packfile.c
+++ b/packfile.c
@@ -576,9 +576,8 @@  static int open_packed_git_1(struct packed_git *p)
 			     " while index indicates %"PRIu32" objects",
 			     p->pack_name, ntohl(hdr.hdr_entries),
 			     p->num_objects);
-	if (lseek(p->pack_fd, p->pack_size - hashsz, SEEK_SET) == -1)
-		return error("end of packfile %s is unavailable", p->pack_name);
-	read_result = read_in_full(p->pack_fd, hash, hashsz);
+	read_result = pread_in_full(p->pack_fd, hash, hashsz,
+					p->pack_size - hashsz);
 	if (read_result < 0)
 		return error_errno("error reading from %s", p->pack_name);
 	if (read_result != hashsz)