Skip to content

Commit c0ca4bb

Browse files
committed
webassembly: Set GC threshold and do top-level GC collect when possible.
Signed-off-by: Damien George <damien@micropython.org>
1 parent cdaf2de commit c0ca4bb

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

ports/webassembly/main.c

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,17 @@
4949
// the top-level call into C.
5050
static size_t external_call_depth = 0;
5151

52+
#if MICROPY_GC_SPLIT_HEAP_AUTO
53+
static void gc_collect_top_level(void);
54+
#endif
55+
5256
void external_call_depth_inc(void) {
5357
++external_call_depth;
58+
#if MICROPY_GC_SPLIT_HEAP_AUTO
59+
if (external_call_depth == 1) {
60+
gc_collect_top_level();
61+
}
62+
#endif
5463
}
5564

5665
void external_call_depth_dec(void) {
@@ -63,6 +72,14 @@ void mp_js_init(int heap_size) {
6372
gc_init(heap, heap + heap_size);
6473
#endif
6574

75+
#if MICROPY_GC_SPLIT_HEAP_AUTO
76+
// When MICROPY_GC_SPLIT_HEAP_AUTO is enabled, set the GC threshold to a low
77+
// value so that a collection is triggered before the heap fills up. The actual
78+
// garbage collection will happen later when control returns to the top-level,
79+
// via the `gc_collect_pending` flag and `gc_collect_top_level()`.
80+
MP_STATE_MEM(gc_alloc_threshold) = 16 * 1024 / MICROPY_BYTES_PER_GC_BLOCK;
81+
#endif
82+
6683
#if MICROPY_ENABLE_PYSTACK
6784
static mp_obj_t pystack[1024];
6885
mp_pystack_init(pystack, &pystack[MP_ARRAY_SIZE(pystack)]);
@@ -121,10 +138,6 @@ void mp_js_do_import(const char *name, uint32_t *out) {
121138
}
122139

123140
void mp_js_do_exec(const char *src, size_t len, uint32_t *out) {
124-
// Collect at the top-level, where there are no root pointers from stack/registers.
125-
gc_collect_start();
126-
gc_collect_end();
127-
128141
external_call_depth_inc();
129142
mp_parse_input_kind_t input_kind = MP_PARSE_FILE_INPUT;
130143
nlr_buf_t nlr;
@@ -163,13 +176,25 @@ int mp_js_repl_process_char(int c) {
163176

164177
#if MICROPY_GC_SPLIT_HEAP_AUTO
165178

179+
static bool gc_collect_pending = false;
180+
166181
// The largest new region that is available to become Python heap.
167182
size_t gc_get_max_new_split(void) {
168183
return 128 * 1024 * 1024;
169184
}
170185

171186
// Don't collect anything. Instead require the heap to grow.
172187
void gc_collect(void) {
188+
gc_collect_pending = true;
189+
}
190+
191+
// Collect at the top-level, where there are no root pointers from stack/registers.
192+
static void gc_collect_top_level(void) {
193+
if (gc_collect_pending) {
194+
gc_collect_pending = false;
195+
gc_collect_start();
196+
gc_collect_end();
197+
}
173198
}
174199

175200
#else

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy