Re: [PATCH 3/3] lib/uuid: avoid double traversal in __uuid_parse()
From: Andy Shevchenko
Date: Fri Mar 13 2026 - 05:04:59 EST
On Thu, Mar 12, 2026 at 06:41:13PM +0000, Josh Law wrote:
> __uuid_parse() calls uuid_is_valid() to walk all 36 characters for
> format validation, then walks the string a second time to parse the
> hex bytes. Combine both passes into one: validate each hex digit
> inline via hex_to_bin() return value and check the four dash positions
> after the loop.
>
> uuid_is_valid() remains exported unchanged for callers that only need
> validation without parsing.
...
> - if (!uuid_is_valid(uuid))
> - return -EINVAL;
In case if valid string it heats up the caches, no?
> for (i = 0; i < 16; i++) {
> int hi = hex_to_bin(uuid[si[i] + 0]);
> int lo = hex_to_bin(uuid[si[i] + 1]);
> + if (hi < 0 || lo < 0)
> + return -EINVAL;
Here we add a branch (or two, depending on the code generation)
> b[ei[i]] = (hi << 4) | lo;
> }
> + if (uuid[8] != '-' || uuid[13] != '-' ||
> + uuid[18] != '-' || uuid[23] != '-')
> + return -EINVAL;
And one more here.
> return 0;
> }
If you a really into performance, please, provide the tests and the results to
show the benefit. Numbers will tell for themselves.
--
With Best Regards,
Andy Shevchenko