Every RHMAP_*_STR insert stores a private strdup() of the key. For
config_file that copy is pure overhead: the key handed to the map is
always the 'key' field of a struct config_entry_list that the config
file itself owns and that outlives the map at every site --
config_file_deinitialize tears the entry list down after the map,
config_unset removes the map slot before freeing the key, and both
config_file_add_child_list and config_append_file pilfer the donor's
entries before freeing the donor.
Add an opt-in RHMAP_BORROW_KEYS(map) that flips a per-map flag, so
inserts store the caller's pointer and teardown leaves it alone. The
flag lives in the header and is only read on insert, delete and free,
so the lookup path is untouched. Default behaviour is unchanged for
every existing user.
nested_list is the one other string-keyed user in tree and it was
keying off the caller's transient token rather than the copy it had
just made one line above. That is correct today only because the map
duplicates the key; make it key off its own copy so the call site is
right regardless, and bail cleanly if that copy failed to allocate
instead of hashing a NULL key.
Measured on a 1700-entry retroarch.cfg, load plus one
config_get_array() per key, 100 iterations, 5 runs each:
before 63.2 - 64.6 ms (mean 63.8)
after 52.8 - 54.9 ms (mean 54.0) -15.4%
cachegrind (--D1=49152,12,64 --LL=1048576,16,64):
D1 misses 124,714 -> 82,556 -34%
D refs 5.62M -> 4.53M -19%
Ir 14.67M -> 11.8M -20%
Allocation count for the same workload drops from 13,671 to 10,271,
exactly two per key. Identical lookup hit counts before and after;
valgrind reports no leaks and no errors.
Before this change 57% of D1 read misses on a config load were inside
malloc/free rather than in config code at all.