get_sha1_hex() micro-optimization
The function appeared high on a gprof output for a rev-list run of a non-trivial size, and it was an obvious low-hanging fruit. The code is from Linus. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Sep 9, 2006 at 22:21 UTC
a41fae9c46a4cb5e59cc1a17d19f6a3a6cbfbb2f
1 file changed
+37
-9
sha1_file.c
+37
-9
@@ -26,15 +26,43 @@ const unsigned char null_sha1[20];
26
27
static unsigned int sha1_file_open_flag = O_NOATIME;
28
29
-static unsigned hexval(char c)
30
-{
31
- if (c >= '0' && c <= '9')
32
- return c - '0';
33
- if (c >= 'a' && c <= 'f')
34
- return c - 'a' + 10;
35
- if (c >= 'A' && c <= 'F')
36
- return c - 'A' + 10;
37
- return ~0;
29
+static inline unsigned int hexval(unsigned int c)
30
+{
31
+ static signed char val[256] = {
32
+ -1, -1, -1, -1, -1, -1, -1, -1, /* 00-07 */
33
+ -1, -1, -1, -1, -1, -1, -1, -1, /* 08-0f */
34
+ -1, -1, -1, -1, -1, -1, -1, -1, /* 10-17 */
35
+ -1, -1, -1, -1, -1, -1, -1, -1, /* 18-1f */
36
+ -1, -1, -1, -1, -1, -1, -1, -1, /* 20-27 */
37
+ -1, -1, -1, -1, -1, -1, -1, -1, /* 28-2f */
38
+ 0, 1, 2, 3, 4, 5, 6, 7, /* 30-37 */
39
+ 8, 9, -1, -1, -1, -1, -1, -1, /* 38-3f */
40
+ -1, 10, 11, 12, 13, 14, 15, -1, /* 40-47 */
41
+ -1, -1, -1, -1, -1, -1, -1, -1, /* 48-4f */
42
+ -1, -1, -1, -1, -1, -1, -1, -1, /* 50-57 */
43
+ -1, -1, -1, -1, -1, -1, -1, -1, /* 58-5f */
44
+ -1, 10, 11, 12, 13, 14, 15, -1, /* 60-67 */
45
+ -1, -1, -1, -1, -1, -1, -1, -1, /* 68-67 */
46
+ -1, -1, -1, -1, -1, -1, -1, -1, /* 70-77 */
47
+ -1, -1, -1, -1, -1, -1, -1, -1, /* 78-7f */
48
+ -1, -1, -1, -1, -1, -1, -1, -1, /* 80-87 */
49
+ -1, -1, -1, -1, -1, -1, -1, -1, /* 88-8f */
50
+ -1, -1, -1, -1, -1, -1, -1, -1, /* 90-97 */
51
+ -1, -1, -1, -1, -1, -1, -1, -1, /* 98-9f */
52
+ -1, -1, -1, -1, -1, -1, -1, -1, /* a0-a7 */
53
+ -1, -1, -1, -1, -1, -1, -1, -1, /* a8-af */
54
+ -1, -1, -1, -1, -1, -1, -1, -1, /* b0-b7 */
55
+ -1, -1, -1, -1, -1, -1, -1, -1, /* b8-bf */
56
+ -1, -1, -1, -1, -1, -1, -1, -1, /* c0-c7 */
57
+ -1, -1, -1, -1, -1, -1, -1, -1, /* c8-cf */
58
+ -1, -1, -1, -1, -1, -1, -1, -1, /* d0-d7 */
59
+ -1, -1, -1, -1, -1, -1, -1, -1, /* d8-df */
60
+ -1, -1, -1, -1, -1, -1, -1, -1, /* e0-e7 */
61
+ -1, -1, -1, -1, -1, -1, -1, -1, /* e8-ef */
62
+ -1, -1, -1, -1, -1, -1, -1, -1, /* f0-f7 */
63
+ -1, -1, -1, -1, -1, -1, -1, -1, /* f8-ff */
64
+ };
65
+ return val[c];
66
}
67
68
int get_sha1_hex(const char *hex, unsigned char *sha1)