Skip to content

Commit c4e2f4e

Browse files
committed
print.h: replace else..if with switch..case and use fput* functions
* Replace all else..if branches with switch..case. A optimizing compiler might be able to create a lookup table instead relying on the branches. * Replace fprintf() with fputc() and fputs(), for strings that do not require formatting. C compilers such as TCC (that doesn't replace fprintf() calls, for (non-formatting) strings with fput* functions) can make use of this. This shall increase the performance a bit or so when compiled with TCC.
1 parent 21a9959 commit c4e2f4e

File tree

1 file changed

+50
-31
lines changed

1 file changed

+50
-31
lines changed

print.h

+50-31
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ int __print_enable_color = 1;
55

66
void __print_color(FILE* fd, int a) {
77
if (!__print_enable_color) return;
8-
if (a == -1) fprintf(fd, "\x1b(B\x1b[m");
8+
if (a == -1) fputs("\x1b(B\x1b[m", fd);
99
else fprintf(fd, "\x1b[38;5;%im", a);
1010
}
1111

@@ -14,15 +14,15 @@ void __print_color(FILE* fd, int a) {
1414
int max_len = 16; \
1515
int n = size/sizeof(T); \
1616
T *m = va_arg(v, T*); \
17-
fprintf(fd, "["); \
17+
fputc('[', fd); \
1818
__print_color(fd, color); \
1919
for (int i = 0; i < (n < max_len ? n : max_len); i++) { \
20-
if (i > 0) fprintf(fd, " "); \
20+
if (i > 0) fputc(' ', fd); \
2121
fprintf(fd, qual, m[i]); \
2222
} \
2323
__print_color(fd, __print_color_normal); \
24-
if (n > max_len) fprintf(fd, "..."); \
25-
fprintf(fd, "]");
24+
if (n > max_len) fputs("...", fd); \
25+
fputc(']', fd);
2626

2727
int __print_color_normal = -1; // -1 means default terminal foreground color
2828
int __print_color_number = 4;
@@ -46,88 +46,107 @@ void __print_func (FILE *fd, int count, unsigned short types[], ...) {
4646
for (int i = 0; i < count; i++) {
4747
char type = types[i] & 0x1F;
4848
char size = types[i] >> 5;
49-
if (i > 0) fprintf(fd, " ");
49+
if (i > 0) fputc(' ', fd);
5050
fprintf(fd, "%i[%i]", type, size);
5151
}
52-
fprintf(fd, "\n");
52+
fputc('\n', fd);
5353
#endif // __print_DEBUG
5454

55+
double d;
56+
char c;
5557
for (int i = 0; i < count; i++) {
56-
if (i > 0) fprintf(fd, " ");
58+
if (i > 0) fputc(' ', fd);
5759
char type = types[i] & 0x1F;
5860
char size = types[i] >> 5;
59-
if (type == 1) {
61+
switch (type) {
62+
case 1: {
6063
__print_color(fd, __print_color_float);
61-
double d = va_arg(v, double);
64+
d = va_arg(v, double);
6265
fprintf(fd, "%'G", d);
66+
break;
6367
}
64-
else if (type == 2) {
68+
case 2: {
6569
__print_color(fd, __print_color_string);
66-
char c = va_arg(v, int);
70+
c = va_arg(v, int);
6771
fprintf(fd, "'%c'", c); __print_color(fd, __print_color_number);
6872
fprintf(fd, "%i", (int)c);
73+
break;
6974
}
70-
else if (type == 3) {
75+
case 3: {
7176
__print_color(fd, __print_color_number);
72-
char c = va_arg(v, int);
77+
c = va_arg(v, int);
7378
fprintf(fd, "%i", (unsigned char)c);
7479
__print_color(fd, __print_color_normal);
75-
fprintf(fd, "<");
80+
fputc('<', fd);
7681
__print_color(fd, __print_color_hex);
7782
fprintf(fd, "0x%X", (unsigned char)c);
7883
__print_color(fd, __print_color_normal);
79-
fprintf(fd, ">");
84+
fputc('>', fd);
85+
break;
8086
}
81-
else if (type == 4) {
87+
case 4: {
8288
__print_color(fd, __print_color_number);
8389
fprintf(fd, "%'i", va_arg(v, int));
90+
break;
8491
}
85-
else if (type == 5) {
92+
case 5: {
8693
__print_color(fd, __print_color_number);
8794
fprintf(fd, "%'u", va_arg(v, int));
95+
break;
8896
}
89-
else if (type == 6) {
97+
case 6: {
9098
__print_color(fd, __print_color_number);
9199
fprintf(fd, "%'li", va_arg(v, unsigned long));
100+
break;
92101
}
93-
else if (type == 7) {
102+
case 7: {
94103
__print_color(fd, __print_color_number);
95104
fprintf(fd, "%'lu", va_arg(v, long));
105+
break;
96106
}
97-
else if (type == 8) {
107+
case 8: {
98108
__print_color(fd, __print_color_string);
99109
fprintf(fd, "\"%s\"", va_arg(v, char*));
110+
break;
100111
}
101-
else if (type == 9) {
112+
case 9: {
102113
__print_color(fd, __print_color_normal);
103114
fprintf(fd, "%s", va_arg(v, char*));
115+
break;
104116
}
105-
else if (type == 10) {
117+
case 10: {
106118
__print_color(fd, __print_color_hex);
107119
fprintf(fd, "%p", va_arg(v, void*));
120+
break;
108121
}
109-
else if (type == 11) {
122+
case 11: {
110123
__print_array(fd, int, "%i", __print_color_number);
124+
break;
111125
}
112-
else if (type == 12) {
126+
case 12: {
113127
__print_array(fd, unsigned int, "%u", __print_color_number);
128+
break;
114129
}
115-
else if (type == 13) {
130+
case 13: {
116131
__print_array(fd, short, "%i", __print_color_number);
132+
break;
117133
}
118-
else if (type == 14) {
134+
case 14: {
119135
__print_array(fd, unsigned short, "%i", __print_color_number);
136+
break;
120137
}
121-
else if (type == 15) {
138+
case 15: {
122139
__print_array(fd, char*, "\"%s\"", __print_color_string);
140+
break;
123141
}
124-
else {
125-
fprintf(fd, "print: unsupported type (of size %i)\n", size); break;
142+
default:
143+
fprintf(fd, "print: unsupported type (of size %i)\n", size);
144+
break;
126145
}
127146
}
128147
va_end(v);
129148
__print_color(fd, -1);
130-
fprintf(fd, "\n");
149+
fputc('\n', fd);
131150
}
132151

133152
#define __print_typeid(a) \

0 commit comments

Comments
 (0)