Skip to content

Commit 17f324b

Browse files
committed
py/frozenmod: Store frozen module names together, to quickly scan them.
1 parent 1b0aab6 commit 17f324b

2 files changed

Lines changed: 18 additions & 13 deletions

File tree

py/frozenmod.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,22 @@
3232

3333
#if MICROPY_MODULE_FROZEN
3434

35-
extern const uint16_t mp_frozen_sizes[];
35+
extern const char mp_frozen_names[];
36+
extern const uint32_t mp_frozen_sizes[];
3637
extern const char mp_frozen_content[];
3738

3839
mp_lexer_t *mp_find_frozen_module(const char *str, int len) {
39-
const uint16_t *sz_ptr = mp_frozen_sizes;
40-
const char *s = mp_frozen_content;
40+
const char *name = mp_frozen_names;
4141

42-
while (*sz_ptr) {
43-
int l = strlen(s);
44-
if (l == len && !memcmp(str, s, l)) {
45-
s += l + 1;
46-
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR_, s, *sz_ptr, 0);
42+
size_t offset = 0;
43+
for (int i = 0; *name != 0; i++) {
44+
int l = strlen(name);
45+
if (l == len && !memcmp(str, name, l)) {
46+
mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR_, mp_frozen_content + offset, mp_frozen_sizes[i], 0);
4747
return lex;
4848
}
49-
s += (l + 1) + (*sz_ptr++ + 1);
49+
name += l + 1;
50+
offset += mp_frozen_sizes[i] + 1;
5051
}
5152
return NULL;
5253
}

tools/make-frozen.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,21 @@ def module_name(f):
3737
modules.append((fullpath[root_len + 1:], st))
3838

3939
print("#include <stdint.h>")
40-
print("const uint16_t mp_frozen_sizes[] = {")
40+
print("const char mp_frozen_names[] = {")
41+
for f, st in modules:
42+
m = module_name(f)
43+
print('"%s\\0"' % m)
44+
print('"\\0"};')
45+
46+
print("const uint32_t mp_frozen_sizes[] = {")
4147

4248
for f, st in modules:
4349
print("%d," % st.st_size)
4450

45-
print("0};")
51+
print("};")
4652

4753
print("const char mp_frozen_content[] = {")
4854
for f, st in modules:
49-
m = module_name(f)
50-
print('"%s\\0"' % m)
5155
data = open(sys.argv[1] + "/" + f, "rb").read()
5256
# Python2 vs Python3 tricks
5357
data = repr(data)

0 commit comments

Comments
 (0)