[systemd-devel] [PATCH 2/2] RFC: util: Avoid doing work on the string when it is not used

Zbigniew Jędrzejewski-Szmek zbyszek at in.waw.pl
Thu Apr 4 20:44:34 PDT 2013


On Tue, Apr 02, 2013 at 10:35:51PM +0200, Holger Freyther wrote:
> From: Holger Hans Peter Freyther <holger at moiji-mobile.com>
> 
> The routine is dominated by the fgets and not the code that is
> moved around. Doing the work only for the lines that will be
> used should be a small gain. Routines in the selinux-access.c
> should probably query the gid and uid at the same time instead
> of opening and parsing the file twice.
It seems that this function must be invoked a bizillion times,
so making it a bit leaner probably makes sense. Your implicit
assumpition that the field is not prefixed by whitespace is
almost certainly true in this case. But it would be better to
mention it in the commit message. The strstrip on the line
can simply be dropped. But notice that startswith returns
the pointer to the end of the pattern (a relatively recent addition),
so this can be simplified further.

Zbyszek

> ---
>  src/shared/util.c |    6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/src/shared/util.c b/src/shared/util.c
> index ce8f866..d5ed615 100644
> --- a/src/shared/util.c
> +++ b/src/shared/util.c
> @@ -746,11 +746,9 @@ static int get_process_id(pid_t pid, const char *field, uid_t *uid) {
>                  return -errno;
>  
>          FOREACH_LINE(line, f, return -errno) {
> -                char *l;
>  
> -                l = strstrip(line);
> -
> -                if (startswith(l, field)) {
> +                if (startswith(line, field)) {
> +                        char *l = strstrip(line);
>                          l += strlen(field);
>                          l += strspn(l, WHITESPACE);
>  
> -- 
> 1.7.10.4
> 
> _______________________________________________
> systemd-devel mailing list
> systemd-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/systemd-devel
> 


More information about the systemd-devel mailing list