Skip to content

Commit 8dda2a0

Browse files
Fabrice Bellardsaghul
authored andcommitted
fixed hash_map_resize() - added Map/WeakMap in microbench
saghul: Also removed slack since it's unused.
1 parent ef0d29d commit 8dda2a0

File tree

2 files changed

+66
-9
lines changed

2 files changed

+66
-9
lines changed

quickjs.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48645,7 +48645,6 @@ static JSMapRecord *map_find_record(JSContext *ctx, JSMapState *s,
4864548645
static void map_hash_resize(JSContext *ctx, JSMapState *s)
4864648646
{
4864748647
uint32_t new_hash_size, i, h;
48648-
size_t slack;
4864948648
struct list_head *new_hash_table, *el;
4865048649
JSMapRecord *mr;
4865148650

@@ -48654,11 +48653,10 @@ static void map_hash_resize(JSContext *ctx, JSMapState *s)
4865448653
new_hash_size = 4;
4865548654
else
4865648655
new_hash_size = s->hash_size * 2;
48657-
new_hash_table = js_realloc2(ctx, s->hash_table,
48658-
sizeof(new_hash_table[0]) * new_hash_size, &slack);
48656+
new_hash_table = js_realloc(ctx, s->hash_table,
48657+
sizeof(new_hash_table[0]) * new_hash_size);
4865948658
if (!new_hash_table)
4866048659
return;
48661-
new_hash_size += slack / sizeof(*new_hash_table);
4866248660

4866348661
for(i = 0; i < new_hash_size; i++)
4866448662
init_list_head(&new_hash_table[i]);

tests/microbench.js

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -578,22 +578,78 @@ function bigint256_arith(n)
578578
return bigint_arith(n, 256);
579579
}
580580

581-
function set_collection_add(n)
581+
function map_set(n)
582582
{
583583
var s, i, j, len = 100;
584-
s = new Set();
585584
for(j = 0; j < n; j++) {
585+
s = new Map();
586586
for(i = 0; i < len; i++) {
587-
s.add(String(i), i);
587+
s.set(String(i), i);
588588
}
589589
for(i = 0; i < len; i++) {
590590
if (!s.has(String(i)))
591-
throw Error("bug in Set");
591+
throw Error("bug in Map");
592592
}
593593
}
594594
return n * len;
595595
}
596596

597+
function map_delete(n)
598+
{
599+
var a, i, j, len;
600+
601+
len = 1000;
602+
for(j = 0; j < n; j++) {
603+
a = new Map();
604+
for(i = 0; i < len; i++) {
605+
a.set(String(i), i);
606+
}
607+
for(i = 0; i < len; i++) {
608+
a.delete(String(i));
609+
}
610+
}
611+
return len * n;
612+
}
613+
614+
function weak_map_set(n)
615+
{
616+
var a, i, j, len, tab;
617+
618+
len = 1000;
619+
tab = [];
620+
for(i = 0; i < len; i++) {
621+
tab.push({ key: i });
622+
}
623+
for(j = 0; j < n; j++) {
624+
a = new WeakMap();
625+
for(i = 0; i < len; i++) {
626+
a.set(tab[i], i);
627+
}
628+
}
629+
return len * n;
630+
}
631+
632+
function weak_map_delete(n)
633+
{
634+
var a, i, j, len, tab;
635+
636+
len = 1000;
637+
for(j = 0; j < n; j++) {
638+
tab = [];
639+
for(i = 0; i < len; i++) {
640+
tab.push({ key: i });
641+
}
642+
a = new WeakMap();
643+
for(i = 0; i < len; i++) {
644+
a.set(tab[i], i);
645+
}
646+
for(i = 0; i < len; i++) {
647+
tab[i] = null;
648+
}
649+
}
650+
return len * n;
651+
}
652+
597653
function array_for(n)
598654
{
599655
var r, i, j, sum;
@@ -1034,7 +1090,10 @@ function main(argc, argv, g)
10341090
closure_var,
10351091
int_arith,
10361092
float_arith,
1037-
set_collection_add,
1093+
map_set,
1094+
map_delete,
1095+
weak_map_set,
1096+
weak_map_delete,
10381097
array_for,
10391098
array_for_in,
10401099
array_for_of,

0 commit comments

Comments
 (0)