ifcfg-rh: use binary search for converting string to ethtool ID

System Internals / NetworkManager - Thomas Haller [redhat.com] - 6 March 2020 08:52 EST

Don't do a linear search through all names, but use binary search.

Upside: calling nms_ifcfg_rh_utils_get_ethtool_by_name() in a loop (once over all 60 names) is 75% faster.

Downside: when adding a new feature, we have yet another line that we need to add. Previously, adding a new feature required adding 7 lines, not it is 8. But we didn't add a single feature since this was added, so that happens very seldom.

Possible downside: is this code harder to read? Now we track both how to convert the ID to name and back. This is redundant (and thus harder to maintain). But it's really just one extra line per feature, for which there is a unit test. So, when adding a new NMEthtoolID it would be pretty hard to mess this up, because of all the tests and assertions. So, maybe it's slightly harder to read. On the other hand, it unifies handling for ethtool and kernel names, and the code has less logic and is more descriptive. I don't think this is actually harder to maintain and it should be easy to see that it is correct (readability).

d482eec6b ifcfg-rh: use binary search for converting string to ethtool ID
src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-utils.c | 118 +++++++++++++++------
1 file changed, 87 insertions(+), 31 deletions(-)

Upstream: cgit.freedesktop.org


  • Share