Skip to content

Commit 5f753f9

Browse files
committed
Micro optimizations
1 parent 06483a9 commit 5f753f9

File tree

4 files changed

+34
-31
lines changed

4 files changed

+34
-31
lines changed

main/fastcgi.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,10 @@ FCGI_API void fcgi_set_allowed_clients(char *ip)
768768
}
769769
}
770770

771+
static void fcgi_hook_dummy() {
772+
return;
773+
}
774+
771775
FCGI_API fcgi_request *fcgi_init_request(fcgi_request *req, int listen_socket)
772776
{
773777
memset(req, 0, sizeof(fcgi_request));
@@ -790,19 +794,21 @@ FCGI_API fcgi_request *fcgi_init_request(fcgi_request *req, int listen_socket)
790794
791795
*/
792796
req->out_pos = req->out_buf;
797+
req->hook.on_accept = fcgi_hook_dummy;
798+
req->hook.on_read = fcgi_hook_dummy;
799+
req->hook.on_close = fcgi_hook_dummy;
793800

794801
#ifdef _WIN32
795802
req->tcp = !GetNamedPipeInfo((HANDLE)_get_osfhandle(req->listen_socket), NULL, NULL, NULL, NULL);
796803
#endif
804+
797805
fcgi_hash_init(&req->env);
798806

799807
return req;
800808
}
801809

802810
FCGI_API void fcgi_destroy_request(fcgi_request *req) {
803-
if (req->env.buckets) {
804-
fcgi_hash_destroy(&req->env);
805-
}
811+
fcgi_hash_destroy(&req->env);
806812
}
807813

808814
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)
11931199
#endif
11941200
req->fd = -1;
11951201

1196-
if (req->hook.on_close) {
1197-
req->hook.on_close();
1198-
}
1202+
req->hook.on_close();
11991203
}
12001204
}
12011205

@@ -1285,9 +1289,7 @@ FCGI_API int fcgi_accept_request(fcgi_request *req)
12851289
sa_t sa;
12861290
socklen_t len = sizeof(sa);
12871291

1288-
if (req->hook.on_accept) {
1289-
req->hook.on_accept();
1290-
}
1292+
req->hook.on_accept();
12911293

12921294
FCGI_LOCK(req->listen_socket);
12931295
req->fd = accept(listen_socket, (struct sockaddr *)&sa, &len);
@@ -1318,9 +1320,7 @@ FCGI_API int fcgi_accept_request(fcgi_request *req)
13181320
struct pollfd fds;
13191321
int ret;
13201322

1321-
if (req->hook.on_read) {
1322-
req->hook.on_read();
1323-
}
1323+
req->hook.on_read();
13241324

13251325
fds.fd = req->fd;
13261326
fds.events = POLLIN;
@@ -1334,9 +1334,7 @@ FCGI_API int fcgi_accept_request(fcgi_request *req)
13341334
}
13351335
fcgi_close(req, 1, 0);
13361336
#else
1337-
if (req->hook.on_read) {
1338-
req->hook.on_read();
1339-
}
1337+
req->hook.on_read();
13401338

13411339
if (req->fd < FD_SETSIZE) {
13421340
struct timeval tv = {5,0};

main/fastcgi.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,17 @@ struct _fcgi_request {
181181
int nodelay;
182182
#endif
183183
int closed;
184-
185-
fcgi_req_hook hook;
186-
187184
int in_len;
188185
int in_pad;
189186

190187
fcgi_header *out_hdr;
188+
191189
unsigned char *out_pos;
192190
unsigned char out_buf[1024*8];
193191
unsigned char reserved[sizeof(fcgi_end_request_rec)];
194192

193+
fcgi_req_hook hook;
194+
195195
int has_env;
196196
fcgi_hash env;
197197
};

sapi/cgi/cgi_main.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,7 +2101,9 @@ consult the installation file that came with this distribution, or visit \n\
21012101
break;
21022102
case 'h':
21032103
case '?':
2104-
fcgi_destroy_request(&request);
2104+
if (request.listen_socket) {
2105+
fcgi_destroy_request(&request);
2106+
}
21052107
fcgi_shutdown();
21062108
no_headers = 1;
21072109
SG(headers_sent) = 1;
@@ -2533,7 +2535,10 @@ consult the installation file that came with this distribution, or visit \n\
25332535
}
25342536
/* end of fastcgi loop */
25352537
}
2536-
fcgi_destroy_request(&request);
2538+
2539+
if (request.listen_socket) {
2540+
fcgi_destroy_request(&request);
2541+
}
25372542
fcgi_shutdown();
25382543

