Skip to content

Commit

Permalink
htp/table: only fetch element when needed
Browse files Browse the repository at this point in the history
It is unnecessary to always fetch the element as it is only needed when
there is a match. Get the list item only when the key candidate matches
the key parameter and the element needs to be returned. This will ensure
lesser calls to htp_list_get fn.
  • Loading branch information
inashivb committed Aug 15, 2024
1 parent ce25f9e commit 09d8110
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions htp/htp_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ void *htp_table_get(const htp_table_t *table, const bstr *key) {
// keys with the parameter, return data if found.
for (size_t i = 0, n = htp_list_size(&table->list); i < n; i += 2) {
bstr *key_candidate = htp_list_get(&table->list, i);
void *element = htp_list_get(&table->list, i + 1);
if (bstr_cmp_nocase(key_candidate, key) == 0) {
void *element = htp_list_get(&table->list, i + 1);
return element;
}
}
Expand All @@ -207,8 +207,8 @@ void *htp_table_get_c(const htp_table_t *table, const char *ckey) {
// keys with the parameter, return data if found.
for (size_t i = 0, n = htp_list_size(&table->list); i < n; i += 2) {
bstr *key_candidate = htp_list_get(&table->list, i);
void *element = htp_list_get(&table->list, i + 1);
if (bstr_cmp_c_nocasenorzero(key_candidate, ckey) == 0) {
void *element = htp_list_get(&table->list, i + 1);
return element;
}
}
Expand All @@ -235,8 +235,8 @@ void *htp_table_get_mem(const htp_table_t *table, const void *key, size_t key_le
// keys with the parameter, return data if found.
for (size_t i = 0, n = htp_list_size(&table->list); i < n; i += 2) {
bstr *key_candidate = htp_list_get(&table->list, i);
void *element = htp_list_get(&table->list, i + 1);
if (bstr_cmp_mem_nocase(key_candidate, key, key_len) == 0) {
void *element = htp_list_get(&table->list, i + 1);
return element;
}
}
Expand Down

0 comments on commit 09d8110

Please sign in to comment.