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_authn_dbi.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_authn_dbi.c')
-rw-r--r-- | src/mod_authn_dbi.c | 11 |
1 files changed, 5 insertions, 6 deletions
diff --git a/src/mod_authn_dbi.c b/src/mod_authn_dbi.c index 37de01f0..f375d7ea 100644 --- a/src/mod_authn_dbi.c +++ b/src/mod_authn_dbi.c @@ -128,8 +128,7 @@ mod_authn_dbi_dbconf_setup (server *srv, const array *opts, void **vdata) * - encoding, default: database default */ - if (!buffer_string_is_empty(sqlquery) - && !buffer_is_empty(dbname) && !buffer_is_empty(dbtype)) { + if (sqlquery && !buffer_is_blank(sqlquery) && dbname && dbtype) { /* create/initialise database */ dbi_config *dbconf; dbi_inst dbinst = NULL; @@ -153,7 +152,7 @@ mod_authn_dbi_dbconf_setup (server *srv, const array *opts, void **vdata) for (size_t j = 0; j < opts->used; ++j) { data_unset *du = opts->data[j]; const buffer *opt = &du->key; - if (!buffer_string_is_empty(opt)) { + if (!buffer_is_blank(opt)) { if (du->type == TYPE_INTEGER) { data_integer *di = (data_integer *)du; dbi_conn_set_option_numeric(dbconn, opt->ptr, di->value); @@ -474,7 +473,7 @@ mod_authn_dbi_query_build (buffer * const sqlquery, dbi_config * const dbconf, h free(esc); } else { - d = dbconf->sqlquery->ptr + buffer_string_length(dbconf->sqlquery); + d = dbconf->sqlquery->ptr + buffer_clen(dbconf->sqlquery); buffer_append_string_len(sqlquery, b, (size_t)(d - b)); break; } @@ -546,9 +545,9 @@ mod_authn_dbi_basic (request_st * const r, void *p_d, const http_auth_require_t ai.dalgo = HTTP_AUTH_DIGEST_NONE; ai.dlen = 0; ai.username = username->ptr; - ai.ulen = buffer_string_length(username); + ai.ulen = buffer_clen(username); ai.realm = require->realm->ptr; - ai.rlen = buffer_string_length(require->realm); + ai.rlen = buffer_clen(require->realm); rc = mod_authn_dbi_query(r, p_d, &ai, pw); if (HANDLER_GO_ON != rc) return rc; return http_auth_match_rules(require, username->ptr, NULL, NULL) |