]> granicus.if.org Git - php/commitdiff
Micro optimizations
authorXinchen Hui <laruence@gmail.com>
Tue, 26 May 2015 03:47:01 +0000 (11:47 +0800)
committerXinchen Hui <laruence@gmail.com>
Tue, 26 May 2015 03:47:01 +0000 (11:47 +0800)
main/fastcgi.c
main/fastcgi.h
sapi/cgi/cgi_main.c
sapi/fpm/fpm/fpm_main.c

index 675b9a88b0bd6c08c71baccb3f6fa1a2a4e27c5d..e2e356faacac67cb4c5a9d673756a2b1594c3fe6 100644 (file)
@@ -768,6 +768,10 @@ FCGI_API void fcgi_set_allowed_clients(char *ip)
        }
 }
 
+static void fcgi_hook_dummy() {
+       return;
+}
+
 FCGI_API fcgi_request *fcgi_init_request(fcgi_request *req, int listen_socket)
 {
        memset(req, 0, sizeof(fcgi_request));
@@ -790,19 +794,21 @@ FCGI_API fcgi_request *fcgi_init_request(fcgi_request *req, int listen_socket)
 
        */
        req->out_pos = req->out_buf;
+       req->hook.on_accept = fcgi_hook_dummy;
+       req->hook.on_read = fcgi_hook_dummy;
+       req->hook.on_close = fcgi_hook_dummy;
 
 #ifdef _WIN32
        req->tcp = !GetNamedPipeInfo((HANDLE)_get_osfhandle(req->listen_socket), NULL, NULL, NULL, NULL);
 #endif
+
        fcgi_hash_init(&req->env);
 
        return req;
 }
 
 FCGI_API void fcgi_destroy_request(fcgi_request *req) {
-       if (req->env.buckets) {
-               fcgi_hash_destroy(&req->env);
-       }
+       fcgi_hash_destroy(&req->env);
 }
 
 static inline ssize_t safe_write(fcgi_request *req, const void *buf, size_t count)
@@ -1193,9 +1199,7 @@ FCGI_API void fcgi_close(fcgi_request *req, int force, int destroy)
 #endif
                req->fd = -1;
 
-               if (req->hook.on_close) {
-                       req->hook.on_close();
-               }
+               req->hook.on_close();
        }
 }
 
@@ -1285,9 +1289,7 @@ FCGI_API int fcgi_accept_request(fcgi_request *req)
                                        sa_t sa;
                                        socklen_t len = sizeof(sa);
 
-                                       if (req->hook.on_accept) {
-                                               req->hook.on_accept();
-                                       }
+                                       req->hook.on_accept();
 
                                        FCGI_LOCK(req->listen_socket);
                                        req->fd = accept(listen_socket, (struct sockaddr *)&sa, &len);
@@ -1318,9 +1320,7 @@ FCGI_API int fcgi_accept_request(fcgi_request *req)
                                        struct pollfd fds;
                                        int ret;
 
-                                       if (req->hook.on_read) {
-                                               req->hook.on_read();
-                                       }
+                                       req->hook.on_read();
 
                                        fds.fd = req->fd;
                                        fds.events = POLLIN;
@@ -1334,9 +1334,7 @@ FCGI_API int fcgi_accept_request(fcgi_request *req)
                                        }
                                        fcgi_close(req, 1, 0);
 #else
-                                       if (req->hook.on_read) {
-                                               req->hook.on_read();
-                                       }
+                                       req->hook.on_read();
 
                                        if (req->fd < FD_SETSIZE) {
                                                struct timeval tv = {5,0};
index e5888228fe666fcff6e28af402839a0825c223f6..67203f1647d505412ad601826e57f75e5bb247e3 100644 (file)
@@ -181,17 +181,17 @@ struct _fcgi_request {
        int            nodelay;
 #endif
        int            closed;
-
-       fcgi_req_hook  hook;
-
        int            in_len;
        int            in_pad;
 
        fcgi_header   *out_hdr;
+
        unsigned char *out_pos;
        unsigned char  out_buf[1024*8];
        unsigned char  reserved[sizeof(fcgi_end_request_rec)];
 
+       fcgi_req_hook  hook;
+
        int            has_env;
        fcgi_hash      env;
 };
index e7b5722368d0acd0c51f95355a830d7078b0cdf2..c14aeb405a4804625e823dc4a2a8ebc3cd7af763 100644 (file)
@@ -2101,7 +2101,9 @@ consult the installation file that came with this distribution, or visit \n\
                                        break;
                                case 'h':
                                case '?':
-                                       fcgi_destroy_request(&request);
+                                       if (request.listen_socket) {
+                                               fcgi_destroy_request(&request);
+                                       }
                                        fcgi_shutdown();
                                        no_headers = 1;
                                        SG(headers_sent) = 1;
@@ -2533,7 +2535,10 @@ fastcgi_request_done:
                        }
                        /* end of fastcgi loop */
                }
