@matheusbsbs / matheus-bagliano / commits / d65a16f6c4
Fork 0

sha1_file.c: cleanup hdr usage

Let's have hdr be a simple char pointer/array when possible, and let's reduce its storage to 32 bytes. Especially for sha1_loose_object_info() where 128 bytes is way excessive and wastes extra CPU cycles inflating. The object type is already restricted to 10 bytes in parse_sha1_header() and the size, even if it is 64 bits, will fit in 20 decimal numbers. So 32 bytes is plenty. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Nicolas Pitre committed Feb 26, 2007 at 14:55 UTC d65a16f6c45c9b2a16f3abc45f727dc6507c853d
1 file changed +12 -12
sha1_file.c
+12 -12
@@ -1461,7 +1461,7 @@ static int sha1_loose_object_info(const unsigned char *sha1, char *type, unsigne
1461 unsigned long mapsize, size;
1462 void *map;
1463 z_stream stream;
1464 - char hdr[128];
1464 + char hdr[32];
1465
1466 map = map_sha1_file(sha1, &mapsize);
1467 if (!map)
@@ -1628,12 +1628,12 @@ void *read_object_with_reference(const unsigned char *sha1,
1628
1629 static void write_sha1_file_prepare(void *buf, unsigned long len,
1630 const char *type, unsigned char *sha1,
1631 - unsigned char *hdr, int *hdrlen)
1631 + char *hdr, int *hdrlen)
1632 {
1633 SHA_CTX c;
1634
1635 /* Generate the header */
1636 - *hdrlen = sprintf((char *)hdr, "%s %lu", type, len)+1;
1636 + *hdrlen = sprintf(hdr, "%s %lu", type, len)+1;
1637
1638 /* Sha1.. */
1639 SHA1_Init(&c);
@@ -1740,7 +1740,7 @@ static int write_binary_header(unsigned char *hdr, enum object_type type, unsign
1740
1741 static void setup_object_header(z_stream *stream, const char *type, unsigned long len)
1742 {
1743 - int obj_type, hdr;
1743 + int obj_type, hdrlen;
1744
1745 if (use_legacy_headers) {
1746 while (deflate(stream, 0) == Z_OK)
@@ -1757,16 +1757,16 @@ static void setup_object_header(z_stream *stream, const char *type, unsigned lon
1757 obj_type = OBJ_TAG;
1758 else
1759 die("trying to generate bogus object of type '%s'", type);
1760 - hdr = write_binary_header(stream->next_out, obj_type, len);
1761 - stream->total_out = hdr;
1762 - stream->next_out += hdr;
1763 - stream->avail_out -= hdr;
1760 + hdrlen = write_binary_header(stream->next_out, obj_type, len);
1761 + stream->total_out = hdrlen;
1762 + stream->next_out += hdrlen;
1763 + stream->avail_out -= hdrlen;
1764 }
1765
1766 int hash_sha1_file(void *buf, unsigned long len, const char *type,
1767 unsigned char *sha1)
1768 {
1769 - unsigned char hdr[50];
1769 + char hdr[32];
1770 int hdrlen;
1771 write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
1772 return 0;
@@ -1780,7 +1780,7 @@ int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned cha
1780 unsigned char sha1[20];
1781 char *filename;
1782 static char tmpfile[PATH_MAX];
1783 - unsigned char hdr[50];
1783 + char hdr[32];
1784 int fd, hdrlen;
1785
1786 /* Normally if we have it in the pack then we do not bother writing
@@ -1827,7 +1827,7 @@ int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned cha
1827 stream.avail_out = size;
1828
1829 /* First header.. */
1830 - stream.next_in = hdr;
1830 + stream.next_in = (unsigned char *)hdr;
1831 stream.avail_in = hdrlen;
1832 setup_object_header(&stream, type, len);
1833
@@ -1859,7 +1859,7 @@ static void *repack_object(const unsigned char *sha1, unsigned long *objsize)
1859 unsigned char *unpacked;
1860 unsigned long len;
1861 char type[20];
1862 - char hdr[50];
1862 + char hdr[32];
1863 int hdrlen;
1864 void *buf;
1865