libsensors4: Renumber the features linearly
PATCH 4/7 libsensors4: Renumber the features linearly
Renumber the features linearly, so that feature number N is at position N in the array. This allows for O(1) look-ups, as opposed to O(N) before. This makes sensors_lookup_feature_nr() 2.4 times faster in my real-world tests, resulting in a 6% performance boost on average in the runtime part of "sensors".
--- lib/access.c | 8 ++------ lib/sysfs.c | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 6 deletions(-)
lib/sysfs.c +15 -0
@@ -178,6 +178,21 @@ static int sensors_read_dynamic_chip(sen } } +/* Renumber the features linearly, so that feature number N is at +position N in the array. This allows for O(1) look-ups. */ +for (i =0 ; i < fnum; i++) { +int j, old; + + old = dyn_features[i].data.number; + dyn_features[i].data.number = i; +for (j = i +1 ; + j < fnum && dyn_features[j].data.mapping !=SENSORS_NO_MAPPING ; + j++) { +if (dyn_features[j].data.mapping = old) + dyn_features[j].data.mapping = i; + } + } + chip->feature = dyn_features; exit_free:
lib/access.c +2 -6
@@ -92,15 +92,11 @@ sensors_for_all_config_chips(const sensoconst sensors_chip_feature *sensors_lookup_feature_nr (const sensors_chip_name *chip,int feature) { -int i, j; -const sensors_chip_feature *features; +int i;for (i =0 ; i < sensors_proc_chips_count; i++)if (sensors_match_chip (&sensors_proc_chips[i].chip, chip)) { - features = sensors_proc_chips[i].feature; -for (j =0 ; features[j].data.name; j++) -if (features[j].data.number = feature) -return features + j; +return sensors_proc_chips[i].feature + feature; }return NULL ; }
Signature · 6 lines
Jean Delvare _______________________________________________ lm-sensors mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/lm-sensors