Skip to content
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

Performance | Remove Enum.values() calls to avoid unnecessary array cloning #1065

Merged
merged 2 commits into from
May 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 29 additions & 16 deletions src/main/java/com/microsoft/sqlserver/jdbc/DataTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,15 @@ enum TDSType {
private final int intValue;

private static final int MAXELEMENTS = 256;
private static final TDSType[] VALUES = values();
private static final TDSType valuesTypes[] = new TDSType[MAXELEMENTS];

byte byteValue() {
return (byte) intValue;
}

static {
for (TDSType s : values())
for (TDSType s : VALUES)
valuesTypes[s.intValue] = s;
}

Expand Down Expand Up @@ -152,6 +153,7 @@ enum SSType {
final Category category;
private final String name;
private final JDBCType jdbcType;
private static final SSType[] VALUES = values();

static final BigDecimal MAX_VALUE_MONEY = new BigDecimal("922337203685477.5807");
static final BigDecimal MIN_VALUE_MONEY = new BigDecimal("-922337203685477.5808");
Expand All @@ -173,7 +175,7 @@ final JDBCType getJDBCType() {
}

static SSType of(String typeName) throws SQLServerException {
for (SSType ssType : values())
for (SSType ssType : VALUES)
if (ssType.name.equalsIgnoreCase(typeName))
return ssType;

Expand Down Expand Up @@ -201,7 +203,9 @@ enum Category {
TIMESTAMP,
UDT,
SQL_VARIANT,
XML
XML;

private static final Category[] VALUES = values();
}

enum GetterConversion {
Expand Down Expand Up @@ -265,6 +269,7 @@ enum GetterConversion {

private final SSType.Category from;
private final EnumSet<JDBCType.Category> to;
private static final GetterConversion[] VALUES = values();

private GetterConversion(SSType.Category from, EnumSet<JDBCType.Category> to) {
this.from = from;
Expand All @@ -275,10 +280,10 @@ private GetterConversion(SSType.Category from, EnumSet<JDBCType.Category> to) {
SSType.Category.class);

static {
for (SSType.Category category : SSType.Category.values())
for (SSType.Category category : SSType.Category.VALUES)
conversionMap.put(category, EnumSet.noneOf(JDBCType.Category.class));

for (GetterConversion conversion : values())
for (GetterConversion conversion : VALUES)
conversionMap.get(conversion.from).addAll(conversion.to);
}

Expand Down Expand Up @@ -473,6 +478,7 @@ JDBCType getJDBCType(SSType ssType, JDBCType jdbcTypeFromApp) {
private final Class<?> javaClass;
private final JDBCType jdbcTypeFromJavaType;
private static double jvmVersion = 0.0;
private static final JavaType[] VALUES = values();

private JavaType(Class<?> javaClass, JDBCType jdbcTypeFromJavaType) {
this.javaClass = javaClass;
Expand Down Expand Up @@ -521,7 +527,7 @@ static JavaType of(Object obj) {
if (obj instanceof SQLServerDataTable || obj instanceof ResultSet || obj instanceof ISQLServerDataRecord)
return JavaType.TVP;
if (null != obj) {
for (JavaType javaType : values())
for (JavaType javaType : VALUES)
// if JVM version is prior to Java 8, the javaClass variable can be
// null if the java type is introduced in Java 8
if (null != javaType.javaClass) {
Expand Down Expand Up @@ -578,6 +584,7 @@ enum SetterConversionAE {

private final EnumSet<JDBCType> to;
private final JavaType from;
private static final SetterConversionAE[] VALUES = values();

private SetterConversionAE(JavaType from, EnumSet<JDBCType> to) {
this.from = from;
Expand All @@ -587,10 +594,10 @@ private SetterConversionAE(JavaType from, EnumSet<JDBCType> to) {
private static final EnumMap<JavaType, EnumSet<JDBCType>> setterConversionAEMap = new EnumMap<>(JavaType.class);

static {
for (JavaType javaType : JavaType.values())
for (JavaType javaType : JavaType.VALUES)
setterConversionAEMap.put(javaType, EnumSet.noneOf(JDBCType.class));

for (SetterConversionAE conversion : values())
for (SetterConversionAE conversion : VALUES)
setterConversionAEMap.get(conversion.from).addAll(conversion.to);
}

Expand Down Expand Up @@ -668,6 +675,7 @@ enum JDBCType {
final Category category;
private final int intValue;
private final String className;
private static final JDBCType[] VALUES = values();

final String className() {
return className;
Expand Down Expand Up @@ -711,7 +719,9 @@ enum Category {
GUID,
SQL_VARIANT,
GEOMETRY,
GEOGRAPHY
GEOGRAPHY;

private static final Category[] VALUES = values();
}

// This SetterConversion enum is based on the Category enum
Expand Down Expand Up @@ -786,6 +796,7 @@ enum SetterConversion {

private final JDBCType.Category from;
private final EnumSet<JDBCType.Category> to;
private static final SetterConversion[] VALUES = values();

private SetterConversion(JDBCType.Category from, EnumSet<JDBCType.Category> to) {
this.from = from;
Expand All @@ -796,10 +807,10 @@ private SetterConversion(JDBCType.Category from, EnumSet<JDBCType.Category> to)
JDBCType.Category.class);

static {
for (JDBCType.Category category : JDBCType.Category.values())
for (JDBCType.Category category : JDBCType.Category.VALUES)
conversionMap.put(category, EnumSet.noneOf(JDBCType.Category.class));

for (SetterConversion conversion : values())
for (SetterConversion conversion : VALUES)
conversionMap.get(conversion.from).addAll(conversion.to);
}

Expand Down Expand Up @@ -885,6 +896,7 @@ enum UpdaterConversion {

private final JDBCType.Category from;
private final EnumSet<SSType.Category> to;
private static final UpdaterConversion[] VALUES = values();

private UpdaterConversion(JDBCType.Category from, EnumSet<SSType.Category> to) {
this.from = from;
Expand All @@ -895,10 +907,10 @@ private UpdaterConversion(JDBCType.Category from, EnumSet<SSType.Category> to) {
JDBCType.Category.class);

static {
for (JDBCType.Category category : JDBCType.Category.values())
for (JDBCType.Category category : JDBCType.Category.VALUES)
conversionMap.put(category, EnumSet.noneOf(SSType.Category.class));

for (UpdaterConversion conversion : values())
for (UpdaterConversion conversion : VALUES)
conversionMap.get(conversion.from).addAll(conversion.to);
}

Expand All @@ -912,7 +924,7 @@ boolean convertsTo(SSType ssType) {
}

static JDBCType of(int intValue) throws SQLServerException {
for (JDBCType jdbcType : values())
for (JDBCType jdbcType : VALUES)
if (jdbcType.intValue == intValue)
return jdbcType;

Expand Down Expand Up @@ -1064,6 +1076,7 @@ enum NormalizationAE {

private final JDBCType from;
private final EnumSet<SSType> to;
private static final NormalizationAE[] VALUES = values();

private NormalizationAE(JDBCType from, EnumSet<SSType> to) {
this.from = from;
Expand All @@ -1073,10 +1086,10 @@ private NormalizationAE(JDBCType from, EnumSet<SSType> to) {
private static final EnumMap<JDBCType, EnumSet<SSType>> normalizationMapAE = new EnumMap<>(JDBCType.class);

static {
for (JDBCType jdbcType : JDBCType.values())
for (JDBCType jdbcType : JDBCType.VALUES)
normalizationMapAE.put(jdbcType, EnumSet.noneOf(SSType.class));

for (NormalizationAE conversion : values())
for (NormalizationAE conversion : VALUES)
normalizationMapAE.get(conversion.from).addAll(conversion.to);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public enum InternalSpatialDatatype {

private byte typeCode;
private String typeName;
private static final InternalSpatialDatatype[] VALUES = values();

private InternalSpatialDatatype(byte typeCode, String typeName) {
this.typeCode = typeCode;
Expand All @@ -39,7 +40,7 @@ String getTypeName() {
}

static InternalSpatialDatatype valueOf(byte typeCode) {
for (InternalSpatialDatatype internalType : values()) {
for (InternalSpatialDatatype internalType : VALUES) {
if (internalType.typeCode == typeCode) {
return internalType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ enum SQLServerEncryptionType {
PlainText((byte) 0);

final byte value;
private static final SQLServerEncryptionType[] VALUES = values();

SQLServerEncryptionType(byte val) {
this.value = val;
Expand All @@ -29,7 +30,7 @@ byte getValue() {
}

static SQLServerEncryptionType of(byte val) throws SQLServerException {
for (SQLServerEncryptionType type : values())
for (SQLServerEncryptionType type : VALUES)
if (val == type.value)
return type;

Expand Down
1 change: 1 addition & 0 deletions src/samples/adaptive/src/main/java/ReadLargeData.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.microsoft.sqlserver.jdbc.SQLServerStatement;

/**
Expand Down
1 change: 1 addition & 0 deletions src/samples/datatypes/src/main/java/SpatialDataTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import com.microsoft.sqlserver.jdbc.Geography;
import com.microsoft.sqlserver.jdbc.Geometry;
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.math.BigDecimal;
import java.sql.Types;

import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
Expand Down