Commit 19d747f7bf553c46c0385d960db0d4f6d5f4543a

Stefan Sperling 2018-03-16T20:18:09

avoid an extra memcpy in got_inflate_to_mem()

diff --git a/got/Makefile b/got/Makefile
index 8e74120..b96fa54 100644
--- a/got/Makefile
+++ b/got/Makefile
@@ -7,7 +7,9 @@ SRCS=		got.c delta.c error.c fileindex.c object.c path.c pack.c \
 CPPFLAGS = -I${.CURDIR}/../include -I${.CURDIR}/../lib
 LDADD = -lutil -lz
 DPADD = ${LIBZ} ${LIBUTIL}
-DEBUG = -O0 -g
+CC = gcc
+DEBUG = -O0 -pg
+CPPFLAGS += -DPROFILE
 CFLAGS += -Werror -Wall -Wstrict-prototypes -Wunused-variable
 
 # For now, default to installing binary in ~/bin
diff --git a/got/got.c b/got/got.c
index be95ec2..3ae4fc6 100644
--- a/got/got.c
+++ b/got/got.c
@@ -173,9 +173,10 @@ cmd_checkout(int argc, char *argv[])
 	argc -= optind;
 	argv += optind;
 
+#ifndef PROFILE
 	if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
 		err(1, "pledge");
-
+#endif
 	if (argc == 1) {
 		char *cwd, *base, *dotgit;
 		repo_path = argv[0];
@@ -298,9 +299,10 @@ cmd_log(int argc, char *argv[])
 	struct got_object *obj;
 	char *repo_path = NULL;
 
+#ifndef PROFILE
 	if (pledge("stdio rpath wpath cpath", NULL) == -1)
 		err(1, "pledge");
-
+#endif
 	if (argc == 1) {
 		repo_path = getcwd(NULL, 0);
 		if (repo_path == NULL)
diff --git a/lib/got_zbuf_lib.h b/lib/got_zbuf_lib.h
index 066d182..9285ed5 100644
--- a/lib/got_zbuf_lib.h
+++ b/lib/got_zbuf_lib.h
@@ -21,12 +21,14 @@ struct got_zstream_buf {
 	char *outbuf;
 	size_t outlen;
 	int flags;
-#define GOT_ZSTREAM_F_HAVE_MORE 0x01
+#define GOT_ZSTREAM_F_HAVE_MORE		0x01
+#define GOT_ZSTREAM_F_OWN_OUTBUF	0x02
 };
 
-#define GOT_ZSTREAM_BUFSIZE	8192
+#define GOT_ZSTREAM_BUFSIZE		8192
 
-const struct got_error *got_inflate_init(struct got_zstream_buf *, size_t);
+const struct got_error *got_inflate_init(struct got_zstream_buf *, uint8_t *,
+    size_t);
 const struct got_error *got_inflate_read(struct got_zstream_buf *, FILE *,
     size_t *);
 void got_inflate_end(struct got_zstream_buf *);
diff --git a/lib/object.c b/lib/object.c
index 6ad23fc..8a150ed 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -162,7 +162,7 @@ read_object_header(struct got_object **obj, struct got_repository *repo,
 	if (buf == NULL)
 		return got_error(GOT_ERR_NO_MEM);
 
-	err = got_inflate_init(&zb, zbsize);
+	err = got_inflate_init(&zb, NULL, zbsize);
 	if (err)
 		return err;
 
@@ -759,7 +759,7 @@ got_object_blob_open(struct got_blob_object **blob,
 			return err;
 		}
 
-		err = got_inflate_init(&(*blob)->zb, blocksize);
+		err = got_inflate_init(&(*blob)->zb, NULL, blocksize);
 		if (err != NULL) {
 			fclose((*blob)->f);
 			free(*blob);
diff --git a/lib/zbuf.c b/lib/zbuf.c
index 4d83cf8..cf71b54 100644
--- a/lib/zbuf.c
+++ b/lib/zbuf.c
@@ -29,7 +29,7 @@
 #include "got_zbuf_lib.h"
 
 const struct got_error *
-got_inflate_init(struct got_zstream_buf *zb, size_t bufsize)
+got_inflate_init(struct got_zstream_buf *zb, uint8_t *outbuf, size_t bufsize)
 {
 	const struct got_error *err = NULL;
 
@@ -50,11 +50,15 @@ got_inflate_init(struct got_zstream_buf *zb, size_t bufsize)
 		goto done;
 	}
 
-	zb->outbuf = calloc(1, zb->outlen);
-	if (zb->outbuf == NULL) {
-		err = got_error(GOT_ERR_NO_MEM);
-		goto done;
-	}
+	if (outbuf == NULL) {
+		zb->outbuf = calloc(1, zb->outlen);
+		if (zb->outbuf == NULL) {
+			err = got_error(GOT_ERR_NO_MEM);
+			goto done;
+		}
+		zb->flags |= GOT_ZSTREAM_F_OWN_OUTBUF;
+	} else
+		zb->outbuf = outbuf;
 
 done:
 	if (err)
@@ -103,7 +107,8 @@ void
 got_inflate_end(struct got_zstream_buf *zb)
 {
 	free(zb->inbuf);
-	free(zb->outbuf);
+	if (zb->flags & GOT_ZSTREAM_F_OWN_OUTBUF)
+		free(zb->outbuf);
 	inflateEnd(&zb->z);
 }
 
@@ -115,19 +120,23 @@ got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
 	struct got_zstream_buf zb;
 	void *newbuf;
 
-	err = got_inflate_init(&zb, GOT_ZSTREAM_BUFSIZE);
+	*outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
+	if (*outbuf == NULL)
+		return got_error(GOT_ERR_NO_MEM);
+	err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE);
 	if (err)
 		return err;
 
-	*outbuf = NULL;
 	*outlen = 0;
 
 	do {
 		err = got_inflate_read(&zb, f, &avail);
 		if (err)
 			return err;
-		if (avail > 0) {
-			newbuf = reallocarray(*outbuf, 1, *outlen + avail);
+		*outlen += avail;
+		if (zb.flags & GOT_ZSTREAM_F_HAVE_MORE) {
+			newbuf = reallocarray(*outbuf, 1,
+			    *outlen + GOT_ZSTREAM_BUFSIZE);
 			if (newbuf == NULL) {
 				free(*outbuf);
 				*outbuf = NULL;
@@ -135,9 +144,9 @@ got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
 				err = got_error(GOT_ERR_NO_MEM);
 				goto done;
 			}
-			memcpy(newbuf + *outlen, zb.outbuf, avail);
 			*outbuf = newbuf;
-			*outlen += avail;
+			zb.outbuf = newbuf + *outlen;
+			zb.outlen = GOT_ZSTREAM_BUFSIZE;
 		}
 	} while (zb.flags & GOT_ZSTREAM_F_HAVE_MORE);
 
@@ -153,7 +162,7 @@ got_inflate_to_file(size_t *outlen, FILE *infile, FILE *outfile)
 	size_t avail;
 	struct got_zstream_buf zb;
 
-	err = got_inflate_init(&zb, GOT_ZSTREAM_BUFSIZE);
+	err = got_inflate_init(&zb, NULL, GOT_ZSTREAM_BUFSIZE);
 	if (err)
 		goto done;