Skip to contentSkip to conversation

libsensors4: Optimize

PATCH In review Jean Delvare 2 participants 4 messages Jul 16, 2007 – Jul 22, 2007 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 3/3 libsensors4: Optimize

Minor optimizations to sensors_feature_get_type():
* We no longer support any single type, so no need to handle this case.
* We can start looking for "_" at offset 3 rather than the beginning of the string (the shortest valid prefix is "in0", 3 characters.)

---
 lib/access.c |   23 ++++++++---------------
 1 file changed, 8 insertions(+), 15 deletions(-)
lib/access.c +8 -15
@@ -522,8 +522,6 @@ struct feature_subtype_match
 struct feature_type_match
 {
 	const char *name;
-	sensors_feature_type type;
-	
 	const struct feature_subtype_match *submatches;
 };
 
@@ -568,10 +566,10 @@ static const struct feature_subtype_matc
 };
 
 static struct feature_type_match matches[] = { 
-	{ "temp%d%c", SENSORS_FEATURE_UNKNOWN, temp_matches },
-	{ "in%d%c", SENSORS_FEATURE_UNKNOWN, in_matches },
-	{ "fan%d%c", SENSORS_FEATURE_UNKNOWN, fan_matches },
-	{ "cpu%d%c", SENSORS_FEATURE_UNKNOWN, cpu_matches },
+	{ "temp%d%c", temp_matches },
+	{ "in%d%c", in_matches },
+	{ "fan%d%c", fan_matches },
+	{ "cpu%d%c", cpu_matches },
 };
 
 /* Return the feature type and channel number based on the feature name */
@@ -585,18 +583,13 @@ sensors_feature_type sensors_feature_get
 		if ((count = sscanf(name, matches[i].name, nr, &c)))
 			break;
 	
-	if (i = ARRAY_SIZE(matches)) /* no match */
-		return SENSORS_FEATURE_UNKNOWN;
-	else if (count = 1) /* single type */
-		return matches[i].type;
-
-	/* assert: count = 2 */
-	if (c != '_')
-		return SENSORS_FEATURE_UNKNOWN;
+	if (i = ARRAY_SIZE(matches) || count != 2 || c != '_')
+		return SENSORS_FEATURE_UNKNOWN;  /* no match */
 
 	submatches = matches[i].submatches;
+	name = strchr(name + 3, '_') + 1;
 	for (i = 0; submatches[i].name != NULL; i++)
-		if (!strcmp(strchr(name, '_') + 1, submatches[i].name))
+		if (!strcmp(name, submatches[i].name))
 			return submatches[i].type;
 	
 	return SENSORS_FEATURE_UNKNOWN;
Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#1 lore raw

Hi Hans,

Quoted 11 lines · Hans de Goede
Quoted 4 lines · Jean Delvare

Minor optimizations to sensors_feature_get_type():
* We no longer support any single type, so no need to handle this case.
* We can start looking for "_" at offset 3 rather than the beginning of the string (the shortest valid prefix is "in0", 3 characters.)

Nice cleanup!

I've checked the whole bunch of patches, and they look okay. Once you've applied them to svn I'll do some testing to see if there are any regressions, but I don't expect any,

Thanks for the review. I've committed the patches now. I hope to have time for more interface cleanups during the next week-end.

My next victim will probably be sensors_get_ignored(), I see no valid reason why each application has to care about ignore statements when libsensors itself could hide the ignored chip features to start with. I'm just not sure if the features should be skipped when the application asks for the feature list, or even earlier, not inserted in the feature list when walking the sysfs device directories.

Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#2 lore raw
In reply to Jean Delvare
Quoted 8 lines · Jean Delvare

Hi Hans,

Quoted 5 lines · Hans de Goede
Quoted 4 lines · Jean Delvare

Minor optimizations to sensors_feature_get_type():
* We no longer support any single type, so no need to handle this case.
* We can start looking for "_" at offset 3 rather than the beginning of the string (the shortest valid prefix is "in0", 3 characters.)

Quoted 9 lines
Quoted 5 lines

Nice cleanup!

I've checked the whole bunch of patches, and they look okay. Once you've applied them to svn I'll do some testing to see if there are any regressions, but I don't expect any,

Thanks for the review. I've committed the patches now. I hope to have time for more interface cleanups during the next week-end.

As promised, I've done some testing of the current svn with these patches integrated, everything still seems to work fine.

Quoted 7 lines

My next victim will probably be sensors_get_ignored(), I see no valid reason why each application has to care about ignore statements when libsensors itself could hide the ignored chip features to start with. I'm just not sure if the features should be skipped when the application asks for the feature list, or even earlier, not inserted in the feature list when walking the sysfs device directories.

Since both the config file loading and the sysfs reading happens only once, there is no advantage to delaying the hiding to the moment the applications asks for the feature list (If sensors.conf would/could be rescanned at that moment, that would be an argument to delay the hiding).

Thus this then purely becomes a question of where can it be implemented the cleanest, assuming that the current sensors.conf parsing code skips sections for not detected chips, doing this when parsing sysfs creates a chicken and egg problem, thus I think it would be best to handle the ignores when the apps asks for the feature list.

Regards,

Hans

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors

#3 lore raw
In reply to Jean Delvare

Hi Hans,

Quoted 6 lines · Hans de Goede
Quoted 2 lines · Jean Delvare

Thanks for the review. I've committed the patches now. I hope to have time for more interface cleanups during the next week-end.

As promised, I've done some testing of the current svn with these patches integrated, everything still seems to work fine.

Great, thanks.

Quoted 11 lines
Quoted 6 lines

My next victim will probably be sensors_get_ignored(), I see no valid reason why each application has to care about ignore statements when libsensors itself could hide the ignored chip features to start with. I'm just not sure if the features should be skipped when the application asks for the feature list, or even earlier, not inserted in the feature list when walking the sysfs device directories.

Since both the config file loading and the sysfs reading happens only once, there is no advantage to delaying the hiding to the moment the applications asks for the feature list (If sensors.conf would/could be rescanned at that moment, that would be an argument to delay the hiding).

sensors.conf _can_ be rescanned at any time. It doesn't make sense for "sensors", but it would make sense for GUI applications. That being said, we have a single function (sensors_init) to initialize the list of chips, the list of features, and do the configuration file parsing, so we still have full control on the order in which we want to do it. So your point is completely valid nevertheless.

Quoted 5 lines

Thus this then purely becomes a question of where can it be implemented the cleanest, assuming that the current sensors.conf parsing code skips sections for not detected chips, doing this when parsing sysfs creates a chicken and egg problem, thus I think it would be best to handle the ignores when the apps asks for the feature list.

I don't think we skip unused sections of sensors.conf, no. Mark, you are more familiar with that code than I am, can you please confirm? This would be a possible optimization, and a valuable one with the current fat default sensors.conf file we provide. But OTOH I think we agree that we want to take a different road in the future, with dedicated configuration files, so I'm not sure it's worth the effort.

If we really want to do this optimization, this wouldn't really be a chicken-and-egg problem. We can sequence the actions the following way:
* Read the list of chips from sysfs.
* Parse sensors.conf, skipping unused sections.
* For each chip, read the list of features from sysfs, skipping ignored ones.
This would mean reorganizing the code a bit, but nothing impossible.

Anyway, I don't really care about the performance at this point, only about the library API. I'll move the handling of ignore statements wherever it is easier now, so that applications no longer have to care about this. We can always optimize later.

Thanks,

Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors