Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Optimized out some unnecessary memory allocations

  • Loading branch information...
commit 64202eda5226dcf87fb96fa33cbdae8377c41641 1 parent 98f75c8
@vkholodkov authored
Showing with 62 additions and 50 deletions.
  1. +62 −50 ngx_http_upload_module.c
View
112 ngx_http_upload_module.c
@@ -93,14 +93,12 @@ typedef struct {
typedef struct ngx_http_upload_md5_ctx_s {
MD5_CTX md5;
- u_char md5_digest[MD5_DIGEST_LENGTH];
- u_char md5_digest_hex[MD5_DIGEST_LENGTH * 2];
+ u_char md5_digest[MD5_DIGEST_LENGTH * 2];
} ngx_http_upload_md5_ctx_t;
typedef struct ngx_http_upload_sha1_ctx_s {
SHA1_CTX sha1;
- u_char sha1_digest[SHA_DIGEST_LENGTH];
- u_char sha1_digest_hex[SHA_DIGEST_LENGTH * 2];
+ u_char sha1_digest[SHA_DIGEST_LENGTH * 2];
} ngx_http_upload_sha1_ctx_t;
/*
@@ -433,6 +431,16 @@ static ngx_http_variable_t ngx_http_upload_aggregate_variables[] = { /* {{{ */
static ngx_str_t ngx_http_upload_empty_field_value = ngx_null_string;
+static ngx_str_t ngx_upload_field_part1 = { /* {{{ */
+ sizeof("\r\nContent-Disposition: form-data; name=\"") - 1,
+ (u_char*)"\r\nContent-Disposition: form-data; name=\""
+}; /* }}} */
+
+static ngx_str_t ngx_upload_field_part2 = { /* {{{ */
+ sizeof("\"\r\n\r\n") - 1,
+ (u_char*)"\"\r\n\r\n"
+}; /* }}} */
+
static ngx_int_t /* {{{ ngx_http_upload_handler */
ngx_http_upload_handler(ngx_http_request_t *r)
{
@@ -863,57 +871,55 @@ static ngx_int_t ngx_http_upload_flush_output_buffer(ngx_http_upload_ctx_t *u, u
}
} /* }}} */
-static ngx_int_t /* {{{ ngx_http_upload_append_field */
-ngx_http_upload_append_field(ngx_http_upload_ctx_t *u, ngx_str_t *name, ngx_str_t *value)
+static void /* {{{ ngx_http_upload_append_str */
+ngx_http_upload_append_str(ngx_http_upload_ctx_t *u, ngx_buf_t *b, ngx_chain_t *cl, ngx_str_t *s)
{
- ngx_int_t len;
- ngx_chain_t *cl;
- ngx_buf_t *b;
+ b->start = b->pos = s->data;
+ b->end = b->last = s->data + s->len;
+ b->memory = 1;
+ b->in_file = 0;
+ b->last_buf = 0;
- len = u->first_part ? u->boundary.len - 2 : u->boundary.len;
+ b->last_in_chain = 0;
- len += sizeof("\r\nContent-Disposition: form-data; name=\"") - 1;
+ cl->buf = b;
+ cl->next = NULL;
- len += name->len;
+ if(u->chain == NULL) {
+ u->chain = cl;
+ u->last = cl;
+ }else{
+ u->last->next = cl;
+ u->last = cl;
+ }
+} /* }}} */
- len += sizeof("\"\r\n\r\n") - 1;
+static ngx_int_t /* {{{ ngx_http_upload_append_field */
+ngx_http_upload_append_field(ngx_http_upload_ctx_t *u, ngx_str_t *name, ngx_str_t *value)
+{
+ ngx_str_t boundary = { u->first_part ? u->boundary.len - 2 : u->boundary.len,
+ u->first_part ? u->boundary.data + 2 : u->boundary.data };
- len += value->len;
+ ngx_buf_t *b;
+ ngx_chain_t *cl;
- b = ngx_create_temp_buf(u->request->pool, len);
+ b = ngx_palloc(u->request->pool, 5 * sizeof(ngx_buf_t) + 5 * sizeof(ngx_chain_t));
if (b == NULL) {
return NGX_UPLOAD_NOMEM;
}
- cl = ngx_alloc_chain_link(u->request->pool);
- if (cl == NULL) {
- return NGX_UPLOAD_NOMEM;
- }
-
- b->last = ngx_cpymem(b->last, u->first_part ? u->boundary.data + 2 : u->boundary.data,
- u->first_part ? u->boundary.len - 2 : u->boundary.len);
+ cl = (ngx_chain_t *)(b + 5);
- b->last = ngx_cpymem(b->last, "\r\nContent-Disposition: form-data; name=\"", sizeof("\r\nContent-Disposition: form-data; name=\"") - 1);
+ ngx_http_upload_append_str(u, b, cl, &boundary);
- b->last = ngx_cpymem(b->last, name->data, name->len);
+ ngx_http_upload_append_str(u, b + 1, cl + 1, &ngx_upload_field_part1);
- b->last = ngx_cpymem(b->last, "\"\r\n\r\n", sizeof("\"\r\n\r\n") - 1);
+ ngx_http_upload_append_str(u, b + 2, cl + 2, name);
- b->last = ngx_cpymem(b->last, value->data, value->len);
-
- b->last_in_chain = 0;
-
- cl->buf = b;
- cl->next = NULL;
+ ngx_http_upload_append_str(u, b + 3, cl + 3, &ngx_upload_field_part2);
- if(u->chain == NULL) {
- u->chain = cl;
- u->last = cl;
- }else{
- u->last->next = cl;
- u->last = cl;
- }
+ ngx_http_upload_append_str(u, b + 4, cl + 4, value);
u->first_part = 0;
@@ -1059,14 +1065,17 @@ ngx_http_upload_md5_variable(ngx_http_request_t *r,
v->not_found = 0;
hex_table = (u_char*)data;
- c = u->md5_ctx->md5_digest_hex;
+ c = u->md5_ctx->md5_digest + MD5_DIGEST_LENGTH * 2;
- for (i = 0; i < MD5_DIGEST_LENGTH; i++) {
- *c++ = hex_table[u->md5_ctx->md5_digest[i] >> 4];
- *c++ = hex_table[u->md5_ctx->md5_digest[i] & 0xf];
- }
+ i = MD5_DIGEST_LENGTH;
+
+ do{
+ i--;
+ *--c = hex_table[u->md5_ctx->md5_digest[i] & 0xf];
+ *--c = hex_table[u->md5_ctx->md5_digest[i] >> 4];
+ }while(i != 0);
- v->data = u->md5_ctx->md5_digest_hex;
+ v->data = u->md5_ctx->md5_digest;
v->len = MD5_DIGEST_LENGTH * 2;
return NGX_OK;
@@ -1093,14 +1102,17 @@ ngx_http_upload_sha1_variable(ngx_http_request_t *r,
v->not_found = 0;
hex_table = (u_char*)data;
- c = u->sha1_ctx->sha1_digest_hex;
+ c = u->sha1_ctx->sha1_digest + SHA_DIGEST_LENGTH * 2;
- for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
- *c++ = hex_table[u->sha1_ctx->sha1_digest[i] >> 4];
- *c++ = hex_table[u->sha1_ctx->sha1_digest[i] & 0xf];
- }
+ i = SHA_DIGEST_LENGTH;
+
+ do{
+ i--;
+ *--c = hex_table[u->sha1_ctx->sha1_digest[i] & 0xf];
+ *--c = hex_table[u->sha1_ctx->sha1_digest[i] >> 4];
+ }while(i != 0);
- v->data = u->sha1_ctx->sha1_digest_hex;
+ v->data = u->sha1_ctx->sha1_digest;
v->len = SHA_DIGEST_LENGTH * 2;
return NGX_OK;
Please sign in to comment.
Something went wrong with that request. Please try again.