lkml.org 
[lkml]   [2015]   [May]   [11]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patches in this message
/
Date
From
Subject[RFC PATCH] lib/vsprintf.c: Simplify uuid_string()
I didn't see your decimal print changes before they went in, but great
work! That's a real "why didn't I think of that?" change.


Anyway, merging some local patches I have with that prompted me to look
over that file and I came up with the following two patches which might
be interesting. RFC until I build a test harness and made sure there
aren't any stupid bugs, but the ideas are simple enough.

They're so small I'm breaking protocol and including them both in the same
e-mail.


From 065a49efdf601acdf8f9c2689259a4210a527da0 Mon Sep 17 00:00:00 2001
From: George Spelvin <linux@horizon.com>
Date: Mon, 11 May 2015 08:09:39 -0400
Subject: [PATCH 1/2] lib/vsprintf.c: Simplify uuid_string()

Rather than have a second pass to upcase the buffer, just make the
hex lookup table variable.

I suspect it's a speedup, but since this is not hot code, the important
part is that it shrinks the function from 332 to 256 bytes.

Signed-off-by: George Spelvin <linux@horizon.com>
---
lib/vsprintf.c | 17 ++++++-----------
1 file changed, 6 insertions(+), 11 deletions(-)

diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 452e4a16..de7f5bde 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -1270,21 +1270,23 @@ char *uuid_string(char *buf, char *end, const u8 *addr,
static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
const u8 *index = be;
- bool uc = false;
+ const char *hex = hex_asc;

switch (*(++fmt)) {
case 'L':
- uc = true; /* fall-through */
+ hex = hex_asc_upper; /* fall-through */
case 'l':
index = le;
break;
case 'B':
- uc = true;
+ hex = hex_asc_upper;
break;
}

for (i = 0; i < 16; i++) {
- p = hex_byte_pack(p, addr[index[i]]);
+ u8 byte = addr[index[i]];
+ *p++ = hex[x >> 4];
+ *p++ = hex[x & 0x0f];
switch (i) {
case 3:
case 5:
@@ -1297,13 +1299,6 @@ char *uuid_string(char *buf, char *end, const u8 *addr,

*p = 0;

- if (uc) {
- p = uuid;
- do {
- *p = toupper(*p);
- } while (*(++p));
- }
-
return string(buf, end, uuid, spec);
}

--
2.1.4

From 744056f73bb5625d52de61605b31300ba3d3fbc6 Mon Sep 17 00:00:00 2001
From: George Spelvin <linux@horizon.com>
Date: Mon, 11 May 2015 10:46:52 -0400
Subject: [PATCH 2/2] lib/vsprintf.c: Further simplify uuid_string().

Make the endianness permutation table do double duty by having it
list not source offsets, but destination offsets. Thus, it both puts
the bytes in the right order and skips the hyphens.

This further shrinks the code from 256 to 214 bytes. Eliminating
erratic branches probably helps speed, too.

Signed-off-by: George Spelvin <linux@horizon.com>
---
lib/vsprintf.c | 25 +++++++++----------------
1 file changed, 9 insertions(+), 16 deletions(-)
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index de7f5bde..38c1d87e 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -1265,10 +1265,9 @@ char *uuid_string(char *buf, char *end, const u8 *addr,
struct printf_spec spec, const char *fmt)
{
char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
- char *p = uuid;
int i;
- static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
- static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
+ static const u8 be[16] = {0,2,4,6,9,11,14,16,19,21,24,26,28,30,32,34};
+ static const u8 le[16] = {6,4,2,0,11,9,16,14,19,21,24,26,28,30,32,34};
const u8 *index = be;
const char *hex = hex_asc;

@@ -1284,20 +1283,14 @@ char *uuid_string(char *buf, char *end, const u8 *addr,
}

for (i = 0; i < 16; i++) {
- u8 byte = addr[index[i]];
- *p++ = hex[x >> 4];
- *p++ = hex[x & 0x0f];
- switch (i) {
- case 3:
- case 5:
- case 7:
- case 9:
- *p++ = '-';
- break;
- }
- }
+ u8 byte = addr[i];
+ char *p = uuid + index[i];

- *p = 0;
+ p[0] = hex[byte >> 4];
+ p[1] = hex[byte & 0x0f];
+ }
+ uuid[23] = uuid[18] = uuid[13] = uuid[8] = '-';
+ uuid[36] = 0;

return string(buf, end, uuid, spec);
}
--
2.1.4


\
 
 \ /
  Last update: 2015-05-11 19:01    [W:0.088 / U:12.908 seconds]
©2003-2018 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site