[nginx] Moved the code for coalescing file buffers to a separate...

Valentin Bartenev vbart at nginx.com
Wed Nov 19 18:19:06 UTC 2014


details:   http://hg.nginx.org/nginx/rev/ac3f78219f85
branches:  
changeset: 5915:ac3f78219f85
user:      Valentin Bartenev <vbart at nginx.com>
date:      Wed Aug 13 15:11:45 2014 +0400
description:
Moved the code for coalescing file buffers to a separate function.

diffstat:

 src/core/ngx_buf.c                       |  42 ++++++++++++++++++++++++++++++++
 src/core/ngx_buf.h                       |   2 +
 src/os/unix/ngx_darwin_sendfile_chain.c  |  27 ++------------------
 src/os/unix/ngx_freebsd_sendfile_chain.c |  27 ++------------------
 src/os/unix/ngx_linux_sendfile_chain.c   |  27 ++------------------
 5 files changed, 53 insertions(+), 72 deletions(-)

diffs (199 lines):

diff -r 4dd67e5d958e -r ac3f78219f85 src/core/ngx_buf.c
--- a/src/core/ngx_buf.c	Wed Nov 19 21:17:11 2014 +0300
+++ b/src/core/ngx_buf.c	Wed Aug 13 15:11:45 2014 +0400
@@ -220,6 +220,48 @@ ngx_chain_update_chains(ngx_pool_t *p, n
 }
 
 