-               fcgi_destroy_request(&request);
+               
+               if (request.listen_socket) {
+                       fcgi_destroy_request(&request);
+               }
                fcgi_shutdown();
 
                if (cgi_sapi_module.php_ini_path_override) {
index 373aee5defa9569a0ff659d8f4da133d9fabad00..559f2e1aea170e604f963178666706738c36cf82 100644 (file)
@@ -1863,7 +1863,7 @@ consult the installation file that came with this distribution, or visit \n\
        fpm_init_request(&request, fcgi_fd);
 
        zend_first_try {
-               while (fcgi_accept_request(&request) >= 0) {
+               while (EXPECTED(fcgi_accept_request(&request) >= 0)) {
                        request_body_fd = -1;
                        SG(server_context) = (void *) &request;
                        init_request_info();
@@ -1873,7 +1873,7 @@ consult the installation file that came with this distribution, or visit \n\
 
                        /* request startup only after we've done all we can to
                         *            get path_translated */
-                       if (php_request_startup() == FAILURE) {
+                       if (UNEXPECTED(php_request_startup() == FAILURE)) {
                                fcgi_finish_request(&request, 1);
                                SG(server_context) = NULL;
                                php_module_shutdown();
@@ -1882,16 +1882,16 @@ consult the installation file that came with this distribution, or visit \n\
 
                        /* check if request_method has been sent.
                         * if not, it's certainly not an HTTP over fcgi request */
-                       if (!SG(request_info).request_method) {
+                       if (UNEXPECTED(!SG(request_info).request_method)) {
                                goto fastcgi_request_done;
                        }
 
-                       if (fpm_status_handle_request()) {
+                       if (UNEXPECTED(fpm_status_handle_request())) {
                                goto fastcgi_request_done;
                        }
 
                        /* If path_translated is NULL, terminate here with a 404 */
-                       if (!SG(request_info).path_translated) {
+                       if (UNEXPECTED(!SG(request_info).path_translated)) {
                                zend_try {
                                        zlog(ZLOG_DEBUG, "Primary script unknown");
                                        SG(sapi_headers).http_response_code = 404;
@@ -1901,7 +1901,7 @@ consult the installation file that came with this distribution, or visit \n\
                                goto fastcgi_request_done;
                        }
 
-                       if (fpm_php_limit_extensions(SG(request_info).path_translated)) {
+                       if (UNEXPECTED(fpm_php_limit_extensions(SG(request_info).path_translated))) {
                                SG(sapi_headers).http_response_code = 403;
                                PUTS("Access denied.\n");
                                goto fastcgi_request_done;
@@ -1914,7 +1914,7 @@ consult the installation file that came with this distribution, or visit \n\
                        primary_script = estrdup(SG(request_info).path_translated);
 
                        /* path_translated exists, we can continue ! */
-                       if (php_fopen_primary_script(&file_handle) == FAILURE) {
+                       if (UNEXPECTED(php_fopen_primary_script(&file_handle) == FAILURE)) {
                                zend_try {
                                        zlog(ZLOG_ERROR, "Unable to open primary script: %s (%s)", primary_script, strerror(errno));
                                        if (errno == EACCES) {
@@ -1938,16 +1938,16 @@ consult the installation file that came with this distribution, or visit \n\
                        php_execute_script(&file_handle);
 
 fastcgi_request_done:
-                       if (primary_script) {
+                       if (EXPECTED(primary_script)) {
                                efree(primary_script);
                        }
 
-                       if (request_body_fd != -1) {
+                       if (UNEXPECTED(request_body_fd != -1)) {
                                close(request_body_fd);
                        }
                        request_body_fd = -2;
 
-                       if (EG(exit_status) == 255) {
+                       if (UNEXPECTED(EG(exit_status) == 255)) {
                                if (CGIG(error_header) && *CGIG(error_header)) {
                                        sapi_header_line ctr = {0};
 
@@ -1966,7 +1966,7 @@ fastcgi_request_done:
                        php_request_shutdown((void *) 0);
 
                        requests++;
-                       if (max_requests && (requests == max_requests)) {
+                       if (UNEXPECTED(max_requests && (requests == max_requests))) {
                                fcgi_finish_request(&request, 1);
                                break;
                        }