public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH] lib/vsprintf.c: Simplify uuid_string()
@ 2015-05-11 16:32 George Spelvin
  2015-05-11 16:49 ` Joe Perches
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: George Spelvin @ 2015-05-11 16:32 UTC (permalink / raw)
  To: linux; +Cc: linux, linux-kernel

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


^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2015-05-12 16:59 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-05-11 16:32 [RFC PATCH] lib/vsprintf.c: Simplify uuid_string() George Spelvin
2015-05-11 16:49 ` Joe Perches
2015-05-11 16:55   ` George Spelvin
2015-05-11 19:53 ` [PATCH 1/2] " George Spelvin
2015-05-11 19:55 ` [PATCH 2/2] lib/vsprintf.c: Further simplify uuid_string() George Spelvin
2015-05-12  2:14   ` Joe Perches
2015-05-12  9:10     ` [PATCH 2/2 v2] " George Spelvin
2015-05-12 11:26       ` Rasmus Villemoes
2015-05-12 13:57         ` George Spelvin
2015-05-12 16:59           ` Joe Perches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox