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

Smart HTTP fetch: gzip requests

The upload-pack requests are mostly plain text and they compress rather well. Deflating them with Content-Encoding: gzip can easily drop the size of the request by 50%, reducing the amount of data to transfer as we negotiate the common commits. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> CC: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Shawn O. Pearce committed Oct 30, 2009 at 17:47 UTC b8538603a34b13ccb1a3a096f461b8a35d7ea878
1 file changed +50
remote-curl.c
+50
@@ -289,6 +289,7 @@ struct rpc_state {
289 int in;
290 int out;
291 struct strbuf result;
292 + unsigned gzip_request : 1;
293 };
294
295 static size_t rpc_out(void *ptr, size_t eltsize,
@@ -327,6 +328,8 @@ static int post_rpc(struct rpc_state *rpc)
328 struct active_request_slot *slot;
329 struct slot_results results;
330 struct curl_slist *headers = NULL;
331 + int use_gzip = rpc->gzip_request;
332 + char *gzip_body = NULL;
333 int err = 0, large_request = 0;
334
335 /* Try to load the entire request, if we can fit it into the
@@ -340,6 +343,7 @@ static int post_rpc(struct rpc_state *rpc)
343
344 if (left < LARGE_PACKET_MAX) {
345 large_request = 1;
346 + use_gzip = 0;
347 break;
348 }
349
@@ -355,6 +359,7 @@ static int post_rpc(struct rpc_state *rpc)
359 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
360 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
361 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
362 + curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
363
364 headers = curl_slist_append(headers, rpc->hdr_content_type);
365 headers = curl_slist_append(headers, rpc->hdr_accept);
@@ -372,6 +377,49 @@ static int post_rpc(struct rpc_state *rpc)
377 fflush(stderr);
378 }
379
380 + } else if (use_gzip && 1024 < rpc->len) {
381 + /* The client backend isn't giving us compressed data so
382 + * we can try to deflate it ourselves, this may save on.
383 + * the transfer time.
384 + */
385 + size_t size;
386 + z_stream stream;
387 + int ret;
388 +
389 + memset(&stream, 0, sizeof(stream));
390 + ret = deflateInit2(&stream, Z_BEST_COMPRESSION,
391 + Z_DEFLATED, (15 + 16),
392 + 8, Z_DEFAULT_STRATEGY);
393 + if (ret != Z_OK)
394 + die("cannot deflate request; zlib init error %d", ret);
395 + size = deflateBound(&stream, rpc->len);
396 + gzip_body = xmalloc(size);
397 +
398 + stream.next_in = (unsigned char *)rpc->buf;
399 + stream.avail_in = rpc->len;
400 + stream.next_out = (unsigned char *)gzip_body;
401 + stream.avail_out = size;
402 +
403 + ret = deflate(&stream, Z_FINISH);
404 + if (ret != Z_STREAM_END)
405 + die("cannot deflate request; zlib deflate error %d", ret);
406 +
407 + ret = deflateEnd(&stream);
408 + if (ret != Z_OK)
409 + die("cannot deflate request; zlib end error %d", ret);
410 +
411 + size = stream.total_out;
412 +
413 + headers = curl_slist_append(headers, "Content-Encoding: gzip");
414 + curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
415 + curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, size);
416 +
417 + if (options.verbosity > 1) {
418 + fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
419 + rpc->service_name,
420 + (unsigned long)rpc->len, (unsigned long)size);
421 + fflush(stderr);
422 + }
423 } else {
424 /* We know the complete request size in advance, use the
425 * more normal Content-Length approach.
@@ -398,6 +446,7 @@ static int post_rpc(struct rpc_state *rpc)
446 }
447
448 curl_slist_free_all(headers);
449 + free(gzip_body);
450 return err;
451 }
452
@@ -523,6 +572,7 @@ static int fetch_git(struct discovery *heads,
572 memset(&rpc, 0, sizeof(rpc));
573 rpc.service_name = "git-upload-pack",
574 rpc.argv = argv;
575 + rpc.gzip_request = 1;
576
577 err = rpc_service(&rpc, heads);
578 if (rpc.result.len)