# [PATCH] pack-write: use hashwrite_be32() instead of double-buffering array

3 messages from 2020-11-01 to 2020-11-04. Participants: René Scharfe, Jeff King, Junio C Hamano.
Thread: https://gitlist.dev/t/54547

## René Scharfe, 2020-11-01 08:52

Subject: [PATCH] pack-write: use hashwrite_be32() instead of double-buffering array
Message-ID: <aec69531-d621-ab26-efd4-96e1ae0ed3a4@web.de>
URL: https://gitlist.dev/e/aec69531-d621-ab26-efd4-96e1ae0ed3a4%40web.de

```
hashwrite() already buffers writes, so pass the fanout table entries
individually via hashwrite_be32(), which also does the endianess
conversion for us.  This avoids a memory copy, shortens the code and
reduces the number of magic numbers.

Signed-off-by: René Scharfe <l.s.r@web.de>
---
Patch generated with -U8 for easier review.

 pack-write.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/pack-write.c b/pack-write.c
index a6cdb3c67c..23e19cc1ec 100644
--- a/pack-write.c
+++ b/pack-write.c
@@ -43,17 +43,16 @@ static int need_large_offset(off_t offset, const struct pack_idx_option *opts)
  */
 const char *write_idx_file(const char *index_name, struct pack_idx_entry **objects,
 			   int nr_objects, const struct pack_idx_option *opts,
 			   const unsigned char *sha1)
 {
 	struct hashfile *f;
 	struct pack_idx_entry **sorted_by_sha, **list, **last;
 	off_t last_obj_offset = 0;
-	uint32_t array[256];
 	int i, fd;
 	uint32_t index_version;

 	if (nr_objects) {
 		sorted_by_sha = objects;
 		list = sorted_by_sha;
 		last = sorted_by_sha + nr_objects;
 		for (i = 0; i < nr_objects; ++i) {
@@ -101,20 +100,19 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
 	for (i = 0; i < 256; i++) {
 		struct pack_idx_entry **next = list;
 		while (next < last) {
 			struct pack_idx_entry *obj = *next;
 			if (obj->oid.hash[0] != i)
 				break;
 			next++;
 		}
-		array[i] = htonl(next - sorted_by_sha);
+		hashwrite_be32(f, next - sorted_by_sha);
 		list = next;
 	}
-	hashwrite(f, array, 256 * 4);

 	/*
 	 * Write the actual SHA1 entries..
 	 */
 	list = sorted_by_sha;
 	for (i = 0; i < nr_objects; i++) {
 		struct pack_idx_entry *obj = *list++;
 		if (index_version < 2)
--
2.29.2

```

## Jeff King, 2020-11-04 13:36

Subject: Re: [PATCH] pack-write: use hashwrite_be32() instead of double-buffering array
Message-ID: <20201104133653.GC3030480@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20201104133653.GC3030480%40coredump.intra.peff.net
In-Reply-To: <aec69531-d621-ab26-efd4-96e1ae0ed3a4@web.de>

```
On Sun, Nov 01, 2020 at 09:52:12AM +0100, René Scharfe wrote:

> hashwrite() already buffers writes, so pass the fanout table entries
> individually via hashwrite_be32(), which also does the endianess
> conversion for us.  This avoids a memory copy, shortens the code and
> reduces the number of magic numbers.

Yep, this seems trivially correct. The key observation is that we are
filling the array in order:

> @@ -101,20 +100,19 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
>  	for (i = 0; i < 256; i++) {
>  		struct pack_idx_entry **next = list;
>  		while (next < last) {
>  			struct pack_idx_entry *obj = *next;
>  			if (obj->oid.hash[0] != i)
>  				break;
>  			next++;
>  		}
> -		array[i] = htonl(next - sorted_by_sha);
> +		hashwrite_be32(f, next - sorted_by_sha);
>  		list = next;
>  	}
> -	hashwrite(f, array, 256 * 4);

Perhaps obvious, but I got bit trying to do another similar conversion
recently that was filling in the array out-of-order (not on the list,
but in some improvements in the bitmap code that haven't been sent in
yet).

-Peff

```

## Junio C Hamano, 2020-11-04 16:23

Subject: Re: [PATCH] pack-write: use hashwrite_be32() instead of double-buffering array
Message-ID: <xmqqmtzxnlad.fsf@gitster.c.googlers.com>
URL: https://gitlist.dev/e/xmqqmtzxnlad.fsf%40gitster.c.googlers.com
In-Reply-To: <20201104133653.GC3030480@coredump.intra.peff.net>

```
Jeff King <peff@peff.net> writes:

> On Sun, Nov 01, 2020 at 09:52:12AM +0100, René Scharfe wrote:
>
>> hashwrite() already buffers writes, so pass the fanout table entries
>> individually via hashwrite_be32(), which also does the endianess
>> conversion for us.  This avoids a memory copy, shortens the code and
>> reduces the number of magic numbers.
>
> Yep, this seems trivially correct. The key observation is that we are
> filling the array in order:
>
>> @@ -101,20 +100,19 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
>>  	for (i = 0; i < 256; i++) {
>>  		struct pack_idx_entry **next = list;
>>  		while (next < last) {
>>  			struct pack_idx_entry *obj = *next;
>>  			if (obj->oid.hash[0] != i)
>>  				break;
>>  			next++;
>>  		}
>> -		array[i] = htonl(next - sorted_by_sha);
>> +		hashwrite_be32(f, next - sorted_by_sha);
>>  		list = next;
>>  	}
>> -	hashwrite(f, array, 256 * 4);
>
> Perhaps obvious, but I got bit trying to do another similar conversion
> recently that was filling in the array out-of-order...

Yeah, filling an array out of order and then writing the result in
order would obviously different from writing out individual pieces
in the order the original codeflow used to fill the array.  Worse,
the order the data items are fed to hashwrite() obviously affects
the hash computed at the end.  An example of too much abstraction
biting us? ;-)



```
