X Tutup
Skip to content

Commit 7e833f3

Browse files
committed
basic: massively reduce the size of arphdr lookup functions
Our biggest object in libsystemd was a table full of zeros, for the arphdr names. Let's use a switch (which gcc nicely optimizes for us), instead a table with a gap between 826 and 65534: $ ls -l build{,2}/src/basic/a6ba3eb@@basic@sta/arphrd-list.c.o -rw-rw-r--. 1 zbyszek zbyszek 540232 Sep 22 00:29 build/src/basic/a6ba3eb\@\@basic\@sta/arphrd-list.c.o -rw-rw-r--. 1 zbyszek zbyszek 20512 Sep 25 11:56 build2/src/basic/a6ba3eb\@\@basic\@sta/arphrd-list.c.o $ ls -l build{,2}/src/shared/libsystemd-shared-243.so -rwxrwxr-x. 1 zbyszek zbyszek 6774368 Sep 22 00:29 build/src/shared/libsystemd-shared-243.so -rwxrwxr-x. 1 zbyszek zbyszek 6254808 Sep 25 12:16 build2/src/shared/libsystemd-shared-243.so No functional change.
1 parent 5a2904a commit 7e833f3

File tree

4 files changed

+16
-34
lines changed

4 files changed

+16
-34
lines changed

src/basic/arphrd-list.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,6 @@ static const struct arphrd_name* lookup_arphrd(register const char *str, registe
1212
#include "arphrd-from-name.h"
1313
#include "arphrd-to-name.h"
1414

15-
const char *arphrd_to_name(int id) {
16-
17-
if (id <= 0)
18-
return NULL;
19-
20-
if ((size_t) id >= ELEMENTSOF(arphrd_names))
21-
return NULL;
22-
23-
return arphrd_names[id];
24-
}
25-
2615
int arphrd_from_name(const char *name) {
2716
const struct arphrd_name *sc;
2817

@@ -34,7 +23,3 @@ int arphrd_from_name(const char *name) {
3423

3524
return sc->id;
3625
}
37-
38-
int arphrd_max(void) {
39-
return ELEMENTSOF(arphrd_names);
40-
}

src/basic/arphrd-list.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,3 @@
33

44
const char *arphrd_to_name(int id);
55
int arphrd_from_name(const char *name);
6-
7-
int arphrd_max(void);

src/basic/arphrd-to-name.awk

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
BEGIN{
2-
print "static const char* const arphrd_names[] = { "
2+
print "const char *arphrd_to_name(int id) {"
3+
print " switch(id) {"
34
}
4-
!/CISCO/ {
5-
printf " [ARPHRD_%s] = \"%s\",\n", $1, $1
5+
!/CISCO|NETROM/ {
6+
printf " case ARPHRD_%s: return \"%s\";\n", $1, $1
67
}
78
END{
8-
print "};"
9+
print " default: return NULL;"
10+
print " }"
11+
print "}"
912
}

src/test/test-arphrd-list.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
11
/* SPDX-License-Identifier: LGPL-2.1+ */
22

33
#include <linux/if_arp.h>
4-
#include <string.h>
54

6-
#include "macro.h"
75
#include "string-util.h"
86
#include "tests.h"
97

10-
_unused_ \
11-
static const struct arphrd_name* lookup_arphrd(register const char *str, register GPERF_LEN_TYPE len);
12-
13-
#include "arphrd-from-name.h"
148
#include "arphrd-list.h"
15-
#include "arphrd-to-name.h"
169

1710
int main(int argc, const char *argv[]) {
1811
test_setup_logging(LOG_INFO);
1912

20-
for (unsigned i = 1; i < ELEMENTSOF(arphrd_names); i++)
21-
if (arphrd_names[i]) {
22-
log_info("%i: %s", i, arphrd_to_name(i));
13+
for (int i = 0; i <= ARPHRD_VOID + 1; i++) {
14+
const char *name;
15+
16+
name = arphrd_to_name(i);
17+
if (name) {
18+
log_info("%i: %s", i, name);
2319

24-
assert_se(streq(arphrd_to_name(i), arphrd_names[i]));
25-
assert_se(arphrd_from_name(arphrd_names[i]) == (int) i);
20+
assert_se(arphrd_from_name(name) == i);
2621
}
22+
}
2723

28-
assert_se(arphrd_to_name(arphrd_max()) == NULL);
24+
assert_se(arphrd_to_name(ARPHRD_VOID + 1) == NULL);
2925
assert_se(arphrd_to_name(0) == NULL);
3026
assert_se(arphrd_from_name("huddlduddl") == -EINVAL);
3127
assert_se(arphrd_from_name("") == -EINVAL);

0 commit comments

Comments
 (0)
X Tutup