Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
String.substring - very low performance #27810
I created a mini performance test for String.substring
Dart VM version: 1.20.1 (Wed Oct 12 22:00:54 2016) on "linux_x64"
NodeJs slice string test
Dart slice (substring) string test
dart ~450 times slower nodejs.
V8, used by node.js, has a cheap substring operation for short-lived substrings. It creates a small object that points into the original string. I believe the substring "slice" is converted to a real string when it's garbage collected, so keeping a lot of substrings alive for a long time will eventually cost anyway, but if it's thrown away immediately, then it's cheap.
The cost of this optimization is that V8 needs handle one more type of string in all places where it handles strings. This complicates both the runtime system and garbage collection.
The Dart VM doesn't have an optimization like this. If you make a 99900 character substring of another string, you copy 99900 characters.
I looked into this. It seems that
To be honest I think that the best way to fix those overheads is to rewrite the
A: Implement string slices in the VM I don't think this is likely to happen. It's quite a bit of complexity that helps only few cases, while introducing subtle memory leaks. Developers should write their code under expectation that
B: Add APIs that would make calling substring unnecessary For example ES2015 adds sticky flag that allows you to create regexps that stick to a particular index in the string - i.e. they match if and only if there is a match starting at that index. In reality we already have similar API on the
I made some prototype changes to the VM and
So to summarize my suggestions: @Aetet if you don't want to rewrite the
@DisDis: are you also using
I think this issue can be closed. Though I do have few other cleanups to push out into