summaryrefslogtreecommitdiff
path: root/src/network.c
diff options
context:
space:
mode:
authorGlenn Strauss <gstrauss@gluelogic.com>2021-06-08 22:57:36 -0400
committerGlenn Strauss <gstrauss@gluelogic.com>2021-08-27 02:16:53 -0400
commitaf3df29ae8276f4380ed25262bcdf3d95446a9b1 (patch)
tree2606d798d156da68dbcfc3f68a3c8d03490dfa11 /src/network.c
parent937d83b6cf8b732b2acae13919a8d944542acd9c (diff)
downloadlighttpd-git-af3df29ae8276f4380ed25262bcdf3d95446a9b1.tar.gz
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of hundreds, perhaps thousands, of CPU instructions, a portion of which are on hot code paths. Most (buffer *) used by lighttpd are not NULL, especially since buffers were inlined into numerous larger structs such as request_st and chunk. In the small number of instances where that is not the case, a NULL check is often performed earlier in a function where that buffer is later used with a buffer_* func. In the handful of cases that remained, a NULL check was added, e.g. with r->http_host and r->conf.server_tag. - check for empty strings at config time and set value to NULL if blank string will be ignored at runtime; at runtime, simple pointer check for NULL can be used to check for a value that has been set and is not blank ("") - use buffer_is_blank() instead of buffer_string_is_empty(), and use buffer_is_unset() instead of buffer_is_empty(), where buffer is known not to be NULL so that NULL check can be skipped - use buffer_clen() instead of buffer_string_length() when buffer is known not to be NULL (to avoid NULL check at runtime) - use buffer_truncate() instead of buffer_string_set_length() to truncate string, and use buffer_extend() to extend Examples where buffer known not to be NULL: - cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL (though we might set it to NULL if buffer_is_blank(cpv->v.b)) - address of buffer is arg (&foo) (compiler optimizer detects this in most, but not all, cases) - buffer is checked for NULL earlier in func - buffer is accessed in same scope without a NULL check (e.g. b->ptr) internal behavior change: callers must not pass a NULL buffer to some funcs. - buffer_init_buffer() requires non-null args - buffer_copy_buffer() requires non-null args - buffer_append_string_buffer() requires non-null args - buffer_string_space() requires non-null arg
Diffstat (limited to 'src/network.c')
-rw-r--r--src/network.c19
1 files changed, 10 insertions, 9 deletions
diff --git a/src/network.c b/src/network.c
index 6a1af49f..860d54ba 100644
--- a/src/network.c
+++ b/src/network.c
@@ -109,7 +109,7 @@ static int network_host_parse_addr(server *srv, sock_addr *addr, socklen_t *addr
const char *chost;
sa_family_t family = use_ipv6 ? AF_INET6 : AF_INET;
unsigned int port = srv->srvconf.port;
- if (buffer_string_is_empty(host)) {
+ if (buffer_is_blank(host)) {
log_error(srv->errh, __FILE__, __LINE__,
"value of $SERVER[\"socket\"] must not be empty");
return -1;
@@ -238,7 +238,7 @@ static uint8_t network_srv_token_colon (const buffer * const b) {
else if (*p != '/') {
colon = strchr(p, ':');
}
- return colon ? (uint8_t)(colon - p) : (uint8_t)buffer_string_length(b);
+ return colon ? (uint8_t)(colon - p) : (uint8_t)buffer_clen(b);
}
static int network_server_init(server *srv, network_socket_config *s, buffer *host_token, size_t sidx, int stdin_fd) {
@@ -249,7 +249,7 @@ static int network_server_init(server *srv, network_socket_config *s, buffer *ho
int family = 0;
int set_v6only = 0;
- if (buffer_string_is_empty(host_token)) {
+ if (buffer_is_blank(host_token)) {
log_error(srv->errh, __FILE__, __LINE__,
"value of $SERVER[\"socket\"] must not be empty");
return -1;
@@ -422,7 +422,7 @@ static int network_server_init(server *srv, network_socket_config *s, buffer *ho
}
if (-1 != stdin_fd) { } else
- if (AF_UNIX == family && !buffer_string_is_empty(s->socket_perms)) {
+ if (AF_UNIX == family && s->socket_perms) {
mode_t m = 0;
for (char *str = s->socket_perms->ptr; *str; ++str) {
m <<= 3;
@@ -451,7 +451,7 @@ static int network_server_init(server *srv, network_socket_config *s, buffer *ho
#endif
#if defined(__FreeBSD__) || defined(__NetBSD__) \
|| defined(__OpenBSD__) || defined(__DragonFly__)
- } else if (!buffer_is_empty(s->bsd_accept_filter)
+ } else if (s->bsd_accept_filter
&& (buffer_is_equal_string(s->bsd_accept_filter, CONST_STR_LEN("httpready"))
|| buffer_is_equal_string(s->bsd_accept_filter, CONST_STR_LEN("dataready")))) {
#ifdef SO_ACCEPTFILTER
@@ -674,11 +674,12 @@ int network_init(server *srv, int stdin_fd) {
/* process srv->srvconf.bindhost
* (skip if systemd socket activation is enabled and bindhost is empty;
* do not additionally listen on "*") */
- if (!srv->srvconf.systemd_socket_activation
- || !buffer_string_is_empty(srv->srvconf.bindhost)) {
+ if (!srv->srvconf.systemd_socket_activation || srv->srvconf.bindhost) {
buffer *b = buffer_init();
- buffer_copy_buffer(b, srv->srvconf.bindhost);
- if (b->ptr[0] != '/') { /*(skip adding port if unix socket path)*/
+ if (srv->srvconf.bindhost)
+ buffer_copy_buffer(b, srv->srvconf.bindhost);
+ /*(skip adding port if unix socket path)*/
+ if (!b->ptr || b->ptr[0] != '/') {
buffer_append_string_len(b, CONST_STR_LEN(":"));
buffer_append_int(b, srv->srvconf.port);
}