Revert "Revert "issue-2303: Consider optimising AbstractSQL::newInstance (becomes a bottleneck for small queries)""

This reverts commit c1dc00ca66.
This commit is contained in:
Ido Salomon 2020-05-27 16:56:16 +03:00 committed by GitHub
parent c0cf15d1b1
commit 93bb433455
3 changed files with 39 additions and 10 deletions

View File

@ -16,8 +16,6 @@ package com.querydsl.core.types;
import java.util.ArrayList;
import java.util.List;
import com.querydsl.core.util.ArrayUtils;
/**
* Utility class to expand {@link FactoryExpression} constructor arguments and compress {@link FactoryExpression}
* invocation arguments
@ -59,7 +57,7 @@ public final class FactoryExpressionUtils {
@Override
public T newInstance(Object... a) {
return inner.newInstance(compress(inner.getArgs(), a));
return inner.newInstance(compress(inner.getArgs(), a, 0));
}
@Override
@ -140,7 +138,7 @@ public final class FactoryExpressionUtils {
return counter;
}
private static Object[] compress(List<Expression<?>> exprs, Object[] args) {
private static Object[] compress(List<Expression<?>> exprs, Object[] args, int from) {
Object[] rv = new Object[exprs.size()];
int offset = 0;
for (int i = 0; i < exprs.size(); i++) {
@ -151,11 +149,11 @@ public final class FactoryExpressionUtils {
if (expr instanceof FactoryExpression<?>) {
FactoryExpression<?> fe = (FactoryExpression<?>) expr;
int fullArgsLength = countArguments(fe);
Object[] compressed = compress(fe.getArgs(), ArrayUtils.subarray(args, offset, offset + fullArgsLength));
Object[] compressed = compress(fe.getArgs(), args, offset);
rv[i] = fe.newInstance(compressed);
offset += fullArgsLength;
} else {
rv[i] = args[offset];
rv[i] = args[from + offset];
offset++;
}
}

View File

@ -20,12 +20,14 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import javax.inject.Provider;
import com.querydsl.sql.types.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
@ -447,15 +449,17 @@ public abstract class AbstractSQLQuery<T, Q extends AbstractSQLQuery<T, Q>> exte
listeners.executed(context);
try {
lastCell = null;
final List<T> rv = new ArrayList<T>();
final List<T> rv = new LinkedList<T>();
if (expr instanceof FactoryExpression) {
FactoryExpression<T> fe = (FactoryExpression<T>) expr;
List<Type<T>> types = getTypesFromExpressions(fe.getArgs());
int sizeOfTypes = types.size();
while (rs.next()) {
if (getLastCell) {
lastCell = rs.getObject(fe.getArgs().size() + 1);
getLastCell = false;
}
rv.add(newInstance(fe, rs, 0));
rv.add(fe.newInstance(populateArguments(sizeOfTypes, types, rs)));
}
} else if (expr.equals(Wildcard.all)) {
while (rs.next()) {
@ -470,12 +474,14 @@ public abstract class AbstractSQLQuery<T, Q extends AbstractSQLQuery<T, Q>> exte
rv.add((T) row);
}
} else {
Path<?> path = (expr instanceof Path) ? (Path<?>) expr : null;
Type<? extends T> type = configuration.getType(path, expr.getType());
while (rs.next()) {
if (getLastCell) {
lastCell = rs.getObject(2);
getLastCell = false;
}
rv.add(get(rs, expr, 1, expr.getType()));
rv.add(configuration.get(rs, 1, type));
}
}
return rv;
@ -562,6 +568,26 @@ public abstract class AbstractSQLQuery<T, Q extends AbstractSQLQuery<T, Q>> exte
return c.newInstance(args);
}
private Object[] populateArguments(int typesSize, List<Type<T>> types, ResultSet rs)
throws InstantiationException, IllegalAccessException, InvocationTargetException, SQLException {
Object[] args = new Object[typesSize];
int i = 0;
for (Type type : types) {
args[i] = type.getValue(rs, ++i);
}
return args;
}
private List<Type<T>> getTypesFromExpressions(List<Expression<?>> expressions) {
List<Type<T>> types = new ArrayList<Type<T>>();
for (Expression e : expressions) {
Path path = e instanceof Path ? (Path<?>) e : null;
Type t = configuration.getType(path, e.getType());
types.add(t);
}
return types;
}
private void reset() {
cleanupMDC();
}

View File

@ -195,6 +195,11 @@ public final class Configuration {
return getType(path, clazz).getValue(rs, i);
}
@Nullable
public <T> T get(ResultSet rs,int i, Type<T> type) throws SQLException {
return type.getValue(rs, i);
}
/**
* Get the schema/table override
*
@ -269,7 +274,7 @@ public final class Configuration {
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private <T> Type<T> getType(@Nullable Path<?> path, Class<T> clazz) {
<T> Type<T> getType(@Nullable Path<?> path, Class<T> clazz) {
if (hasTableColumnTypes && path != null && !clazz.equals(Null.class)
&& path.getMetadata().getParent() instanceof RelationalPath) {
String table = ((RelationalPath) path.getMetadata().getParent()).getTableName();