device: Optimize device_get_binding search efficiency

Optimize 'device_get_binding' function search so that
when a matching device is found, it returns NULL if
it is not ready.

Signed-off-by: James Roy <rruuaanng@outlook.com>
This commit is contained in:
James Roy 2024-11-25 18:15:36 +08:00 committed by Benjamin Cabé
commit ef8f037e78

View file

@ -35,20 +35,10 @@ const struct device *z_impl_device_get_binding(const char *name)
return NULL;
}
/* Split the search into two loops: in the common scenario, where
* device names are stored in ROM (and are referenced by the user
* with CONFIG_* macros), only cheap pointer comparisons will be
* performed. Reserve string comparisons for a fallback.
*/
/* Return NULL if the device matching 'name' is not ready. */
STRUCT_SECTION_FOREACH(device, dev) {
if (z_impl_device_is_ready(dev) && (dev->name == name)) {
return dev;
}
}
STRUCT_SECTION_FOREACH(device, dev) {
if (z_impl_device_is_ready(dev) && (strcmp(name, dev->name) == 0)) {
return dev;
if ((dev->name == name) || (strcmp(name, dev->name) == 0)) {
return z_impl_device_is_ready(dev) ? dev : NULL;
}
}