Patch to optimize bonobo's hex encoding functions



Michael,

This patch makes the read_byte and write_byte functions in
bonobo-ui-util.c use lookup tables to do the conversion. I've done
timings both before and after, and the patch increases speed per byte
by around 80-100% (on my PIII)

Any objections?

	John


Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/bonobo/ChangeLog,v
retrieving revision 1.1000
diff -u -p -r1.1000 ChangeLog
--- ChangeLog	2001/02/25 00:59:38	1.1000
+++ ChangeLog	2001/02/26 01:23:59
@@ -1,3 +1,9 @@
+2001-02-24  John Harper  <jsh eazel com>
+
+	* bonobo/bonobo-ui-util.c (write_byte, read_byte): rewritten to
+	use lookup tables to do data conversion. Also made inline
+	(write_four_bytes, read_four_bytes): made inline
+
 2001-02-25  Michael Meeks  <michael ximian com>
 
 	* bonobo/bonobo-async.c: Fully API document.
Index: bonobo/bonobo-ui-util.c
===================================================================
RCS file: /cvs/gnome/bonobo/bonobo/bonobo-ui-util.c,v
retrieving revision 1.40
diff -u -p -r1.40 bonobo-ui-util.c
--- bonobo/bonobo-ui-util.c	2001/02/21 10:19:28	1.40
+++ bonobo/bonobo-ui-util.c	2001/02/26 01:23:59
@@ -17,81 +17,76 @@
 #include <gnome-xml/tree.h>
 #include <gnome-xml/parser.h>
 
-static void
+static const char write_lut[16] = {
+	'0', '1', '2', '3', '4', '5', '6', '7',
+	'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
+};
+
+static const gint8 read_lut[128] = {
+	 -1, -1, -1, -1, -1, -1, -1, -1,		/* 0x00 -> 0x07 */
+	 -1, -1, -1, -1, -1, -1, -1, -1,
+	 -1, -1, -1, -1, -1, -1, -1, -1,		/* 0x10 -> 0x17 */
+	 -1, -1, -1, -1, -1, -1, -1, -1,
+	 -1, -1, -1, -1, -1, -1, -1, -1,		/* 0x20 -> 0x27 */
+	 -1, -1, -1, -1, -1, -1, -1, -1,
+	 0,  1,  2,  3,  4,  5,  6,  7,			/* 0x30 -> 0x37 */
+	 8,  9, -1, -1, -1, -1, -1, -1,
+	 -1, 10, 11, 12, 13, 14, 15, -1,		/* 0x40 -> 0x47 */
+	 -1, -1, -1, -1, -1, -1, -1, -1,
+	 -1, -1, -1, -1, -1, -1, -1, -1,		/* 0x50 -> 0x57 */
+	 -1, -1, -1, -1, -1, -1, -1, -1,
+	 -1, 10, 11, 12, 13, 14, 15, -1,		/* 0x60 -> 0x67 */
+	 -1, -1, -1, -1, -1, -1, -1, -1,
+	 -1, -1, -1, -1, -1, -1, -1, -1,		/* 0x70 -> 0x77 */
+	 -1, -1, -1, -1, -1, -1, -1, -1,
+};
+
+static inline void
 write_byte (char *start, guint8 byte)
 {
-	int chunk;
-
-	chunk = (byte >> 4) & 0xf;
-
-	if (chunk < 10)
-		*start++ = '0' + chunk;
-	else
-		*start++ = 'a' + chunk - 10;
-
-	chunk = byte & 0xf;
-
-	if (chunk < 10)
-		*start = '0' + chunk;
-	else
-		*start = 'a' + chunk - 10;
+	start[0] = write_lut[byte >> 4];
+	start[1] = write_lut[byte & 15];
 }
 
-static char *
+static inline void
 write_four_bytes (char *pos, int value) 
 {
-	write_byte (pos, value >> 24);
-	pos += 2;
-	write_byte (pos, value >> 16);
-	pos += 2;
-	write_byte (pos, value >> 8);
-	pos += 2;
-	write_byte (pos, value);
-	pos += 2;
+	write_byte (pos + 0, value >> 24);
+	write_byte (pos + 2, value >> 16);
+	write_byte (pos + 4, value >> 8);
+	write_byte (pos + 6, value);
+}
 
-	return pos;
+static void
+read_warning (const char *start)
+{
+	g_warning ("Format error in stream '%c', '%c'", start[0], start[1]);
 }
 
-static guint8
+static inline guint8
 read_byte (const char *start)
 {
-	int chunk = 0;
+	guint8 byte1, byte2;
+	gint8 nibble1, nibble2;
+
+	byte1 = start[0];
+	byte2 = start[1];
+
+	if (byte1 >= 128 || byte2 >= 128) {
+		read_warning (start);
+	}
+
+	nibble1 = read_lut[byte1];
+	nibble2 = read_lut[byte2];
 
-	if (*start >= '0' &&
-	    *start <= '9')
-		chunk |= *start - '0';
-
-	else if (*start >= 'a' &&
-		 *start <= 'f')
-		chunk |= *start - 'a' + 10;
-
-	else if (*start >= 'A' &&
-		 *start <= 'F')
-		chunk |= *start - 'A' + 10;
-	else
-		g_warning ("Format error in stream '%c'", *start);
-
-	chunk <<= 4;
-	start++;
-
-	if (*start >= '0' &&
-	    *start <= '9')
-		chunk |= *start - '0';
-
-	else if (*start >= 'a' &&
-		 *start <= 'f')
-		chunk |= *start - 'a' + 10;
-
-	else if (*start >= 'A' &&
-		 *start <= 'F')
-		chunk |= *start - 'A' + 10;
-	else
-		g_warning ("Format error in stream '%c'", *start);
+	if (nibble1 < 0 || nibble2 < 0) {
+		read_warning (start);
+	}
 
-	return chunk;
+	return (nibble1 << 4) + nibble2;
 }
 
-static const guint32
+static inline const guint32
 read_four_bytes (const char *pos)
 {
 	return ((read_byte (pos) << 24) |





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]