diff options
author | Glenn Strauss <gstrauss@gluelogic.com> | 2021-06-08 22:57:36 -0400 |
---|---|---|
committer | Glenn Strauss <gstrauss@gluelogic.com> | 2021-08-27 02:16:53 -0400 |
commit | af3df29ae8276f4380ed25262bcdf3d95446a9b1 (patch) | |
tree | 2606d798d156da68dbcfc3f68a3c8d03490dfa11 /src/mod_alias.c | |
parent | 937d83b6cf8b732b2acae13919a8d944542acd9c (diff) | |
download | lighttpd-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/mod_alias.c')
-rw-r--r-- | src/mod_alias.c | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/src/mod_alias.c b/src/mod_alias.c index d2c67f30..119c3dcc 100644 --- a/src/mod_alias.c +++ b/src/mod_alias.c @@ -52,10 +52,10 @@ static void mod_alias_patch_config(request_st * const r, plugin_data * const p) static int mod_alias_check_order(server * const srv, const array * const a) { for (uint32_t j = 0; j < a->used; ++j) { const buffer * const prefix = &a->data[j]->key; - const size_t plen = buffer_string_length(prefix); + const size_t plen = buffer_clen(prefix); for (uint32_t k = j + 1; k < a->used; ++k) { const buffer * const key = &a->data[k]->key; - if (buffer_string_length(key) < plen) { + if (buffer_clen(key) < plen) { break; } if (memcmp(key->ptr, prefix->ptr, plen) != 0) { @@ -121,10 +121,10 @@ static handler_t mod_alias_remap (request_st * const r, const array * const aliases) { /* do not include trailing slash on basedir */ - uint32_t basedir_len = buffer_string_length(&r->physical.basedir); + uint32_t basedir_len = buffer_clen(&r->physical.basedir); if (buffer_has_pathsep_suffix(&r->physical.basedir)) --basedir_len; - const uint32_t path_len = buffer_string_length(&r->physical.path); + const uint32_t path_len = buffer_clen(&r->physical.path); if (0 == path_len || path_len < basedir_len) return HANDLER_GO_ON; const uint32_t uri_len = path_len - basedir_len; @@ -137,8 +137,8 @@ mod_alias_remap (request_st * const r, const array * const aliases) /* matched */ - const uint32_t alias_len = buffer_string_length(&ds->key); - const uint32_t vlen = buffer_string_length(&ds->value); + const uint32_t alias_len = buffer_clen(&ds->key); + const uint32_t vlen = buffer_clen(&ds->value); /* check for path traversal in url-path following alias if key * does not end in slash, but replacement value ends in slash */ @@ -163,7 +163,7 @@ mod_alias_remap (request_st * const r, const array * const aliases) buffer_string_prepare_append(&r->physical.path, nlen - path_len); memmove(r->physical.path.ptr + vlen, uri_ptr + alias_len, uri_len - alias_len); - buffer_string_set_length(&r->physical.path, nlen); + buffer_truncate(&r->physical.path, nlen); } memcpy(r->physical.path.ptr, ds->value.ptr, vlen); |