lib/uuid: avoid double traversal in __uuid_parse()
Subsystems (2)
PATCH 3/3 lib/uuid: avoid double traversal in __uuid_parse()
__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.
--- lib/uuid.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
lib/uuid.c +7 -3
@@ -108,16 +108,20 @@ static int __uuid_parse(const char *uuid, __u8 b[16], const u8 ei[16])static const u8 si[16 ] = {0 ,2 ,4 ,6 ,9 ,11 ,14 ,16 ,19 ,21 ,24 ,26 ,28 ,30 ,32 ,34 };unsigned int i; -if (!uuid_is_valid (uuid)) -return -EINVAL ; -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 ; + b[ei[i]] = (hi <<4 ) | lo; } +if (uuid[8 ] !='-' || uuid[13 ] !='-' || + uuid[18 ] !='-' || uuid[23 ] !='-' ) +return -EINVAL ; +return 0 ; }
Signature · 1 line
2.34.1
Andrew Morton
maintainerQuoted 23 lines · On Thu, 12 Mar 2026 18:41:13 +0000 Josh Law <[email protected]> wrote:
__uuid_parse()callsuuid_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 viahex_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.lib/uuid.c +1
-3@@ -108,16 +108,20 @@ static int __uuid_parse(const char *uuid, __u8 b[16], const u8 ei[16])static const u8 si[16 ] = {0 ,2 ,4 ,6 ,9 ,11 ,14 ,16 ,19 ,21 ,24 ,26 ,28 ,30 ,32 ,34 };unsigned int i; -if (!uuid_is_valid (uuid)) -return -EINVAL ; -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 )
lib/uuid.c static int __uuid_parse(const char *uuid, __u8 b[16], const u8 ei[16]) · line 125
Quoted 10 lines
lib/uuid.c +6
-0+return -EINVAL ; + b[ei[i]] = (hi <<4 ) | lo; } +if (uuid[8 ] !='-' || uuid[13 ] !='-' || + uuid[18 ] !='-' || uuid[23 ] !='-' ) +return -EINVAL ; +return 0 ;
This rather messifies the code, and for what? Is this in any way a hot path?
12 Mar 2026 20:48:59 Andrew Morton <[email protected]>:
Quoted 30 lines · On Thu, 12 Mar 2026 18:41:13 +0000 Josh Law <[email protected]> wrote:
Quoted 29 lines
__uuid_parse()callsuuid_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 viahex_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.lib/uuid.c +3
-3@@ -108,16 +108,20 @@ static int __uuid_parse(const char *uuid, __u8 b[16], const u8 ei[16]) static const u8 si[16] = {0,2,4,6,9,11,14,16,19,21,24,26,28,30,32,34}; unsigned int i; - if (!uuid_is_valid(uuid)) - return -EINVAL; - 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; + b[ei[i]] = (hi << 4) | lo; }
Quoted 8 lines
lib/uuid.c static int __uuid_parse(const char *uuid, __u8 b[16], const u8 ei[16]) · line 125
Quoted 5 lines
lib/uuid.c +4
-0+ if (uuid[8] != '-' || uuid[13] != '-' || + uuid[18] != '-' || uuid[23] != '-') + return -EINVAL; + return 0;This rather messifies the code, and for what? Is this in any way a hot path?
ehhhh. If I had to say, yes it is a hot path, it runs quite a lot
perf increases ever so slightly, so that's your call Andrew.
Andy Shevchenko
reviewerQuoted 8 lines · Josh Law
__uuid_parse()callsuuid_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 viahex_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.
...
lib/uuid.c static int __uuid_parse(const char *uuid, __u8 b[16], const u8 ei[16]) · line 112
Quoted 2 lines
lib/uuid.c +0
-2-if (!uuid_is_valid (uuid)) -return -EINVAL ;
In case if valid string it heats up the caches, no?
lib/uuid.c static int __uuid_parse(const char *uuid, __u8 b[16], const u8 ei[16]) · line 116
Quoted 6 lines
lib/uuid.c +2
-0for (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)
lib/uuid.c static int __uuid_parse(const char *uuid, __u8 b[16], const u8 ei[16]) · line 123
Quoted 6 lines
lib/uuid.c +3
-0b[ei[i]] = (hi <<4 ) | lo; } +if (uuid[8 ] !='-' || uuid[13 ] !='-' || + uuid[18 ] !='-' || uuid[23 ] !='-' ) +return -EINVAL ;
And one more here.
Quoted 2 lines
return 0;
}
If you a really into performance, please, provide the tests and the results to show the benefit. Numbers will tell for themselves.
Signature · 2 lines
With Best Regards, Andy Shevchenko