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/http_header.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/http_header.c')
-rw-r--r-- | src/http_header.c | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/src/http_header.c b/src/http_header.c index dc20e0c0..7faf94de 100644 --- a/src/http_header.c +++ b/src/http_header.c @@ -181,7 +181,7 @@ int http_header_remove_token (buffer * const b, const char * const m, const uint } else { for (s -= mlen; *s != ',' && s != b->ptr; --s) ; - buffer_string_set_length(b, (size_t)(s - b->ptr)); + buffer_truncate(b, (size_t)(s - b->ptr)); break; } } @@ -193,7 +193,7 @@ int http_header_remove_token (buffer * const b, const char * const m, const uint static inline void http_header_token_append(buffer * const vb, const char * const v, const uint32_t vlen) { - if (!buffer_string_is_empty(vb)) + if (!buffer_is_blank(vb)) buffer_append_string_len(vb, CONST_STR_LEN(", ")); buffer_append_string_len(vb, v, vlen); } @@ -202,7 +202,7 @@ __attribute_cold__ static inline void http_header_token_append_cookie(buffer * const vb, const char * const v, const uint32_t vlen) { /* Cookie request header must be special-cased to use ';' separator * instead of ',' to combine multiple headers (if present) */ - if (!buffer_string_is_empty(vb)) + if (!buffer_is_blank(vb)) buffer_append_string_len(vb, CONST_STR_LEN("; ")); buffer_append_string_len(vb, v, vlen); } @@ -211,7 +211,7 @@ __attribute_pure__ static inline buffer * http_header_generic_get_ifnotempty(const array * const a, const enum http_header_e id, const char * const k, const uint32_t klen) { data_string * const ds = (data_string *)array_get_element_klen_ext(a, id, k, klen); - return ds && !buffer_string_is_empty(&ds->value) ? &ds->value : NULL; + return ds && !buffer_is_blank(&ds->value) ? &ds->value : NULL; } static inline void http_header_set_key_value(array * const a, enum http_header_e id, const char * const k, const size_t klen, const char * const v, const size_t vlen) { @@ -280,7 +280,7 @@ void http_header_response_insert(request_st * const r, enum http_header_e id, co if (0 == vlen) return; light_bset(r->resp_htags, id); buffer * const vb = array_get_buf_ptr_ext(&r->resp_headers, id, k, klen); - if (!buffer_string_is_empty(vb)) /*append repeated field-name on new line*/ + if (!buffer_is_blank(vb)) /*append repeated field-name on new line*/ http_header_response_insert_addtl(r, id, k, klen, vb, vlen); buffer_append_string_len(vb, v, vlen); } @@ -337,7 +337,7 @@ buffer * http_header_env_get(const request_st * const r, const char *k, uint32_t /* similar to http_header_generic_get_ifnotempty() but without id */ data_string * const ds = (data_string *)array_get_element_klen(&r->env, k, klen); - return ds && !buffer_string_is_empty(&ds->value) ? &ds->value : NULL; + return ds && !buffer_is_blank(&ds->value) ? &ds->value : NULL; } buffer * http_header_env_set_ptr(request_st *r, const char *k, uint32_t klen) { |