Skip to content

Commit

Permalink
[#4646] Significant overhead from Scope.data() Map access
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaseder committed Oct 17, 2015
1 parent b84b488 commit ccae6aa
Show file tree
Hide file tree
Showing 28 changed files with 368 additions and 199 deletions.
2 changes: 1 addition & 1 deletion jOOQ/src/main/java/org/jooq/impl/AbstractContext.java
Expand Up @@ -41,7 +41,7 @@
package org.jooq.impl;

import static org.jooq.conf.ParamType.INDEXED;
import static org.jooq.impl.Utils.DATA_OMIT_CLAUSE_EVENT_EMISSION;
import static org.jooq.impl.Utils.DataKey.DATA_OMIT_CLAUSE_EVENT_EMISSION;

import java.sql.PreparedStatement;
import java.util.ArrayDeque;
Expand Down
18 changes: 9 additions & 9 deletions jOOQ/src/main/java/org/jooq/impl/AbstractQuery.java
Expand Up @@ -52,8 +52,8 @@
import static org.jooq.conf.SettingsTools.executePreparedStatements;
import static org.jooq.conf.SettingsTools.getParamType;
import static org.jooq.impl.DSL.using;
import static org.jooq.impl.Utils.DATA_COUNT_BIND_VALUES;
import static org.jooq.impl.Utils.DATA_FORCE_STATIC_STATEMENT;
import static org.jooq.impl.Utils.DataKey.DATA_COUNT_BIND_VALUES;
import static org.jooq.impl.Utils.DataKey.DATA_FORCE_STATIC_STATEMENT;
import static org.jooq.impl.Utils.consumeExceptions;

import java.sql.PreparedStatement;
Expand Down Expand Up @@ -483,13 +483,13 @@ private final String getSQL0(ExecuteContext ctx) {
x
x
xx xxxxxxxxxxxxxxxxxxxxxx x xxxxxxxx x
xxxxx xxx xxxxxxxxxxxxxxxxxxxxx xxxx xxxxxxxx x xxxxxxx xxxxxxx xxxx xxx xxxx xxxxx xxxxxxxx xxxxxx xxxxxxxx xxxxxxxxx xx x xxxxxxxxxx xxxxxxx xx xxxxxxx xxxxxxxxxxxxxxxxxxxxxx xx xxx xxxx xx xxx xxxx xxxxxxx xxxx xxxx xxxx xxxxxxxxx
x
xx xxxx x xxxxxxxxxxxxxxxxxxxxxxxxxxx x
xxxxx xxx xxxxxxxxxxxxxxxxxxxxxx xx xxx xxxxx xxxxxx xxx xxxxx xxxx xxxx xxxx xxxxxx xxxxxxxx xxxxxxxxx xx x xxxxxxxxxx xxxxxxx xx xxxxxxx xxxxxxxxxxxxxxxxxxxxxx xx xxx xxxx xx xxxxxx xxxx xxxx xxxxxxxxx
x
xx xx xxxxxxxxxxxxxxxxxxxxxx x xxxxxxxx x
xx xxxxx xxx xxxxxxxxxxxxxxxxxxxxx xxxx xxxxxxxx x xxxxxxx xxxxxxx xxxx xxx xxxx xxxxx xxxxxxxx xxxxxx xxxxxxxx xxxxxxxxx xx x xxxxxxxxxx xxxxxxx xx xxxxxxx xxxxxxxxxxxxxxxxxxxxxx xx xxx xxxx xx xxx xxxx xxxxxxx xxxx xxxx xxxx xxxxxxxxx
xx x
xx
xx xx xxxx x xxxxxxxxxxxxxxxxxxxxxxxxxxx x
xx xxxxx xxx xxxxxxxxxxxxxxxxxxxxxx xx xxx xxxxx xxxxxx xxx xxxxx xxxx xxxx xxxx xxxxxx xxxxxxxx xxxxxxxxx xx x xxxxxxxxxx xxxxxxx xx xxxxxxx xxxxxxxxxxxxxxxxxxxxxx xx xxx xxxx xx xxxxxx xxxx xxxx xxxxxxxxx
xx x
xx xxxxxxxx xx xx [/pro] */
return result;
Expand Down
2 changes: 1 addition & 1 deletion jOOQ/src/main/java/org/jooq/impl/AbstractResultQuery.java
Expand Up @@ -48,7 +48,7 @@
import static org.jooq.SQLDialect.CUBRID;
import static org.jooq.SQLDialect.POSTGRES;
// ...
import static org.jooq.impl.Utils.DATA_LOCK_ROWS_FOR_UPDATE;
import static org.jooq.impl.Utils.DataKey.DATA_LOCK_ROWS_FOR_UPDATE;
import static org.jooq.impl.Utils.consumeResultSets;

import java.lang.reflect.Array;
Expand Down
3 changes: 1 addition & 2 deletions jOOQ/src/main/java/org/jooq/impl/AbstractScope.java
Expand Up @@ -40,7 +40,6 @@
*/
package org.jooq.impl;

import java.util.HashMap;
import java.util.Map;

import org.jooq.Configuration;
Expand Down Expand Up @@ -69,7 +68,7 @@ abstract class AbstractScope implements Scope {
}

if (data == null) {
data = new HashMap<Object, Object>();
data = new DataMap();
}

this.configuration = configuration;
Expand Down
2 changes: 1 addition & 1 deletion jOOQ/src/main/java/org/jooq/impl/Alias.java
Expand Up @@ -68,7 +68,7 @@
import static org.jooq.impl.DSL.falseCondition;
import static org.jooq.impl.DSL.field;
import static org.jooq.impl.DSL.select;
import static org.jooq.impl.Utils.DATA_UNALIAS_ALIASES_IN_ORDER_BY;
import static org.jooq.impl.Utils.DataKey.DATA_UNALIAS_ALIASES_IN_ORDER_BY;
import static org.jooq.impl.Utils.list;

import org.jooq.Clause;
Expand Down
2 changes: 1 addition & 1 deletion jOOQ/src/main/java/org/jooq/impl/AlterTableImpl.java
Expand Up @@ -55,7 +55,7 @@
import static org.jooq.impl.DSL.inline;
import static org.jooq.impl.DSL.name;
import static org.jooq.impl.DSL.sql;
import static org.jooq.impl.Utils.DATA_DROP_CONSTRAINT;
import static org.jooq.impl.Utils.DataKey.DATA_DROP_CONSTRAINT;
import static org.jooq.impl.Utils.toSQLDDLTypeDeclaration;

import org.jooq.AlterTableAlterStep;
Expand Down
3 changes: 2 additions & 1 deletion jOOQ/src/main/java/org/jooq/impl/BatchCRUD.java
Expand Up @@ -41,6 +41,7 @@
package org.jooq.impl;

import static org.jooq.conf.SettingsTools.executeStaticStatements;
import static org.jooq.impl.Utils.DataKey.DATA_OMIT_RETURNING_CLAUSE;

import java.util.ArrayList;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -111,7 +112,7 @@ private final int[] executePrepared() {
));

// [#1537] Communicate with UpdatableRecordImpl
local.data(Utils.DATA_OMIT_RETURNING_CLAUSE, true);
local.data(DATA_OMIT_RETURNING_CLAUSE, true);

// [#1529] Avoid DEBUG logging of single INSERT / UPDATE statements
local.settings().setExecuteLogging(false);
Expand Down
2 changes: 1 addition & 1 deletion jOOQ/src/main/java/org/jooq/impl/ConstraintImpl.java
Expand Up @@ -48,7 +48,7 @@
import static org.jooq.impl.ConstraintImpl.Action.SET_NULL;
import static org.jooq.impl.DSL.name;
import static org.jooq.impl.DSL.table;
import static org.jooq.impl.Utils.DATA_DROP_CONSTRAINT;
import static org.jooq.impl.Utils.DataKey.DATA_DROP_CONSTRAINT;
import static org.jooq.impl.Utils.fieldsByName;

import javax.annotation.Generated;
Expand Down
2 changes: 1 addition & 1 deletion jOOQ/src/main/java/org/jooq/impl/CreateTableImpl.java
Expand Up @@ -55,7 +55,7 @@
// ...
import static org.jooq.impl.DSL.field;
import static org.jooq.impl.DSL.name;
import static org.jooq.impl.Utils.DATA_SELECT_INTO_TABLE;
import static org.jooq.impl.Utils.DataKey.DATA_SELECT_INTO_TABLE;

import java.util.ArrayList;
import java.util.List;
Expand Down
2 changes: 1 addition & 1 deletion jOOQ/src/main/java/org/jooq/impl/CursorImpl.java
Expand Up @@ -41,7 +41,7 @@
package org.jooq.impl;

import static java.lang.Boolean.TRUE;
import static org.jooq.impl.Utils.DATA_LOCK_ROWS_FOR_UPDATE;
import static org.jooq.impl.Utils.DataKey.DATA_LOCK_ROWS_FOR_UPDATE;
import static org.jooq.impl.Utils.recordFactory;

import java.io.InputStream;
Expand Down
167 changes: 167 additions & 0 deletions jOOQ/src/main/java/org/jooq/impl/DataMap.java
@@ -0,0 +1,167 @@
/**
* Copyright (c) 2009-2015, Data Geekery GmbH (http://www.datageekery.com)
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* ASL 2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: http://www.jooq.org/licenses
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq.impl;

import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.jooq.Scope;
import org.jooq.impl.Utils.DataKey;

/**
* The {@link Map} implementation for use with {@link Scope#data()}.
*
* @author Lukas Eder
*/
class DataMap extends AbstractMap<Object, Object> {

final EnumMap<DataKey, Object> internal;
Map<Object, Object> external;
final Set<Entry<Object, Object>> entrySet;

DataMap() {
internal = new EnumMap<DataKey, Object>(DataKey.class);
entrySet = new EntrySet();
}

@Override
public final int size() {
return internal().size() + external(false).size();
}

@Override
public final boolean isEmpty() {
return internal().isEmpty() && external(false).isEmpty();
}

@Override
public final boolean containsKey(Object key) {
return delegate(key, false).containsKey(key);
}

@Override
public final boolean containsValue(Object value) {
return internal().containsValue(value) || external(false).containsValue(value);
}

@Override
public final Object get(Object key) {
return delegate(key, false).get(key);
}

@Override
public final Object put(Object key, Object value) {
return delegate(key, true).put(key, value);
}

@Override
public final Object remove(Object key) {
return delegate(key, true).remove(key);
}

@Override
public final void clear() {
internal().clear();
external(true).clear();
}

@Override
public final Set<Entry<Object, Object>> entrySet() {
return entrySet;
}

@SuppressWarnings({ "rawtypes", "unchecked" })
private final Map<Object, Object> internal() {
return (Map) internal;
}

private final Map<Object, Object> external(boolean initialise) {
if (external == null) {
if (initialise)
external = new HashMap<Object, Object>();
else
return Collections.emptyMap();
}

return external;
}

@SuppressWarnings({ "unchecked", "rawtypes" })
private final Map<Object, Object> delegate(Object key, boolean initialise) {
return key instanceof DataKey ? (Map) internal() : external(initialise);
}

private class EntrySet extends AbstractSet<Entry<Object, Object>> {
@Override
public final Iterator<Entry<Object, Object>> iterator() {
return new Iterator<Entry<Object, Object>>() {
final Iterator<Entry<Object, Object>> internalIterator = internal().entrySet().iterator();
final Iterator<Entry<Object, Object>> externalIterator = external(false).entrySet().iterator();

@Override
public final boolean hasNext() {
return internalIterator.hasNext() || externalIterator.hasNext();
}

@Override
public final Entry<Object, Object> next() {
return internalIterator.hasNext() ? internalIterator.next() : externalIterator.next();
}

@Override
public final void remove() {
throw new UnsupportedOperationException();
}
};
}

@Override
public final int size() {
return DataMap.this.size();
}
}
}
2 changes: 1 addition & 1 deletion jOOQ/src/main/java/org/jooq/impl/DefaultConfiguration.java
Expand Up @@ -41,7 +41,7 @@
package org.jooq.impl;

import static org.jooq.SQLDialect.DEFAULT;
import static org.jooq.impl.Utils.DATA_DEFAULT_TRANSACTION_PROVIDER_CONNECTION;
import static org.jooq.impl.Utils.DataKey.DATA_DEFAULT_TRANSACTION_PROVIDER_CONNECTION;

import java.io.IOException;
import java.io.ObjectInputStream;
Expand Down
3 changes: 1 addition & 2 deletions jOOQ/src/main/java/org/jooq/impl/DefaultExecuteContext.java
Expand Up @@ -51,7 +51,6 @@
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -371,7 +370,7 @@ static final Connection localTargetConnection() {

private DefaultExecuteContext(Configuration configuration, Query query, Query[] batchQueries, Routine<?> routine) {
this.configuration = configuration;
this.data = new HashMap<Object, Object>();
this.data = new DataMap();
this.query = query;
this.batchQueries = (batchQueries == null ? new Query[0] : batchQueries);
this.routine = routine;
Expand Down
13 changes: 6 additions & 7 deletions jOOQ/src/main/java/org/jooq/impl/DefaultRecordContext.java
Expand Up @@ -40,7 +40,6 @@
*/
package org.jooq.impl;

import java.util.HashMap;
import java.util.Map;

import org.jooq.Configuration;
Expand All @@ -58,16 +57,16 @@
*/
class DefaultRecordContext implements RecordContext {

private final Configuration configuration;
private final HashMap<Object, Object> data;
private final ExecuteType type;
private final Record[] records;
Exception exception;
private final Configuration configuration;
private final Map<Object, Object> data;
private final ExecuteType type;
private final Record[] records;
Exception exception;

DefaultRecordContext(Configuration configuration, ExecuteType type, Record... records) {
this.configuration = configuration;
this.type = type;
this.data = new HashMap<Object, Object>();
this.data = new DataMap();
this.records = records;
}

Expand Down
2 changes: 1 addition & 1 deletion jOOQ/src/main/java/org/jooq/impl/DefaultRenderContext.java
Expand Up @@ -51,7 +51,7 @@
import static org.jooq.impl.Identifiers.QUOTE_END_DELIMITER;
import static org.jooq.impl.Identifiers.QUOTE_END_DELIMITER_ESCAPED;
import static org.jooq.impl.Identifiers.QUOTE_START_DELIMITER;
import static org.jooq.impl.Utils.DATA_COUNT_BIND_VALUES;
import static org.jooq.impl.Utils.DataKey.DATA_COUNT_BIND_VALUES;

import java.util.ArrayDeque;
import java.util.Arrays;
Expand Down
Expand Up @@ -40,9 +40,9 @@
*/
package org.jooq.impl;

import static org.jooq.impl.Utils.DATA_DEFAULT_TRANSACTION_PROVIDER_AUTOCOMMIT;
import static org.jooq.impl.Utils.DATA_DEFAULT_TRANSACTION_PROVIDER_CONNECTION;
import static org.jooq.impl.Utils.DATA_DEFAULT_TRANSACTION_PROVIDER_SAVEPOINTS;
import static org.jooq.impl.Utils.DataKey.DATA_DEFAULT_TRANSACTION_PROVIDER_AUTOCOMMIT;
import static org.jooq.impl.Utils.DataKey.DATA_DEFAULT_TRANSACTION_PROVIDER_CONNECTION;
import static org.jooq.impl.Utils.DataKey.DATA_DEFAULT_TRANSACTION_PROVIDER_SAVEPOINTS;

import java.sql.Connection;
import java.sql.SQLException;
Expand Down
4 changes: 2 additions & 2 deletions jOOQ/src/main/java/org/jooq/impl/Function.java
Expand Up @@ -59,8 +59,8 @@
import static org.jooq.impl.Term.LIST_AGG;
import static org.jooq.impl.Term.MEDIAN;
import static org.jooq.impl.Term.ROW_NUMBER;
import static org.jooq.impl.Utils.DATA_LOCALLY_SCOPED_DATA_MAP;
import static org.jooq.impl.Utils.DATA_WINDOW_DEFINITIONS;
import static org.jooq.impl.Utils.DataKey.DATA_LOCALLY_SCOPED_DATA_MAP;
import static org.jooq.impl.Utils.DataKey.DATA_WINDOW_DEFINITIONS;

import java.math.BigDecimal;
import java.util.Arrays;
Expand Down
4 changes: 2 additions & 2 deletions jOOQ/src/main/java/org/jooq/impl/InternalVisitListener.java
Expand Up @@ -43,8 +43,8 @@
import static java.lang.Boolean.TRUE;
import static org.jooq.impl.DSL.field;
import static org.jooq.impl.DSL.name;
import static org.jooq.impl.Utils.DATA_OVERRIDE_ALIASES_IN_ORDER_BY;
import static org.jooq.impl.Utils.DATA_UNALIAS_ALIASES_IN_ORDER_BY;
import static org.jooq.impl.Utils.DataKey.DATA_OVERRIDE_ALIASES_IN_ORDER_BY;
import static org.jooq.impl.Utils.DataKey.DATA_UNALIAS_ALIASES_IN_ORDER_BY;

import org.jooq.Field;
import org.jooq.QueryPart;
Expand Down

0 comments on commit ccae6aa

Please sign in to comment.