-
Notifications
You must be signed in to change notification settings - Fork 848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
set capacity for StringBuilder in String#repeat #188
Conversation
Good catch! But long size = str.length() * (int) cnt;
// Check for overflow
if (size >= Integer.MAX_VALUE) {
size = Integer.MAX_VALUE;
} is not a correct way to check for overflow, because |
@sainaen Thank you! I fixed. |
@tuchida |
Possibly, it might be able to more quickly. (Japanese page, sorry) |
Huh. Warning: My performance-testing-fu is really, really weak (it's literally my first use of JMH), so please don't take results seriously without looking into benchmark. (plus, I would really like to get some feedback on it.) With that said, here is my benchmarking results (JDK 1.8.0_45):
So while preallocating PS. Interestingly Groovy has this UPD: UPD2: UPD3:
doubling with substring method:
looks like an improvement to me. 📈 :) * — probably. (please, read that warning again ↑) |
The last implementation is very clever and seems to help a lot with performance. @tuchida do you want to try and update the PR? |
Yes, it is. I wish I could say that I came up with it myself, but I found it in the jsperf test linked in the article from @tuchida, so all thanks go to him for the link and original author for the example. :) Now, I see an issue in how we treat the overflow: when |
I tried to improve the https://github.com/sainaen/StringRepeatBenchmark/blob/master/src/main/java/com/sainaen/V8Repeater.java JavaScript String is Rope String. So it is to combine string is fast. |
It's true, but here we always cast it to regular Java String, so no gain with that, IMO.
How did you tested that? I'm asking, because the worst case for doubling method is when the I've tried to run your
But ok, your current version is good enough for me. And we can address that issue with overflow in a separate PR later. |
Good point about "ConsString." If we want this (and other NativeString methods) to be really efficient, we should do something smart with that. Otherwise, we first have to flatten the ConsString (also using StringBuilder) before we start to repeat it. A good way to test would be to see if a string like: 'foo' + 'bar' + 'baz' performs differently than just 'foobarbaz'. Also, for settling these small performance issues, the best way to know for sure is to test with a framework like Google Caliper: https://code.google.com/p/caliper/ It makes sure that you run the micro-benchmark enough times until it gets a repeatable result (due to compilation, etc), that GC doesn't have a big effect, and so on. It's worth trying. |
@gbrail
Here's a difference between
so, it looks like it doesn't affect performance much. |
Thanks -- will check out JMH next time. Just making sure that we don't On Fri, May 8, 2015 at 1:48 PM, Ivan Vyshnevskyi notifications@github.com
greg brail | apigee https://apigee.com/ | twitter @gbrail |
Although this my idea is uncool way... When replace StringBuilder with char[] and use System.arraycopy, fast a little.
Maybe, ConsString can be faster in the same way. |
This has been a good discussion and in general this sounds like a good change to make. Would you like to consolidate the thread above into one change that you think is best? |
OK -- we can keep trying additional optimizations, but the "doubling" one that tuchida posted seems like a good and simple improvement so I pushed it. We can keep elaborating if you'd like. We can also improve other areas of performance if you guys are interested -- remember that the first commit in this repo is from 1999 -- we have learned a little bit about Java performance since then! |
Thanks! And then, ConsString did not make fast. |
repeat.js
non capacity
set capacity