From ca89b25c8fd28e70d912614650151528aa0c545c Tue, 31 Mar 2015 15:44:50 +0300 From: Robert Munteanu Date: Tue, 31 Mar 2015 15:40:35 +0300 Subject: [PATCH] OAK-2710 - Utils.unshareString should not generate String instances unless needed diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/Utils.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/Utils.java index c40f359..ce9c135 100644 --- a/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/Utils.java +++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/document/util/Utils.java @@ -35,6 +35,7 @@ import javax.annotation.Nullable; import com.google.common.base.Predicate; +import com.google.common.base.StandardSystemProperty; import com.google.common.collect.AbstractIterator; import com.mongodb.BasicDBObject; @@ -57,6 +58,32 @@ * Utility methods. */ public class Utils { + + private static final boolean NEEDS_STRING_UNSHARE; + + static { + String version = System.getProperty(StandardSystemProperty.JAVA_VERSION.key()); + + if (version.startsWith("1.6")) { + // always unshare for Java 1.6 + NEEDS_STRING_UNSHARE = true; + } else if (version.startsWith("1.7")) { + // unshare for 1.7 if we have an old enough version + int buildSeparatorIdx = version.indexOf('_'); + if (buildSeparatorIdx != -1 && buildSeparatorIdx < version.length()) { + int build = Integer.parseInt(version.substring(buildSeparatorIdx + 1)); + NEEDS_STRING_UNSHARE = build < 6; + } else { + // if we can't parse the build number, fall back to unsharing + NEEDS_STRING_UNSHARE = true; + } + + } else { + // none of the versions following 1.7 need unsharing + NEEDS_STRING_UNSHARE = false; + } + } + private static final Logger LOG = LoggerFactory.getLogger(Utils.class); /** @@ -105,10 +132,14 @@ * http://mail.openjdk.java.net/pipermail/core-libs-dev/2012-May/010257.html * * @param x the string - * @return the new string + * @return the new string or the passed string for recent enough versions of Java */ public static String unshareString(String x) { - return new String(x); + if (NEEDS_STRING_UNSHARE) { + return new String(x); + } + + return x; } public static int pathDepth(String path) {