ifcfg-rh: add index for O(1) access of variables in shvarFile

System Internals / NetworkManager - Thomas Haller [redhat.com] - 21 December 2019 11:56 EST

Previously, setting or getting a variable required to scan all lines.

Note that frequently we would look up variables that didn't actually exist, which we could only determine after searching the entire list.

Also, since we needed to handle having the same variable specified multiple times (where the last occurrence wins), we always had to search all keys and couldn't stop when finding the first key. Well, technically we could have searched in reverse order for the getter, but that wasn't done. For the setter we wanted to delete all but the last occurrences, so to find them, we really had to search them all.

We want to support profiles with hundreds or thousands of addresses and routes. This does not scale well.

Add an hash table to find the variables in constant time.

Test this commit and the parent commit:

$ git clean -fdx && CFLAGS=-O2 ./autogen.sh --with-more-asserts=0 && ./tools/run-nm-test.sh -m src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh && perf stat -r 50 -B src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh 1>/dev/null

Before:

Performance counter stats for 'src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh' (50 runs):

330.94 msec task-clock:u # 0.961 CPUs utilized ( +- 0.33% ) 0 context-switches:u # 0.000 K/sec 0 cpu-migrations:u # 0.000 K/sec 1,081 page-faults:u # 0.003 M/sec ( +- 0.07% ) 1,035,923,116 cycles:u # 3.130 GHz ( +- 0.29% ) 1,800,084,022 instructions:u # 1.74 insn per cycle ( +- 0.01% ) 362,313,301 branches:u # 1094.784 M/sec ( +- 0.02% ) 6,259,421 branch-misses:u # 1.73% of all branches ( +- 0.13% )

0.34454 +- 0.00116 seconds time elapsed ( +- 0.34% )

Now:

Performance counter stats for 'src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh' (50 runs):

329.78 msec task-clock:u # 0.962 CPUs utilized ( +- 0.39% ) 0 context-switches:u # 0.000 K/sec 0 cpu-migrations:u # 0.000 K/sec 1,084 page-faults:u # 0.003 M/sec ( +- 0.05% ) 1,036,130,698 cycles:u # 3.142 GHz ( +- 0.13% ) 1,799,851,979 instructions:u # 1.74 insn per cycle ( +- 0.01% ) 360,374,338 branches:u # 1092.756 M/sec ( +- 0.01% ) 6,160,796 branch-misses:u # 1.71% of all branches ( +- 0.08% )

0.34287 +- 0.00133 seconds time elapsed ( +- 0.39% )

So, not much difference. But this is not surprising, because test-ifcfg-rh loads and writes predominantly ifcfg files with few variables. The difference should be visible when having large files.

d9bb13f8e ifcfg-rh: add index for O(1) access of variables in shvarFile
src/settings/plugins/ifcfg-rh/shvar.c | 137 ++++++++++++++++++++++------------
1 file changed, 88 insertions(+), 49 deletions(-)

Upstream: cgit.freedesktop.org


  • Share