-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Performance of Constant string printing [ci: last-only] #10897
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
Conversation
@retronym want to test this out on the customer codebase...? |
Some context -- this method is called indirectly by Zinc's Extract API phase to generate its representation of annotations. |
The last commit is, "pre-construct with string add to enclose in quotes" then "expand where necessary using replace". That optimizes for the common case, where the hit for expansion (escaping) is array copy (which only sounds bad). The savings is the append per char (which is bad). The assumption is that the pre-construction is optimal, so it relies on the platform. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but I'm not sure whether to merge without Jason's approval
I'd like @retronym guess about the last commit and comment. I didn't write a test to estimate what ratio of escapes would make it less desirable, but maybe annotations have fewer of them. The other choice is prescan for escapable chars; then only strings with escapes must suffer construction costs. Maybe I'll add that while we're waiting. |
Actually I saw retronym doing thyme timings at c3fcd4e and felt bad for being lazy, so I will try to measure if this is useful. I was at that commit because while exercising |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm late to the party, so don't know what was said in the welcome speech..
92a7e7e
to
7ed7368
Compare
@lrytz squashed, rebased, unwound experiment using replace (I haven't tested performance yet), added pre-check for whether there are escapes to expand. Probably the ticket doesn't require extreme performance. Using string concat (stradd) by default assumes that, while we must be JDK 8 compat, hopefully it's using modern intrinsic for adding quote chars, which I assume to be array copy of underlying chars. However, also untested. |
I tested by moving the You were wise to merge before I benched the Small improvement by sizing a string builder for simple wrapping in quotes:
This is with a 3000 char string, so probably it allocates a big array plus margin when appending; maybe it must arraycopy on toString; is there no arraycopy if exactly sized? |
Thank you for the benchmark!
There must be a copy, the StringBuilder can continue to be used. Unless they do something fancy, like a dirty bit in the string builder and lazy array copy, but I didn't see anything like that. |
Use a StringBuilder to build a String. Since quotes are always added, it's not obvious that string add is worthwhile after chars have been copied to the builder. Could also prescan for an escapable char.
Fixes scala/scala-dev#878