optimize the code for searching file in ngx_conf_open_file

Davood Falahati 0x0davood at gmail.com
Sat Oct 7 04:09:18 UTC 2023


# HG changeset patch
# User Davood Falahati <0x0davood at gmail.com>
# Date 1696647746 -7200
#      Sat Oct 07 05:02:26 2023 +0200
# Node ID ab14ea51bbb15331c9f44f14901d0fd378168647
# Parent  c073e545e1cdcc736f8869a012a78b2dd836eac9
optimize the code for searching file in ngx_conf_open_file

This patch combines two consecutive if statements into one and leveraging
short circuits circuiting. In the current code, we check the lengths of
file names and if they match, the file names are being compared.

I see few other places in the code that writing multiple if statements are
preferred over short circuit evaluation (e.g.
http://hg.nginx.org/nginx/file/tip/src/http/ngx_http_file_cache.c#l1153). I
wanted to make sure if it's a matter of community's taste or if it's in
line with some performance considerations?

diff -r c073e545e1cd -r ab14ea51bbb1 src/core/ngx_conf_file.c
--- a/src/core/ngx_conf_file.c Thu May 18 23:42:22 2023 +0200
+++ b/src/core/ngx_conf_file.c Sat Oct 07 05:02:26 2023 +0200
@@ -927,11 +927,7 @@
                 i = 0;
             }

-            if (full.len != file[i].name.len) {
-                continue;
-            }
-
-            if (ngx_strcmp(full.data, file[i].name.data) == 0) {
+        if (full.len == file[i].name.len && ngx_strcmp(full.data,
file[i].name.data) == 0) {
                 return &file[i];
             }
         }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20231007/a957765b/attachment.htm>


More information about the nginx-devel mailing list