25392544
if (cgi_sapi_module.php_ini_path_override) {

sapi/fpm/fpm/fpm_main.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,7 +1863,7 @@ consult the installation file that came with this distribution, or visit \n\
18631863
fpm_init_request(&request, fcgi_fd);
18641864

18651865
zend_first_try {
1866-
while (fcgi_accept_request(&request) >= 0) {
1866+
while (EXPECTED(fcgi_accept_request(&request) >= 0)) {
18671867
request_body_fd = -1;
18681868
SG(server_context) = (void *) &request;
18691869
init_request_info();
@@ -1873,7 +1873,7 @@ consult the installation file that came with this distribution, or visit \n\
18731873

18741874
/* request startup only after we've done all we can to
18751875
* get path_translated */
1876-
if (php_request_startup() == FAILURE) {
1876+
if (UNEXPECTED(php_request_startup() == FAILURE)) {
18771877
fcgi_finish_request(&request, 1);
18781878
SG(server_context) = NULL;
18791879
php_module_shutdown();
@@ -1882,16 +1882,16 @@ consult the installation file that came with this distribution, or visit \n\
18821882

18831883
/* check if request_method has been sent.
18841884
* if not, it's certainly not an HTTP over fcgi request */
1885-
if (!SG(request_info).request_method) {
1885+
if (UNEXPECTED(!SG(request_info).request_method)) {
18861886
goto fastcgi_request_done;
18871887
}
18881888

1889-
if (fpm_status_handle_request()) {
1889+
if (UNEXPECTED(fpm_status_handle_request())) {
18901890
goto fastcgi_request_done;
18911891
}
18921892

18931893
/* If path_translated is NULL, terminate here with a 404 */
1894-
if (!SG(request_info).path_translated) {
1894+
if (UNEXPECTED(!SG(request_info).path_translated)) {
18951895
zend_try {
18961896
zlog(ZLOG_DEBUG, "Primary script unknown");
18971897
SG(sapi_headers).http_response_code = 404;
@@ -1901,7 +1901,7 @@ consult the installation file that came with this distribution, or visit \n\
19011901
goto fastcgi_request_done;
19021902
}
19031903

1904-
if (fpm_php_limit_extensions(SG(request_info).path_translated)) {
1904+
if (UNEXPECTED(fpm_php_limit_extensions(SG(request_info).path_translated))) {
19051905
SG(sapi_headers).http_response_code = 403;
19061906
PUTS("Access denied.\n");
19071907
goto fastcgi_request_done;
@@ -1914,7 +1914,7 @@ consult the installation file that came with this distribution, or visit \n\
19141914
primary_script = estrdup(SG(request_info).path_translated);
19151915

19161916
/* path_translated exists, we can continue ! */
1917-
if (php_fopen_primary_script(&file_handle) == FAILURE) {
1917+
if (UNEXPECTED(php_fopen_primary_script(&file_handle) == FAILURE)) {
19181918
zend_try {
19191919
zlog(ZLOG_ERROR, "Unable to open primary script: %s (%s)", primary_script, strerror(errno));
19201920
if (errno == EACCES) {
@@ -1938,16 +1938,16 @@ consult the installation file that came with this distribution, or visit \n\
19381938
php_execute_script(&file_handle);
19391939

19401940
fastcgi_request_done:
1941-
if (primary_script) {
1941+
if (EXPECTED(primary_script)) {
19421942
efree(primary_script);
19431943
}
19441944

1945-
if (request_body_fd != -1) {
1945+
if (UNEXPECTED(request_body_fd != -1)) {
19461946
close(request_body_fd);
19471947
}
19481948
request_body_fd = -2;
19491949

1950-
if (EG(exit_status) == 255) {
1950+
if (UNEXPECTED(EG(exit_status) == 255)) {
19511951
if (CGIG(error_header) && *CGIG(error_header)) {
19521952
sapi_header_line ctr = {0};
19531953

@@ -1966,7 +1966,7 @@ consult the installation file that came with this distribution, or visit \n\
19661966
php_request_shutdown((void *) 0);
19671967

19681968
requests++;
1969-
if (max_requests && (requests == max_requests)) {
1969+
if (UNEXPECTED(max_requests && (requests == max_requests))) {
19701970
fcgi_finish_request(&request, 1);
19711971
break;
19721972
}

0 commit comments

Comments
 (0)