+off_t
+ngx_chain_coalesce_file(ngx_chain_t **in, off_t limit)
+{
+    off_t         total, size, aligned, fprev;
+    ngx_fd_t      fd;
+    ngx_chain_t  *cl;
+
+    total = 0;
+
+    cl = *in;
+    fd = cl->buf->file->fd;
+
+    do {
+        size = cl->buf->file_last - cl->buf->file_pos;
+
+        if (size > limit - total) {
+            size = limit - total;
+
+            aligned = (cl->buf->file_pos + size + ngx_pagesize - 1)
+                       & ~((off_t) ngx_pagesize - 1);
+
+            if (aligned <= cl->buf->file_last) {
+                size = aligned - cl->buf->file_pos;
+            }
+        }
+
+        total += size;
+        fprev = cl->buf->file_pos + size;
+        cl = cl->next;
+
+    } while (cl
+             && cl->buf->in_file
+             && total < limit
+             && fd == cl->buf->file->fd
+             && fprev == cl->buf->file_pos);
+
+    *in = cl;
+
+    return total;
+}
+
+
 ngx_chain_t *
 ngx_chain_update_sent(ngx_chain_t *in, off_t sent)
 {
diff -r 4dd67e5d958e -r ac3f78219f85 src/core/ngx_buf.h
--- a/src/core/ngx_buf.h	Wed Nov 19 21:17:11 2014 +0300
+++ b/src/core/ngx_buf.h	Wed Aug 13 15:11:45 2014 +0400
@@ -158,6 +158,8 @@ ngx_chain_t *ngx_chain_get_free_buf(ngx_
 void ngx_chain_update_chains(ngx_pool_t *p, ngx_chain_t **free,
     ngx_chain_t **busy, ngx_chain_t **out, ngx_buf_tag_t tag);
 
+off_t ngx_chain_coalesce_file(ngx_chain_t **in, off_t limit);
+
 ngx_chain_t *ngx_chain_update_sent(ngx_chain_t *in, off_t sent);
 
 #endif /* _NGX_BUF_H_INCLUDED_ */
diff -r 4dd67e5d958e -r ac3f78219f85 src/os/unix/ngx_darwin_sendfile_chain.c
--- a/src/os/unix/ngx_darwin_sendfile_chain.c	Wed Nov 19 21:17:11 2014 +0300
+++ b/src/os/unix/ngx_darwin_sendfile_chain.c	Wed Aug 13 15:11:45 2014 +0400
@@ -31,7 +31,7 @@ ngx_chain_t *
 ngx_darwin_sendfile_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit)
 {
     int              rc;
-    off_t            size, send, prev_send, aligned, sent, fprev;
+    off_t            send, prev_send, sent;
     off_t            file_size;
     ngx_uint_t       eintr;
     ngx_err_t        err;
@@ -95,30 +95,9 @@ ngx_darwin_sendfile_chain(ngx_connection
 
             /* coalesce the neighbouring file bufs */
 
-            do {
-                size = cl->buf->file_last - cl->buf->file_pos;
+            file_size = ngx_chain_coalesce_file(&cl, limit - send);
 
-                if (send + size > limit) {
-                    size = limit - send;
-
-                    aligned = (cl->buf->file_pos + size + ngx_pagesize - 1)
-                               & ~((off_t) ngx_pagesize - 1);
-
-                    if (aligned <= cl->buf->file_last) {
-                        size = aligned - cl->buf->file_pos;
-                    }
-                }
-
-                file_size += size;
-                send += size;
-                fprev = cl->buf->file_pos + size;
-                cl = cl->next;
-
-            } while (cl
-                     && cl->buf->in_file
-                     && send < limit
-                     && file->file->fd == cl->buf->file->fd
-                     && fprev == cl->buf->file_pos);
+            send += file_size;
         }
 
         if (file && header.count == 0) {
diff -r 4dd67e5d958e -r ac3f78219f85 src/os/unix/ngx_freebsd_sendfile_chain.c
--- a/src/os/unix/ngx_freebsd_sendfile_chain.c	Wed Nov 19 21:17:11 2014 +0300
+++ b/src/os/unix/ngx_freebsd_sendfile_chain.c	Wed Aug 13 15:11:45 2014 +0400
@@ -33,7 +33,7 @@ ngx_chain_t *
 ngx_freebsd_sendfile_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit)
 {
     int              rc, flags;
-    off_t            size, send, prev_send, aligned, sent, fprev;
+    off_t            send, prev_send, sent;
     size_t           file_size;
     ngx_uint_t       eintr, eagain;
     ngx_err_t        err;
@@ -99,30 +99,9 @@ ngx_freebsd_sendfile_chain(ngx_connectio
 
             /* coalesce the neighbouring file bufs */
 
-            do {
-                size = cl->buf->file_last - cl->buf->file_pos;
+            file_size = (size_t) ngx_chain_coalesce_file(&cl, limit - send);
 
-                if (send + size > limit) {
-                    size = limit - send;
-
-                    aligned = (cl->buf->file_pos + size + ngx_pagesize - 1)
-                               & ~((off_t) ngx_pagesize - 1);
-
-                    if (aligned <= cl->buf->file_last) {
-                        size = aligned - cl->buf->file_pos;
-                    }
-                }
-
-                file_size += (size_t) size;
-                send += size;
-                fprev = cl->buf->file_pos + size;
-                cl = cl->next;
-
-            } while (cl
-                     && cl->buf->in_file
-                     && send < limit
-                     && file->file->fd == cl->buf->file->fd
-                     && fprev == cl->buf->file_pos);
+            send += file_size;
         }
 
 
diff -r 4dd67e5d958e -r ac3f78219f85 src/os/unix/ngx_linux_sendfile_chain.c
--- a/src/os/unix/ngx_linux_sendfile_chain.c	Wed Nov 19 21:17:11 2014 +0300
+++ b/src/os/unix/ngx_linux_sendfile_chain.c	Wed Aug 13 15:11:45 2014 +0400
@@ -31,7 +31,7 @@ ngx_chain_t *
 ngx_linux_sendfile_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit)
 {
     int            rc, tcp_nodelay;
-    off_t          size, send, prev_send, aligned, sent, fprev;
+    off_t          send, prev_send, sent;
     size_t         file_size;
     ngx_err_t      err;
     ngx_buf_t     *file;
@@ -153,30 +153,9 @@ ngx_linux_sendfile_chain(ngx_connection_
 
             /* coalesce the neighbouring file bufs */
 
-            do {
-                size = cl->buf->file_last - cl->buf->file_pos;
+            file_size = (size_t) ngx_chain_coalesce_file(&cl, limit - send);
 
-                if (send + size > limit) {
-                    size = limit - send;
-
-                    aligned = (cl->buf->file_pos + size + ngx_pagesize - 1)
-                               & ~((off_t) ngx_pagesize - 1);
-
-                    if (aligned <= cl->buf->file_last) {
-                        size = aligned - cl->buf->file_pos;
-                    }
-                }
-
-                file_size += (size_t) size;
-                send += size;
-                fprev = cl->buf->file_pos + size;
-                cl = cl->next;
-
-            } while (cl
-                     && cl->buf->in_file
-                     && send < limit
-                     && file->file->fd == cl->buf->file->fd
-                     && fprev == cl->buf->file_pos);
+            send += file_size;
         }
 
         if (file) {



More information about the nginx-devel mailing list