gpio: sysfs: don't use sprintf() for 'value' attribute
authorChristophe Leroy <christophe.leroy@c-s.fr>
Mon, 18 Dec 2017 10:08:33 +0000 (11:08 +0100)
committerLinus Walleij <linus.walleij@linaro.org>
Wed, 20 Dec 2017 09:34:01 +0000 (10:34 +0100)
A bench with 'perf record' shows that most of time spent in value_show()
is spent in sprintf()

--42.41%--sysfs_kf_read
          |
          |--39.73%--dev_attr_show
          |          |
          |          |--38.23%--value_show
          |          |          |
          |          |          |--29.22%--sprintf
          |          |          |
          |          |          |--2.94%--gpiod_get_value_cansleep
          |          |          |

value_show() only returns "0\n" or "1\n", therefore the use of
sprintf() can be avoided

With this patch we get the following result with 'perf record'

--13.89%--sysfs_kf_read
          |
          |--10.72%--dev_attr_show
          |          |
          |          |--9.44%--value_show
          |          |          |
          |          |          |--4.61%--gpiod_get_value_cansleep

Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
drivers/gpio/gpiolib-sysfs.c

index ef34b8f..ad7173d 100644 (file)
@@ -110,7 +110,9 @@ static ssize_t value_show(struct device *dev,
        if (status < 0)
                goto err;
 
-       status = sprintf(buf, "%d\n", status);
+       buf[0] = '0' + status;
+       buf[1] = '\n';
+       status = 2;
 err:
        mutex_unlock(&data->mutex);