Commit dee81e98 authored by Alexey Dobriyan's avatar Alexey Dobriyan Committed by Michal Marek

fixdep: faster CONFIG_ search

Do you think kernel build is 100% dominated by gcc? You are wrong!
One small utility called "fixdep" consistently manages to sneak into
profile's first page (unless you have small monitor of course).

The choke point is this clever code:

	for (; m < end; m++) {
		if (*m == INT_CONF) { p = (char *) m  ; goto conf; }
		if (*m == INT_ONFI) { p = (char *) m-1; goto conf; }
		if (*m == INT_NFIG) { p = (char *) m-2; goto conf; }
		if (*m == INT_FIG_) { p = (char *) m-3; goto conf; }

4 branches per 4 characters is not fast.

Use strstr(3), so that SSE2 etc can be used.

With this patch, fixdep is so deep at the bottom, it is hard to find it.
Signed-off-by: default avatarAlexey Dobriyan <adobriyan@gmail.com>
Signed-off-by: default avatarMichal Marek <mmarek@suse.com>
parent e007c533
...@@ -82,8 +82,7 @@ ...@@ -82,8 +82,7 @@
* to date before even starting the recursive build, so it's too late * to date before even starting the recursive build, so it's too late
* at this point anyway. * at this point anyway.
* *
* The algorithm to grep for "CONFIG_..." is bit unusual, but should * We don't even try to really parse the header files, but
* be fast ;-) We don't even try to really parse the header files, but
* merely grep, i.e. if CONFIG_FOO is mentioned in a comment, it will * merely grep, i.e. if CONFIG_FOO is mentioned in a comment, it will
* be picked up as well. It's not a problem with respect to * be picked up as well. It's not a problem with respect to
* correctness, since that can only give too many dependencies, thus * correctness, since that can only give too many dependencies, thus
...@@ -115,11 +114,6 @@ ...@@ -115,11 +114,6 @@
#include <ctype.h> #include <ctype.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#define INT_CONF ntohl(0x434f4e46)
#define INT_ONFI ntohl(0x4f4e4649)
#define INT_NFIG ntohl(0x4e464947)
#define INT_FIG_ ntohl(0x4649475f)
int insert_extra_deps; int insert_extra_deps;
char *target; char *target;
char *depfile; char *depfile;
...@@ -241,37 +235,22 @@ static void use_config(const char *m, int slen) ...@@ -241,37 +235,22 @@ static void use_config(const char *m, int slen)
print_config(m, slen); print_config(m, slen);
} }
static void parse_config_file(const char *map, size_t len) static void parse_config_file(const char *p)
{ {
const int *end = (const int *) (map + len); const char *q, *r;
/* start at +1, so that p can never be < map */
const int *m = (const int *) map + 1; while ((p = strstr(p, "CONFIG_"))) {
const char *p, *q;
for (; m < end; m++) {
if (*m == INT_CONF) { p = (char *) m ; goto conf; }
if (*m == INT_ONFI) { p = (char *) m-1; goto conf; }
if (*m == INT_NFIG) { p = (char *) m-2; goto conf; }
if (*m == INT_FIG_) { p = (char *) m-3; goto conf; }
continue;
conf:
if (p > map + len - 7)
continue;
if (memcmp(p, "CONFIG_", 7))
continue;
p += 7; p += 7;
for (q = p; q < map + len; q++) { q = p;
if (!(isalnum(*q) || *q == '_')) while (*q && (isalnum(*q) || *q == '_'))
goto found; q++;
} if (memcmp(q - 7, "_MODULE", 7) == 0)
continue; r = q - 7;
else
found: r = q;
if (!memcmp(q - 7, "_MODULE", 7)) if (r > p)
q -= 7; use_config(p, r - p);
if (q - p < 0) p = q;
continue;
use_config(p, q - p);
} }
} }
...@@ -291,7 +270,7 @@ static void do_config_file(const char *filename) ...@@ -291,7 +270,7 @@ static void do_config_file(const char *filename)
{ {
struct stat st; struct stat st;
int fd; int fd;
void *map; char *map;
fd = open(filename, O_RDONLY); fd = open(filename, O_RDONLY);
if (fd < 0) { if (fd < 0) {
...@@ -308,18 +287,23 @@ static void do_config_file(const char *filename) ...@@ -308,18 +287,23 @@ static void do_config_file(const char *filename)
close(fd); close(fd);
return; return;
} }
map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); map = malloc(st.st_size + 1);
if ((long) map == -1) { if (!map) {
perror("fixdep: mmap"); perror("fixdep: malloc");
close(fd); close(fd);
return; return;
} }
if (read(fd, map, st.st_size) != st.st_size) {
perror("fixdep: read");
close(fd);
return;
}
map[st.st_size] = '\0';
close(fd);
parse_config_file(map, st.st_size); parse_config_file(map);
munmap(map, st.st_size);
close(fd); free(map);
} }
/* /*
...@@ -446,22 +430,8 @@ static void print_deps(void) ...@@ -446,22 +430,8 @@ static void print_deps(void)
close(fd); close(fd);
} }
static void traps(void)
{
static char test[] __attribute__((aligned(sizeof(int)))) = "CONF";
int *p = (int *)test;
if (*p != INT_CONF) {
fprintf(stderr, "fixdep: sizeof(int) != 4 or wrong endianness? %#x\n",
*p);
exit(2);
}
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
traps();
if (argc == 5 && !strcmp(argv[1], "-e")) { if (argc == 5 && !strcmp(argv[1], "-e")) {
insert_extra_deps = 1; insert_extra_deps = 1;
argv++; argv++;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment