From ba7bbad57bd98fa16b0027b0fecdba01e69e602e Mon Sep 17 00:00:00 2001 From: Fabrice Bellard Date: Fri, 4 Apr 2025 09:49:30 +0200 Subject: [PATCH 1/2] fixed hash_map_resize() - added Map/WeakMap in microbench saghul: Also removed slack since it's unused. --- quickjs.c | 6 ++-- tests/microbench.js | 69 +++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 66 insertions(+), 9 deletions(-) diff --git a/quickjs.c b/quickjs.c index 3e8a1a9cc..30d25bc58 100644 --- a/quickjs.c +++ b/quickjs.c @@ -48645,7 +48645,6 @@ static JSMapRecord *map_find_record(JSContext *ctx, JSMapState *s, static void map_hash_resize(JSContext *ctx, JSMapState *s) { uint32_t new_hash_size, i, h; - size_t slack; struct list_head *new_hash_table, *el; JSMapRecord *mr; @@ -48654,11 +48653,10 @@ static void map_hash_resize(JSContext *ctx, JSMapState *s) new_hash_size = 4; else new_hash_size = s->hash_size * 2; - new_hash_table = js_realloc2(ctx, s->hash_table, - sizeof(new_hash_table[0]) * new_hash_size, &slack); + new_hash_table = js_realloc(ctx, s->hash_table, + sizeof(new_hash_table[0]) * new_hash_size); if (!new_hash_table) return; - new_hash_size += slack / sizeof(*new_hash_table); for(i = 0; i < new_hash_size; i++) init_list_head(&new_hash_table[i]); diff --git a/tests/microbench.js b/tests/microbench.js index 5560c24d9..90f9e15b7 100644 --- a/tests/microbench.js +++ b/tests/microbench.js @@ -578,22 +578,78 @@ function bigint256_arith(n) return bigint_arith(n, 256); } -function set_collection_add(n) +function map_set(n) { var s, i, j, len = 100; - s = new Set(); for(j = 0; j < n; j++) { + s = new Map(); for(i = 0; i < len; i++) { - s.add(String(i), i); + s.set(String(i), i); } for(i = 0; i < len; i++) { if (!s.has(String(i))) - throw Error("bug in Set"); + throw Error("bug in Map"); } } return n * len; } +function map_delete(n) +{ + var a, i, j, len; + + len = 1000; + for(j = 0; j < n; j++) { + a = new Map(); + for(i = 0; i < len; i++) { + a.set(String(i), i); + } + for(i = 0; i < len; i++) { + a.delete(String(i)); + } + } + return len * n; +} + +function weak_map_set(n) +{ + var a, i, j, len, tab; + + len = 1000; + tab = []; + for(i = 0; i < len; i++) { + tab.push({ key: i }); + } + for(j = 0; j < n; j++) { + a = new WeakMap(); + for(i = 0; i < len; i++) { + a.set(tab[i], i); + } + } + return len * n; +} + +function weak_map_delete(n) +{ + var a, i, j, len, tab; + + len = 1000; + for(j = 0; j < n; j++) { + tab = []; + for(i = 0; i < len; i++) { + tab.push({ key: i }); + } + a = new WeakMap(); + for(i = 0; i < len; i++) { + a.set(tab[i], i); + } + for(i = 0; i < len; i++) { + tab[i] = null; + } + } + return len * n; +} + function array_for(n) { var r, i, j, sum; @@ -1034,7 +1090,10 @@ function main(argc, argv, g) closure_var, int_arith, float_arith, - set_collection_add, + map_set, + map_delete, + weak_map_set, + weak_map_delete, array_for, array_for_in, array_for_of, From 085389edfb9211b961d189dfeab3a26481d72509 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Fri, 4 Apr 2025 09:51:27 +0200 Subject: [PATCH 2/2] Agg microbench results to .gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 4ad835cf4..ff10fcd14 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ cmake-* out/ CMakeUserPresets.json fuzz -.vscode/ \ No newline at end of file +.vscode/ +microbench*.txt