You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The execution performance of indexOf method of String is extremely slow for long string (23000+ characters), the calls used around 10 seconds to complete. I tried to implement a rust method to override it. It used just 0.01s. please help to improve.
Hello @t2k269, thank you for reporting this. Do you have an example string we can use to test this? Also, could you share the rust method you implemented to make this go faster?
The code is just try to find the match index of a string in a string with length around 20000.
const a = "0123456789"
let s = ""
for (let i = 0; i < 1000; i++) {
s += a;
}
s = s + "target" + s
const start = new Date().getTime()
let r = s.indexOf("target")
log = `Result: ${r}, Used ${(new Date().getTime() - start) / 1000}s`
I tried to run above code in BOA playground, but it just used 0.17s. Although it is just 0.17s, it is much slower than just running it in Google Chrome console.
For my real project, I have updated BOA to use 0.16, then compile and run it in Linux. The execution time is around 10 seconds.
The replacement rust method is just a simple closure to run rust's string::find just like
context.register_global_closure("stringIndexOf", 1, |_, args, _context| -> Result<JsValue, JsValue> {
let s = args[0].as_string().unwrap().to_string();
let t = args[1].as_string().unwrap().to_string();
Ok(match s.find(t.as_str()) {
Some(i) => JsValue::Integer(i.try_into().unwrap()),
None => JsValue::Integer(-1)
})
})?;
Then, I swap the prototype in JS
const oldStringIndexOf = String.prototype.indexOf;
String.prototype.indexOf = function(s, i) {
if (i !== undefined && i !== 0) {
return oldStringIndexOf.call(this.toString(), s, i)
}
return stringIndexOf(this.toString(), s)
}
I have to say that the playground will always be slower than the chrome console, since the playground is a VM in Webassembly running JS.
We will check if it's possible to optimize that function. Note that the function you propose is probably not 100% spec compliant, and sometimes, doing it spec compliant makes it slower. We will check it, though, to see if it could be optimized.
Thanks for your explanation. I understand that BOA must follow the JS spec. However, I notice that the indexOf slow problem appear only when I force to build my project with tag "v0.16". Previously, my project build with BOA dependency on "main" branch. On main branch, the indexOf is fast and I didn't notice any performance issue.
The reason of why I suddenly build my project with tag "v0.16" is that I saw the news from BOA that supported "async/await" officially in v0.16. The hash in main branch that I used to build my project previously is 9dda8d3
Hope these information can help to feature out the issue.
Activity
Razican commentedon Sep 29, 2022
Hello @t2k269, thank you for reporting this. Do you have an example string we can use to test this? Also, could you share the rust method you implemented to make this go faster?
t2k269 commentedon Sep 29, 2022
Hello Razican,
The code is just try to find the match index of a string in a string with length around 20000.
I tried to run above code in BOA playground, but it just used 0.17s. Although it is just 0.17s, it is much slower than just running it in Google Chrome console.
For my real project, I have updated BOA to use 0.16, then compile and run it in Linux. The execution time is around 10 seconds.
The replacement rust method is just a simple closure to run rust's string::find just like
Then, I swap the prototype in JS
Thanks for your quick reply.
Regards,
Thomas
Razican commentedon Sep 30, 2022
Thanks for the example!
I have to say that the playground will always be slower than the chrome console, since the playground is a VM in Webassembly running JS.
We will check if it's possible to optimize that function. Note that the function you propose is probably not 100% spec compliant, and sometimes, doing it spec compliant makes it slower. We will check it, though, to see if it could be optimized.
t2k269 commentedon Oct 1, 2022
Hello Razican,
Thanks for your explanation. I understand that BOA must follow the JS spec. However, I notice that the indexOf slow problem appear only when I force to build my project with tag "v0.16". Previously, my project build with BOA dependency on "main" branch. On main branch, the indexOf is fast and I didn't notice any performance issue.
The reason of why I suddenly build my project with tag "v0.16" is that I saw the news from BOA that supported "async/await" officially in v0.16. The hash in main branch that I used to build my project previously is 9dda8d3
Hope these information can help to feature out the issue.
Thanks!
jedel1043 commentedon Oct 1, 2022
Thankfully, this is fixed by #1659. Running the original snippet on 11e5bde returns:
t2k269 commentedon Oct 2, 2022
I tried to run the slow script with following 3 BOA versions
The execution time is 0.003s, 2.6s, 0.002s and 3.4s respectively.
Therefore, the problem appear on v0.16 and the current v0.17. Thanks!
Razican commentedon Oct 2, 2022
Hi @t2k269 yes, that PR has not been merged yet. You can test it in the
utf16-strbranch.t2k269 commentedon Oct 2, 2022
Oh! Great. The execution time is 0.001s on utf16-str branch, extremely fast now, thanks.