Introduction of QualifiedNameInterning to configure the behaviour of QualifiedName regarding String interning

Signed-off-by: Oliver Libutzki <oliver@libutzki.de>
This commit is contained in:
Oliver Libutzki 2022-01-10 14:28:54 +01:00
parent 4b5dd10f6f
commit 5fa55c3ca9
2 changed files with 62 additions and 10 deletions

View file

@ -36,7 +36,7 @@ public class QualifiedName implements Comparable<QualifiedName> {
private QualifiedName lowerCase;
private static boolean USE_INTERNING = Boolean.getBoolean("xtext.qn.interning");
static boolean USE_INTERNING = Boolean.getBoolean("xtext.qn.interning");
/**
* The single existing empty QualifiedName.
@ -128,18 +128,15 @@ public class QualifiedName implements Comparable<QualifiedName> {
}
/**
* Returns string internal instance from string pool, if a system property {@code xtext.qn.interning} is set to {@code true}, or the
* same object otherwise.
* Returns string internal instance from string pool, if {@link #USE_INTERNING} is true, or the same object
* otherwise.
* <p>
* Implementation notes:
* <ol>
* <li>Interning {@link String} objects may affect performance, see bug 484215.
* <li>Interning {@link String} objects is not recommended for older JVM's, because of possible perm gen memory explosion, see
* http://java-performance.info/string-intern-in-java-6-7-8/.
* </ol>
* {@link #USE_INTERNING} can be set by using the system property {@code xtext.qn.interning} or by calling
* {@link QualifiedNameInterning#enable()} / {@link QualifiedNameInterning#disable()}.<br/>
* The default value is <code>false</code>.
*/
private static String intern(String string) {
return USE_INTERNING? CommonUtil.intern(string) : string;
return USE_INTERNING ? CommonUtil.intern(string) : string;
}
/**

View file

@ -0,0 +1,55 @@
/*******************************************************************************
* Copyright (c) 2022 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.xtext.naming;
/**
* Provides methods for enabling and disabling the interning of strings used in {@link QualifiedName}.
* <p>
* String interning is disabled by default. It can be enabled by setting the system property {@code xtext.qn.interning}
* to <code>true</code> or by calling {@link #enable()}.
* <p>
* Caution: Setting the system property at runtime does not have an effect. Use {@link #enable()} to enable String
* interning at runtime.
*
* @since 2.26.0
*
* @author Oliver Libutzki - Initial contribution and API
*
* @see QualifiedName#USE_INTERNING
*
*/
public final class QualifiedNameInterning {
/**
* Private constructor to ensure that this class cannot be instantiated.
*/
private QualifiedNameInterning() {
}
/**
* Enables the usage of a String pool for {@link QualifiedName QualifiedName's} segments.
* <p>
* Note: {@link QualifiedName QualifiedNames} which are created before enabling are not affected.
* <p>
* Caution: Interning {@link String} objects may affect performance, see bug 484215.
*/
public static void enable() {
QualifiedName.USE_INTERNING = true;
}
/**
* Disables the usage of a String pool for {@link QualifiedName QualifiedName's} segments.
* <p>
* Note: {@link QualifiedName QualifiedNames} which are created before disabling are not affected.
*/
public static void disable() {
QualifiedName.USE_INTERNING = false;
}
}