8048586: String concatenation with optimistic types is slow

Reviewed-by: lagergren, attila
This commit is contained in:
Hannes Wallnöfer 2014-06-30 17:31:28 +02:00
parent 51aad86e6b
commit 3e0fd99223
5 changed files with 75 additions and 1 deletions
nashorn
src/jdk/nashorn/internal
test/script/basic

@ -1196,7 +1196,9 @@ final class LocalVariableTypesCalculator extends NodeVisitor<LexicalContext>{
} else if(binaryNode.isOptimisticUndecidedType()) {
// At this point, we can assign a static type to the optimistic binary ADD operator as now we know
// the types of its operands.
return binaryNode.setType(Type.widest(binaryNode.lhs().getType(), binaryNode.rhs().getType()));
final Type type = Type.widest(binaryNode.lhs().getType(), binaryNode.rhs().getType());
// Use Type.CHARSEQUENCE instead of Type.STRING to avoid conversion of ConsStrings to Strings.
return binaryNode.setType(type.equals(Type.STRING) ? Type.CHARSEQUENCE : type);
}
return binaryNode;
}

@ -171,6 +171,8 @@ class ObjectType extends Type {
invokestatic(method, JSType.TO_BOOLEAN);
} else if (to.isString()) {
invokestatic(method, JSType.TO_PRIMITIVE_TO_STRING);
} else if (to.isCharSequence()) {
invokestatic(method, JSType.TO_PRIMITIVE_TO_CHARSEQUENCE);
} else {
throw new UnsupportedOperationException("Illegal conversion " + this + " -> " + to + " " + isString() + " " + toString);
}

@ -416,6 +416,15 @@ public abstract class Type implements Comparable<Type>, BytecodeOps {
return this.equals(Type.STRING);
}
/**
* Determines whether a type is a CHARSEQUENCE type used internally strings
*
* @return true if CharSequence (internal string) type, false otherwise
*/
public boolean isCharSequence() {
return this.equals(Type.CHARSEQUENCE);
}
/**
* Determine if two types are equivalent, i.e. need no conversion
*
@ -799,6 +808,13 @@ public abstract class Type implements Comparable<Type>, BytecodeOps {
*/
public static final Type STRING = putInCache(new ObjectType(String.class));
/**
* This is the CharSequence singleton used to represent JS strings internally
* (either a {@code java.lang.String} or {@code jdk.nashorn.internal.runtime.ConsString}.
*/
public static final Type CHARSEQUENCE = putInCache(new ObjectType(CharSequence.class));
/**
* This is the object singleton, used for all object types
*/

@ -130,6 +130,9 @@ public enum JSType {
/** Combined call to toPrimitive followed by toString. */
public static final Call TO_PRIMITIVE_TO_STRING = staticCall(JSTYPE_LOOKUP, JSType.class, "toPrimitiveToString", String.class, Object.class);
/** Combined call to toPrimitive followed by toCharSequence. */
public static final Call TO_PRIMITIVE_TO_CHARSEQUENCE = staticCall(JSTYPE_LOOKUP, JSType.class, "toPrimitiveToCharSequence", CharSequence.class, Object.class);
/** Throw an unwarranted optimism exception */
public static final Call THROW_UNWARRANTED = staticCall(JSTYPE_LOOKUP, JSType.class, "throwUnwarrantedOptimismException", Object.class, Object.class, int.class);
@ -487,6 +490,16 @@ public enum JSType {
return toString(toPrimitive(obj));
}
/**
* Like {@link #toPrimitiveToString(Object)}, but avoids conversion of ConsString to String.
*
* @param obj an object
* @return the CharSequence form of the primitive form of the object
*/
public static CharSequence toPrimitiveToCharSequence(final Object obj) {
return toCharSequence(toPrimitive(obj));
}
/**
* JavaScript compliant conversion of number to boolean
*

@ -0,0 +1,41 @@
/*
* Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* JDK-8048586: String concatenation with optimistic types is slow
*
* @test
* @run
*/
var body = '';
for (var i = 0; i < 1024 * 1024; i++) {
body += 'hello world\n';
}
body = '';
for (var i = 0; i < 1024 * 1024; i++) {
body = body + 'hello world\n';
}