Skip to content
Permalink
Browse files

perf: optimize ResultSet.getObject

This change improves performance by eliminating TypeCache lookup on a per-row basis.
The first lookup is cached inside Field
  • Loading branch information
vlsi committed Oct 29, 2015
1 parent 6dc4183 commit 567e26842095be327ff03b60bb55884be2ecf960
Showing with 51 additions and 5 deletions.
  1. +29 −0 org/postgresql/core/Field.java
  2. +22 −5 org/postgresql/jdbc2/AbstractJdbc2ResultSet.java
@@ -38,6 +38,11 @@
private String schemaName = "";
private int nullable = ResultSetMetaData.columnNullableUnknown;
private boolean autoIncrement = false;
private int sqlType;
private String pgType = NOT_YET_LOADED;

// New string to avoid clashes with other strings
private static final String NOT_YET_LOADED = new String("pgType is not yet loaded");

/*
* Construct a field based on the information fed to it.
@@ -200,4 +205,28 @@ public String toString() {
Oid.toString(oid) + "," + length + "," +
(format == TEXT_FORMAT ? 'T' : 'B') + ")";
}

public void setSQLType(int sqlType)
{
this.sqlType = sqlType;
}

public int getSQLType()
{
return sqlType;
}

public void setPGType(String pgType)
{
this.pgType = pgType;
}

public String getPGType()
{
return pgType;
}

public boolean isTypeInitialized() {
return pgType != NOT_YET_LOADED;
}
}
@@ -49,6 +49,7 @@
import org.postgresql.core.ResultCursor;
import org.postgresql.core.ResultHandler;
import org.postgresql.core.ServerVersion;
import org.postgresql.core.TypeInfo;
import org.postgresql.core.Utils;
import org.postgresql.largeobject.LargeObject;
import org.postgresql.largeobject.LargeObjectManager;
@@ -1275,7 +1276,7 @@ public synchronized void updateNull(int columnIndex)
throws SQLException
{
checkColumnIndex(columnIndex);
String columnTypeName = connection.getTypeInfo().getPGType(fields[columnIndex - 1].getOID());
String columnTypeName = getPGType(columnIndex);
updateValue(columnIndex, new NullObject(columnTypeName));
}

@@ -2880,14 +2881,30 @@ else if (ch == '-' && s.charAt(1) == '$')
return s;
}

protected String getPGType( int column ) throws SQLException
protected String getPGType(int column) throws SQLException
{
return connection.getTypeInfo().getPGType(fields[column - 1].getOID());
Field field = fields[column - 1];
initSqlType(field);
return field.getPGType();
}

protected int getSQLType( int column ) throws SQLException
protected int getSQLType(int column) throws SQLException
{
return connection.getTypeInfo().getSQLType(fields[column - 1].getOID());
Field field = fields[column - 1];
initSqlType(field);
return field.getSQLType();
}

private void initSqlType(Field field) throws SQLException
{
if (field.isTypeInitialized())
return;
TypeInfo typeInfo = connection.getTypeInfo();
int oid = field.getOID();
String pgType = typeInfo.getPGType(oid);
int sqlType = typeInfo.getSQLType(pgType);
field.setSQLType(sqlType);
field.setPGType(pgType);
}

private void checkUpdateable() throws SQLException

0 comments on commit 567e268

Please sign in to comment.
You can’t perform that action at this time.