JDK-8048586 : String concatenation with optimistic types is slow
  • Type: Bug
  • Component: core-libs
  • Sub-Component: jdk.nashorn
  • Affected Version: 9
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2014-06-27
  • Updated: 2015-01-21
  • Resolved: 2014-06-30
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
8u40Fixed 9 b22Fixed
this test abridged from node's test/pummel/test-https-large-response.js -

var body = '';
print('build body...');
for (var i = 0; i < 1024 * 1024; i++) {
  body += 'hello world\n';

with jdk8u20, this test case takes 1.5s, with jdk9 (1.9.0-internal-akhil_2014_06_27_09_52-b00), takes more than several minutes and has to be killed. I suspect exponential behavior in string concat.
verified in 9b25

You can disable optimistic typing as a workaround. What we need to do to fix this is to convert to CharSequence instead of String so ConsStrings are preserved. I was able to fix the problem using this approach, but there are other cases when we actually need to convert to String. We have lots of conversions in codegen and I'll have to figure out when conversion target should be String and when CharSequence.

should --optimistic-types=false be the default?

This is caused by calls to jdk.nashorn.internal.runtime.JSType.toPrimitiveToString in optimistic mode. The problem goes away with --optimistic-types=false.