libctf, hash: save per-item space when no key/item freeing function
authorNick Alcock <nick.alcock@oracle.com>
Tue, 2 Jun 2020 21:00:14 +0000 (22:00 +0100)
committerNick Alcock <nick.alcock@oracle.com>
Wed, 22 Jul 2020 16:57:44 +0000 (17:57 +0100)
commita49c6c6a656c429dc222e04628e085a903194b51
treec2d5bb380ddfbf7745db6619f6aa69fa21a1a2e5
parent5ceee3dba3422bc8de49768c0c2d8f2608672fe7
libctf, hash: save per-item space when no key/item freeing function

The libctf dynhash hashtab abstraction supports per-hashtab arbitrary
key/item freeing functions -- but it also has a constant slot type that
holds both key and value requested by the user, so it needs to use its
own freeing function to free that -- and it has nowhere to store the
freeing functions the caller requested.

So it copies them into every hash item, bloating every slot, even though
all items in a given hash table must have the same key and value freeing
functions.

So point back to the owner using a back-pointer, but don't even spend
space in the item or the hashtab allocating those freeing functions
unless necessary: if none are needed, we can simply arrange to not pass
in ctf_dynhash_item_free as a del_f to hashtab_create_alloc, and none of
those fields will ever be accessed.

The only downside is that this makes the code sensitive to the order of
fields in the ctf_helem_t and ctf_hashtab_t: but the deduplicator
allocates so many hash tables that doing this alone cuts memory usage
during deduplication by about 10%.  (libiberty hashtab itself has a lot
of per-hashtab bloat: in the future we might trim that down, or make a
trimmer version.)

libctf/
* ctf-hash.c (ctf_helem_t) <key_free>: Remove.
<value_free>: Likewise.
<owner>: New.
(ctf_dynhash_item_free): Indirect through the owner.
(ctf_dynhash_create): Only pass in ctf_dynhash_item_free and
allocate space for the key_free and value_free fields fields
if necessary.
(ctf_hashtab_insert): Likewise.  Fix OOM errno value.
(ctf_dynhash_insert): Only access ctf_hashtab's key_free and
value_free if they will exist.  Set the slot's owner, but only
if it exists.
(ctf_dynhash_remove): Adjust.
libctf/ChangeLog
libctf/ctf-hash.c
This page took 0.025768 seconds and 4 git commands to generate.