From 8ea2e11c7e7d656d323c63d696c6b8e93f7f6fde Mon Sep 17 00:00:00 2001 From: theMackabu Date: Mon, 9 Feb 2026 20:58:03 -0800 Subject: [PATCH] optimize tokenization cache for function bodies --- src/ant.c | 20 ++++++------------- tests/mandelbrot_bellard.cjs | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 14 deletions(-) create mode 100644 tests/mandelbrot_bellard.cjs diff --git a/src/ant.c b/src/ant.c index 4aece1c..86be2e6 100644 --- a/src/ant.c +++ b/src/ant.c @@ -6679,7 +6679,7 @@ static bool code_uses_arguments(const char *code, jsoff_t len) { } return false; } -static parsed_func_t *get_or_parse_func(const char *fn, jsoff_t fnlen) { +static parsed_func_t *get_or_parse_func(struct js *js, const char *fn, jsoff_t fnlen) { uint64_t h = hash_key(fn, fnlen); parsed_func_t *cached = NULL; HASH_FIND(hh, func_parse_cache, &h, sizeof(h), cached); @@ -6774,7 +6774,7 @@ static parsed_func_t *get_or_parse_func(const char *fn, jsoff_t fnlen) { pf->is_expr = !is_block; pf->is_strict = is_strict_function_body(&fn[fnpos], pf->body_len); pf->uses_arguments = code_uses_arguments(&fn[pf->body_start], pf->body_len); - pf->tokens = NULL; + pf->tokens = (pf->body_len > 0) ? tokenize_body(js, &fn[pf->body_start], pf->body_len) : NULL; HASH_ADD(hh, func_parse_cache, code_hash, sizeof(pf->code_hash), pf); return pf; @@ -6905,7 +6905,7 @@ jsval_t call_js_internal( js->pos = caller_pos; js->scope = function_scope; - parsed_func_t *pf = get_or_parse_func(fn, fnlen); + parsed_func_t *pf = get_or_parse_func(js, fn, fnlen); if (!pf) { if (args_on_heap) free(args); restore_saved_scope(js); @@ -6992,11 +6992,7 @@ jsval_t call_js_internal( int saved_token_stream_pos = js->token_stream_pos; const char *saved_token_stream_code = js->token_stream_code; - if (!pf->is_expr && !pf->tokens && pf->body_len > 0) { - pf->tokens = tokenize_body(js, &fn[pf->body_start], pf->body_len); - } - - if (pf->tokens && !pf->is_expr) { + if (pf->tokens) { js->token_stream = pf->tokens; js->token_stream_pos = 0; js->token_stream_code = &fn[pf->body_start]; @@ -23093,7 +23089,7 @@ static jsval_t js_call_internal(struct js *js, jsval_t func, jsval_t bound_this, utarray_push_back(global_scope_stack, &function_scope_offset); hoist_var_declarations_from_slot(js, js->scope, func_obj); - parsed_func_t *pf = get_or_parse_func(fn, fnlen); + parsed_func_t *pf = get_or_parse_func(js, fn, fnlen); if (!pf) { if (global_scope_stack && utarray_len(global_scope_stack) > 0) utarray_pop_back(global_scope_stack); delscope(js); js->scope = saved_scope; @@ -23160,11 +23156,7 @@ static jsval_t js_call_internal(struct js *js, jsval_t func, jsval_t bound_this, int saved_token_stream_pos = js->token_stream_pos; const char *saved_token_stream_code = js->token_stream_code; - if (!pf->is_expr && !pf->tokens && pf->body_len > 0) { - pf->tokens = tokenize_body(js, &fn[pf->body_start], pf->body_len); - } - - if (pf->tokens && !pf->is_expr) { + if (pf->tokens) { js->token_stream = pf->tokens; js->token_stream_pos = 0; js->token_stream_code = &fn[pf->body_start]; diff --git a/tests/mandelbrot_bellard.cjs b/tests/mandelbrot_bellard.cjs new file mode 100644 index 0000000..b8b6fae --- /dev/null +++ b/tests/mandelbrot_bellard.cjs @@ -0,0 +1,37 @@ +/* Mandelbrot display on a color terminal + (c) 2025 Fabrice Bellard + MIT license +*/ +function mandelbrot(center_x, center_y, scale, w, h, max_it) { + var x1, y1, y2, i, x, y, cx, cy, fx, fy, i, t, c, s, c0; + var colors = [14, 15, 7, 8, 0, 4, 12, 5, 13, 1, 9, 3, 11, 10, 2, 6]; + fx = (scale * 0.5) / Math.min(w, h); + fy = fx * 2; + for (y1 = 0; y1 < h; y1++) { + s = ''; + for (x1 = 0; x1 < w; x1++) { + for (y2 = 0; y2 < 2; y2++) { + cx = (x1 - w * 0.5) * fx + center_x; + cy = (y1 + y2 * 0.5 - h * 0.5) * fy + center_y; + x = 0; + y = 0; + for (i = 0; i < max_it && x * x + y * y < 4; i++) { + t = x * x - y * y + cx; + y = 2 * x * y + cy; + x = t; + } + if (i >= max_it) { + c = 0; + } else { + c = colors[i % colors.length]; + } + if (y2 == 0) c0 = c; + } + s += '\x1b[' + (c0 >= 8 ? 82 + c0 : 30 + c0) + ';' + (c >= 8 ? 92 + c : 40 + c) + 'm\u2580'; + } + s += '\x1b[0m'; /* reset the colors */ + console.log(s); + } +} + +mandelbrot(-0.75, 0.0, 2.0, 80, 25, 50); -- 2.51.2