Changeset 0db0df2 in mainline for uspace/app/hbench/env.c


Ignore:
Timestamp:
2025-04-07T17:53:53Z (11 days ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
master
Children:
0c6fc7a, 45226285, 93de384
Parents:
8f8818ac
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2025-04-07 16:41:57)
git-committer:
Jiří Zárevúcky <zarevucky.jiri@…> (2025-04-07 17:53:53)
Message:

Hash table improvements

Implement hash_table_foreach macro, analogous to list_foreach.

Remove superfluous argument to hash_table_find_next().
(If the user needs to recheck the part of the list already
checked by hash_table_find(), they can just rerun that function.)

Add hash argument to hash_table_ops_t::key_equal.
The big change here is that users with big keys can store the hash
value alongside key in their entries, and for the low low cost of
sizeof(size_t) bytes eliminate a bunch of expensive key comparisons.

Also added a hash function for strings and arbitrary data.
Found this one by asking ChatGPT, because the latency of accesses
to my book collection is currently a couple of hours.

+ Some drive-by unused #include removal.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/hbench/env.c

    r8f8818ac r0db0df2  
    3434 */
    3535
     36#include <adt/hash.h>
    3637#include <stdlib.h>
    3738#include <stdio.h>
     
    4243        ht_link_t link;
    4344
     45        size_t hash;
    4446        char *key;
    4547        char *value;
     
    4951{
    5052        param_t *param = hash_table_get_inst(item, param_t, link);
    51         return str_size(param->key);
     53        return param->hash;
    5254}
    5355
    5456static size_t param_key_hash(const void *key)
    5557{
    56         const char *key_str = key;
    57         return str_size(key_str);
     58        return hash_string(key);
    5859}
    5960
    60 static bool param_key_equal(const void *key, const ht_link_t *item)
     61static bool param_key_equal(const void *key, size_t hash, const ht_link_t *item)
    6162{
    6263        param_t *param = hash_table_get_inst(item, param_t, link);
     64
     65        if (param->hash != hash)
     66                return false;
     67
    6368        const char *key_str = key;
    64 
    6569        return str_cmp(param->key, key_str) == 0;
    6670}
     
    7175        param_t *b = hash_table_get_inst(link_b, param_t, link);
    7276
    73         return str_cmp(a->key, b->key) == 0;
     77        return a->hash == b->hash && str_cmp(a->key, b->key) == 0;
    7478}
    7579
     
    116120        param->key = str_dup(key);
    117121        param->value = str_dup(value);
     122        param->hash = hash_string(key);
    118123
    119124        if ((param->key == NULL) || (param->value == NULL)) {
     
    132137const char *bench_env_param_get(bench_env_t *env, const char *key, const char *default_value)
    133138{
    134         ht_link_t *item = hash_table_find(&env->parameters, (char *) key);
     139        ht_link_t *item = hash_table_find(&env->parameters, key);
    135140
    136141        if (item == NULL) {
Note: See TracChangeset for help on using the changeset viewer.