added string ropes for faster concatenation of long strings (issue #67)

This commit is contained in:
Fabrice Bellard
2025-03-25 16:01:40 +01:00
parent 372ad84e9a
commit 156d981afe
4 changed files with 624 additions and 71 deletions

View File

@@ -957,6 +957,32 @@ function string_build4(n)
return n * 1000;
}
/* append */
function string_build_large1(n)
{
var i, j, r, len = 20000;
for(j = 0; j < n; j++) {
r = "";
for(i = 0; i < len; i++)
r += "abcdef";
global_res = r;
}
return n * len;
}
/* prepend */
function string_build_large2(n)
{
var i, j, r, len = 20000;
for(j = 0; j < n; j++) {
r = "";
for(i = 0; i < len; i++)
r = "abcdef" + r;
global_res = r;
}
return n * len;
}
/* sort bench */
function sort_bench(text) {
@@ -1336,6 +1362,8 @@ function main(argc, argv, g)
string_build2,
string_build3,
string_build4,
string_build_large1,
string_build_large2,
int_to_string,
int_toString,
float_to_string,

View File

@@ -864,6 +864,32 @@ function test_generator()
assert(v.value === 6 && v.done === true);
}
function rope_concat(n, dir)
{
var i, s;
s = "";
if (dir > 0) {
for(i = 0; i < n; i++)
s += String.fromCharCode(i & 0xffff);
} else {
for(i = n - 1; i >= 0; i--)
s = String.fromCharCode(i & 0xffff) + s;
}
for(i = 0; i < n; i++) {
/* test before the assert to go faster */
if (s.charCodeAt(i) != (i & 0xffff)) {
assert(s.charCodeAt(i), i & 0xffff);
}
}
}
function test_rope()
{
rope_concat(100000, 1);
rope_concat(100000, -1);
}
test();
test_function();
test_enum();
@@ -880,3 +906,4 @@ test_symbol();
test_map();
test_weak_map();
test_generator();
test_rope();