Skip to content

Commit dee81e9

Browse files
Alexey DobriyanMichal Marek
authored andcommitted
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: Alexey Dobriyan <adobriyan@gmail.com> Signed-off-by: Michal Marek <mmarek@suse.com>
1 parent e007c53 commit dee81e9

1 file changed

Lines changed: 28 additions & 58 deletions

File tree

scripts/basic/fixdep.c

Lines changed: 28 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@
8282
* to date before even starting the recursive build, so it's too late
8383
* at this point anyway.
8484
*
85-
* The algorithm to grep for "CONFIG_..." is bit unusual, but should
86-
* be fast ;-) We don't even try to really parse the header files, but
85+
* We don't even try to really parse the header files, but
8786
* merely grep, i.e. if CONFIG_FOO is mentioned in a comment, it will
8887
* be picked up as well. It's not a problem with respect to
8988
* correctness, since that can only give too many dependencies, thus
@@ -115,11 +114,6 @@
115114
#include <ctype.h>
116115
#include <arpa/inet.h>
117116

118-
#define INT_CONF ntohl(0x434f4e46)
119-
#define INT_ONFI ntohl(0x4f4e4649)
120-
#define INT_NFIG ntohl(0x4e464947)
121-
#define INT_FIG_ ntohl(0x4649475f)
122-
123117
int insert_extra_deps;
124118
char *target;
125119
char *depfile;
@@ -241,37 +235,22 @@ static void use_config(const char *m, int slen)
241235
print_config(m, slen);
242236
}
243237

244-
static void parse_config_file(const char *map, size_t len)
238+
static void parse_config_file(const char *p)
245239
{
246-
const int *end = (const int *) (map + len);
247-
/* start at +1, so that p can never be < map */
248-
const int *m = (const int *) map + 1;
249-
const char *p, *q;
250-
251-
for (; m < end; m++) {
252-
if (*m == INT_CONF) { p = (char *) m ; goto conf; }
253-
if (*m == INT_ONFI) { p = (char *) m-1; goto conf; }
254-
if (*m == INT_NFIG) { p = (char *) m-2; goto conf; }
255-
if (*m == INT_FIG_) { p = (char *) m-3; goto conf; }
256-
continue;
257-
conf:
258-
if (p > map + len - 7)
259-
continue;
260-
if (memcmp(p, "CONFIG_", 7))
261-
continue;
240+
const char *q, *r;
241+
242+
while ((p = strstr(p, "CONFIG_"))) {
262243
p += 7;
263-
for (q = p; q < map + len; q++) {
264-
if (!(isalnum(*q) || *q == '_'))
265-
goto found;
266-
}
267-
continue;
268-
269-
found:
270-
if (!memcmp(q - 7, "_MODULE", 7))
271-
q -= 7;
272-
if (q - p < 0)
273-
continue;
274-
use_config(p, q - p);
244+
q = p;
245+
while (*q && (isalnum(*q) || *q == '_'))
246+
q++;
247+
if (memcmp(q - 7, "_MODULE", 7) == 0)
248+
r = q - 7;
249+
else
250+
r = q;
251+
if (r > p)
252+
use_config(p, r - p);
253+
p = q;
275254
}
276255
}
277256

@@ -291,7 +270,7 @@ static void do_config_file(const char *filename)
291270
{
292271
struct stat st;
293272
int fd;
294-
void *map;
273+
char *map;
295274

296275
fd = open(filename, O_RDONLY);
297276
if (fd < 0) {
@@ -308,18 +287,23 @@ static void do_config_file(const char *filename)
308287
close(fd);
309288
return;
310289
}
311-
map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
312-
if ((long) map == -1) {
313-
perror("fixdep: mmap");
290+
map = malloc(st.st_size + 1);
291+
if (!map) {
292+
perror("fixdep: malloc");
314293
close(fd);
315294
return;
316295
}
296+
if (read(fd, map, st.st_size) != st.st_size) {
297+
perror("fixdep: read");
298+
close(fd);
299+
return;
300+
}
301+
map[st.st_size] = '\0';
302+
close(fd);
317303

318-
parse_config_file(map, st.st_size);
319-
320-
munmap(map, st.st_size);
304+
parse_config_file(map);
321305

322-
close(fd);
306+
free(map);
323307
}
324308

325309
/*
@@ -446,22 +430,8 @@ static void print_deps(void)
446430
close(fd);
447431
}
448432

449-
static void traps(void)
450-
{
451-
static char test[] __attribute__((aligned(sizeof(int)))) = "CONF";
452-
int *p = (int *)test;
453-
454-
if (*p != INT_CONF) {
455-
fprintf(stderr, "fixdep: sizeof(int) != 4 or wrong endianness? %#x\n",
456-
*p);
457-
exit(2);
458-
}
459-
}
460-
461433
int main(int argc, char *argv[])
462434
{
463-
traps();
464-
465435
if (argc == 5 && !strcmp(argv[1], "-e")) {
466436
insert_extra_deps = 1;
467437
argv++;

0 commit comments

Comments
 (0)