Skip to content

Commit c676253

Browse files
committed
Switch translate() to the header file
This allows the compile stage to optimize most of the translate() function away and saves a ton of space (~40k on ESP). *However*, it requires us to wait for the qstr output before we compile the rest of our .o files. (Only qstr.o used to wait.) This isn't as good as the current setup with LTO though. Trinket M0 loses <1k with this setup. So, we should probably conditionalize this along with LTO.
1 parent dc5565a commit c676253

File tree

4 files changed

+21
-22
lines changed

4 files changed

+21
-22
lines changed

ports/espressif/boards/adafruit_feather_esp32s3_tft/mpconfigboard.mk

-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ USB_MANUFACTURER = "Adafruit"
66

77
IDF_TARGET = esp32s3
88

9-
# Make room for build
10-
CIRCUITPY_ULAB = 0
11-
129
INTERNAL_FLASH_FILESYSTEM = 1
1310
LONGINT_IMPL = MPZ
1411

py/mkrules.mk

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ $(Q)$(CXX) $(CXXFLAGS) -c -MD -o $@ $<
5858
endef
5959

6060
vpath %.c . $(TOP) $(USER_C_MODULES) $(DEVICES_MODULES)
61-
$(BUILD)/%.o: %.c
61+
$(BUILD)/%.o: %.c | $(HEADER_BUILD)/qstrdefs.generated.h $(HEADER_BUILD)/qstrdefs.enum.h
6262
$(call compile_c)
6363

6464
vpath %.cpp . $(TOP) $(USER_C_MODULES)
@@ -89,7 +89,7 @@ $(BUILD)/%.pp: %.c
8989
# the right .o's to get recompiled if the generated.h file changes. Adding
9090
# an order-only dependency to all of the .o's will cause the generated .h
9191
# to get built before we try to compile any of them.
92-
$(OBJ): | $(HEADER_BUILD)/qstrdefs.enum.h $(HEADER_BUILD)/mpversion.h
92+
$(OBJ): | $(HEADER_BUILD)/mpversion.h
9393

9494
# The logic for qstr regeneration (applied by makeqstrdefs.py) is:
9595
# - if anything in QSTR_GLOBAL_DEPENDENCIES is newer, then process all source files ($^)

supervisor/shared/translate.c

-16
Original file line numberDiff line numberDiff line change
@@ -134,19 +134,3 @@ char *decompress(const compressed_string_t *compressed, char *decompressed) {
134134
decompressed[length - 1] = '\0';
135135
return decompressed;
136136
}
137-
138-
inline
139-
// gcc10 -flto has issues with this being always_inline for debug builds.
140-
#if CIRCUITPY_DEBUG < 1
141-
__attribute__((always_inline))
142-
#endif
143-
const compressed_string_t *translate(const char *original) {
144-
#ifndef NO_QSTR
145-
#define QDEF(id, hash, len, str)
146-
#define TRANSLATION(id, firstbyte, ...) if (strcmp(original, id) == 0) { static const compressed_string_t v = { .data = firstbyte, .tail = { __VA_ARGS__ } }; return &v; } else
147-
#include "genhdr/qstrdefs.generated.h"
148-
#undef TRANSLATION
149-
#undef QDEF
150-
#endif
151-
return NULL;
152-
}

supervisor/shared/translate.h

+19-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
#ifndef MICROPY_INCLUDED_SUPERVISOR_TRANSLATE_H
2828
#define MICROPY_INCLUDED_SUPERVISOR_TRANSLATE_H
2929

30+
#include <stddef.h>
3031
#include <stdint.h>
32+
#include <string.h>
3133

3234
// The format of the compressed data is:
3335
// - the size of the uncompressed string in UTF-8 bytes, encoded as a
@@ -77,7 +79,7 @@ typedef struct compressed_string {
7779
// Return the compressed, translated version of a source string
7880
// Usually, due to LTO, this is optimized into a load of a constant
7981
// pointer.
80-
const compressed_string_t *translate(const char *c);
82+
// const compressed_string_t *translate(const char *c);
8183
void serial_write_compressed(const compressed_string_t *compressed);
8284
char *decompress(const compressed_string_t *compressed, char *decompressed);
8385
uint16_t decompress_length(const compressed_string_t *compressed);
@@ -90,4 +92,20 @@ uint16_t decompress_length(const compressed_string_t *compressed);
9092
#define MP_ERROR_TEXT(x) translate(x)
9193
#endif
9294

95+
static inline
96+
// gcc10 -flto has issues with this being always_inline for debug builds.
97+
#if CIRCUITPY_DEBUG < 1
98+
__attribute__((always_inline))
99+
#endif
100+
const compressed_string_t *translate(const char *original) {
101+
#ifndef NO_QSTR
102+
#define QDEF(id, hash, len, str)
103+
#define TRANSLATION(id, firstbyte, ...) if (strcmp(original, id) == 0) { static const compressed_string_t v = { .data = firstbyte, .tail = { __VA_ARGS__ } }; return &v; } else
104+
#include "genhdr/qstrdefs.generated.h"
105+
#undef TRANSLATION
106+
#undef QDEF
107+
#endif
108+
return NULL;
109+
}
110+
93111
#endif // MICROPY_INCLUDED_SUPERVISOR_TRANSLATE_H

0 commit comments

Comments
 (0)