[#2144] Improve AbstractField.equals() and AbstractTable.equals()

and similar, as these two are called very often
This commit is contained in:
Lukas Eder 2013-01-31 17:35:20 +01:00
parent b1cd631026
commit 5a94b69ea0
8 changed files with 110 additions and 10 deletions

View File

@ -77,15 +77,28 @@ public class BenchmarkTests<
T785 extends TableRecord<T785>>
extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T725, T639, T785> {
private static final int REPETITIONS = 100;
private static final String RANDOM = "" + new Random().nextLong();
private static final int REPETITIONS_FIELD_ACCESS = 1000000;
private static final int REPETITIONS_SELECT = 100;
private static final String RANDOM = "" + new Random().nextLong();
public BenchmarkTests(jOOQAbstractTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T725, T639, T785> delegate) {
super(delegate);
}
@Test
public void testBenchmark() throws Exception {
public void testBenchmarkFieldAccess() throws Exception {
// This benchmark is inspired by a private contribution by Roberto Giacco
B book = create().newRecord(TBook());
for (int i = 0; i < REPETITIONS_FIELD_ACCESS; i++) {
book.setValue(TBook_ID(), i);
book.setValue(TBook_AUTHOR_ID(), book.getValue(TBook_ID()));
}
}
@Test
public void testBenchmarkSelect() throws Exception {
// This benchmark is contributed by "jjYBdx4IL" on GitHub:
// https://github.com/jOOQ/jOOQ/issues/1625
@ -103,13 +116,13 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, UU, I, IPK, T7
StopWatch watch = new StopWatch();
watch.splitInfo("Benchmark start");
testBenchmarkFullExecution(create, REPETITIONS);
testBenchmarkFullExecution(create, REPETITIONS_SELECT);
watch.splitInfo("Full re-execution");
testBenchmarkReuseSelect(create, REPETITIONS);
testBenchmarkReuseSelect(create, REPETITIONS_SELECT);
watch.splitInfo("Reuse select");
testBenchmarkReuseSQLString(create, REPETITIONS);
testBenchmarkReuseSQLString(create, REPETITIONS_SELECT);
watch.splitInfo("Reuse SQL String");
}

View File

@ -1975,8 +1975,13 @@ public abstract class jOOQAbstractTest<
}
@Test
public void testBenchmark() throws Exception {
new BenchmarkTests(this).testBenchmark();
public void testBenchmarkFieldAccess() throws Exception {
new BenchmarkTests(this).testBenchmarkFieldAccess();
}
@Test
public void testBenchmarkSelect() throws Exception {
new BenchmarkTests(this).testBenchmarkSelect();
}
@Test

View File

@ -1604,6 +1604,22 @@ abstract class AbstractField<T> extends AbstractQueryPart implements Field<T> {
// XXX: Object API
// ------------------------------------------------------------------------
@Override
public boolean equals(Object that) {
// [#2144] Non-equality can be decided early, without executing the
// rather expensive implementation of AbstractQueryPart.equals()
if (that instanceof AbstractField) {
if (name.equals(((AbstractField<?>) that).name)) {
return super.equals(that);
}
return false;
}
return false;
}
@Override
public int hashCode() {

View File

@ -259,9 +259,8 @@ abstract class AbstractRecord extends AbstractStore implements Record {
return (Value<T>) v[index];
}
@SuppressWarnings("unchecked")
final <T> Value<T> getValue0(Field<T> field) {
return (Value<T>) getValues()[fieldsRow().indexOf(field)];
return getValue0(fieldsRow().indexOf(field));
}
final Value<?>[] getValues() {

View File

@ -415,6 +415,22 @@ abstract class AbstractTable<R extends Record> extends AbstractFieldProviderQuer
// XXX: Object API
// ------------------------------------------------------------------------
@Override
public boolean equals(Object that) {
// [#2144] Non-equality can be decided early, without executing the
// rather expensive implementation of AbstractQueryPart.equals()
if (that instanceof AbstractTable) {
if (name.equals(((AbstractTable<?>) that).name)) {
return super.equals(that);
}
return false;
}
return false;
}
@Override
public int hashCode() {

View File

@ -139,4 +139,20 @@ public class SchemaImpl extends AbstractQueryPart implements Schema {
public List<Sequence<?>> getSequences() {
return Collections.emptyList();
}
// ------------------------------------------------------------------------
// XXX: Object API
// ------------------------------------------------------------------------
@Override
public boolean equals(Object that) {
// [#2144] SchemaImpl equality can be decided without executing the
// rather expensive implementation of AbstractQueryPart.equals()
if (that instanceof SchemaImpl) {
return getName().equals(((SchemaImpl) that).getName());
}
return super.equals(that);
}
}

View File

@ -82,4 +82,21 @@ class TableFieldImpl<R extends Record, T> extends AbstractField<T> implements Ta
@Override
public final void bind(BindContext context) {}
// ------------------------------------------------------------------------
// XXX: Object API
// ------------------------------------------------------------------------
@Override
public boolean equals(Object that) {
// [#2144] TableFieldImpl equality can be decided without executing the
// rather expensive implementation of AbstractQueryPart.equals()
if (that instanceof TableField) {
TableField<?, ?> other = (TableField<?, ?>) that;
return getTable().equals(other.getTable()) && getName().equals(other.getName());
}
return super.equals(that);
}
}

View File

@ -41,6 +41,7 @@ import org.jooq.Record;
import org.jooq.RenderContext;
import org.jooq.Schema;
import org.jooq.Table;
import org.jooq.tools.StringUtils;
/**
* A common base type for tables
@ -172,4 +173,21 @@ public class TableImpl<R extends Record> extends AbstractTable<R> {
return super.declaresTables();
}
}
// ------------------------------------------------------------------------
// XXX: Object API
// ------------------------------------------------------------------------
@Override
public boolean equals(Object that) {
// [#2144] TableImpl equality can be decided without executing the
// rather expensive implementation of AbstractQueryPart.equals()
if (that instanceof TableImpl) {
TableImpl<?> other = (TableImpl<?>) that;
return StringUtils.equals(getSchema(), other.getSchema()) && getName().equals(other.getName());
}
return super.equals(that);
}
}