Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Hhh 7869 #426

Closed
wants to merge 2 commits into from

1 participant

@stliu
Collaborator

improvement the performance of org.hibernate.internal.util.StringHelper#firstIndexOfChar

@stliu
Collaborator

pushed

@stliu stliu closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 17, 2012
  1. @stliu

    HHH-7869 bad performance problem with org.hibernate.internal.util.Str…

    stliu authored
    …ingHelper#firstIndexOfChar
  2. @stliu
This page is out of date. Refresh to see the latest.
View
16 hibernate-core/src/main/java/org/hibernate/engine/query/spi/HQLQueryPlan.java
@@ -309,24 +309,20 @@ private ParameterMetadata buildParameterMetadata(ParameterTranslations parameter
);
}
- Iterator itr = recognizer.getNamedParameterDescriptionMap().entrySet().iterator();
Map<String, NamedParameterDescriptor> namedParamDescriptorMap = new HashMap<String, NamedParameterDescriptor>();
- while( itr.hasNext() ) {
- final Map.Entry entry = ( Map.Entry ) itr.next();
- final String name = ( String ) entry.getKey();
- final ParamLocationRecognizer.NamedParameterDescription description =
- ( ParamLocationRecognizer.NamedParameterDescription ) entry.getValue();
+ Map<String, ParamLocationRecognizer.NamedParameterDescription> map = recognizer.getNamedParameterDescriptionMap();
+ for ( final String name : map.keySet() ) {
+ final ParamLocationRecognizer.NamedParameterDescription description = map.get( name );
namedParamDescriptorMap.put(
name,
new NamedParameterDescriptor(
name,
- parameterTranslations.getNamedParameterExpectedType( name ),
- description.buildPositionsArray(),
- description.isJpaStyle()
+ parameterTranslations.getNamedParameterExpectedType( name ),
+ description.buildPositionsArray(),
+ description.isJpaStyle()
)
);
}
-
return new ParameterMetadata( ordinalParamDescriptors, namedParamDescriptorMap );
}
public QueryTranslator[] getTranslators() {
View
22 hibernate-core/src/main/java/org/hibernate/engine/query/spi/ParamLocationRecognizer.java
@@ -41,7 +41,7 @@
public static class NamedParameterDescription {
private final boolean jpaStyle;
- private final List positions = new ArrayList();
+ private final List<Integer> positions = new ArrayList<Integer>();
public NamedParameterDescription(boolean jpaStyle) {
this.jpaStyle = jpaStyle;
@@ -60,8 +60,8 @@ private void add(int position) {
}
}
- private Map namedParameterDescriptions = new HashMap();
- private List ordinalParameterLocationList = new ArrayList();
+ private Map<String, NamedParameterDescription> namedParameterDescriptions = new HashMap<String, NamedParameterDescription>();
+ private List<Integer> ordinalParameterLocationList = new ArrayList<Integer>();
/**
* Convenience method for creating a param location recognizer and
@@ -82,7 +82,7 @@ public static ParamLocationRecognizer parseLocations(String query) {
*
* @return The map of named parameter locations.
*/
- public Map getNamedParameterDescriptionMap() {
+ public Map<String, NamedParameterDescription> getNamedParameterDescriptionMap() {
return namedParameterDescriptions;
}
@@ -94,38 +94,38 @@ public Map getNamedParameterDescriptionMap() {
*
* @return The list of ordinal parameter locations.
*/
- public List getOrdinalParameterLocationList() {
+ public List<Integer> getOrdinalParameterLocationList() {
return ordinalParameterLocationList;
}
// Recognition code ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
+ @Override
public void ordinalParameter(int position) {
ordinalParameterLocationList.add( position );
}
-
+ @Override
public void namedParameter(String name, int position) {
getOrBuildNamedParameterDescription( name, false ).add( position );
}
-
+ @Override
public void jpaPositionalParameter(String name, int position) {
getOrBuildNamedParameterDescription( name, true ).add( position );
}
private NamedParameterDescription getOrBuildNamedParameterDescription(String name, boolean jpa) {
- NamedParameterDescription desc = ( NamedParameterDescription ) namedParameterDescriptions.get( name );
+ NamedParameterDescription desc = namedParameterDescriptions.get( name );
if ( desc == null ) {
desc = new NamedParameterDescription( jpa );
namedParameterDescriptions.put( name, desc );
}
return desc;
}
-
+ @Override
public void other(char character) {
// don't care...
}
-
+ @Override
public void outParameter(int position) {
// don't care...
}
View
2  hibernate-core/src/main/java/org/hibernate/engine/query/spi/ParameterParser.java
@@ -88,7 +88,7 @@ else if ( '\\' == c ) {
else {
if ( c == ':' ) {
// named parameter
- int right = StringHelper.firstIndexOfChar( sqlString, ParserHelper.HQL_SEPARATORS, indx + 1 );
+ int right = StringHelper.firstIndexOfChar( sqlString, ParserHelper.HQL_SEPARATORS_BITSET, indx + 1 );
int chopLocation = right < 0 ? sqlString.length() : right;
String param = sqlString.substring( indx + 1, chopLocation );
if ( StringHelper.isEmpty( param ) ) {
View
24 hibernate-core/src/main/java/org/hibernate/engine/query/spi/QueryPlanCache.java
@@ -139,23 +139,23 @@ private ParameterMetadata buildParameterMetadata(String query){
final int size = recognizer.getOrdinalParameterLocationList().size();
OrdinalParameterDescriptor[] ordinalDescriptors = new OrdinalParameterDescriptor[ size ];
for ( int i = 0; i < size; i++ ) {
- final Integer position = ( Integer ) recognizer.getOrdinalParameterLocationList().get( i );
+ final Integer position = recognizer.getOrdinalParameterLocationList().get( i );
ordinalDescriptors[i] = new OrdinalParameterDescriptor( i, null, position );
}
-
- Iterator itr = recognizer.getNamedParameterDescriptionMap().entrySet().iterator();
- Map<String,NamedParameterDescriptor> namedParamDescriptorMap = new HashMap<String,NamedParameterDescriptor>();
- while( itr.hasNext() ) {
- final Map.Entry entry = ( Map.Entry ) itr.next();
- final String name = ( String ) entry.getKey();
- final ParamLocationRecognizer.NamedParameterDescription description =
- ( ParamLocationRecognizer.NamedParameterDescription ) entry.getValue();
+ Map<String, NamedParameterDescriptor> namedParamDescriptorMap = new HashMap<String, NamedParameterDescriptor>();
+ Map<String, ParamLocationRecognizer.NamedParameterDescription> map = recognizer.getNamedParameterDescriptionMap();
+ for ( final String name : map.keySet() ) {
+ final ParamLocationRecognizer.NamedParameterDescription description = map.get( name );
namedParamDescriptorMap.put(
- name ,
- new NamedParameterDescriptor( name, null, description.buildPositionsArray(), description.isJpaStyle() )
+ name,
+ new NamedParameterDescriptor(
+ name,
+ null,
+ description.buildPositionsArray(),
+ description.isJpaStyle()
+ )
);
}
-
return new ParameterMetadata( ordinalDescriptors, namedParamDescriptorMap );
}
View
7 hibernate-core/src/main/java/org/hibernate/hql/internal/classic/ParserHelper.java
@@ -23,6 +23,7 @@
*
*/
package org.hibernate.hql.internal.classic;
+import java.util.BitSet;
import java.util.StringTokenizer;
import org.hibernate.QueryException;
@@ -33,6 +34,12 @@
public static final String HQL_VARIABLE_PREFIX = ":";
public static final String HQL_SEPARATORS = " \n\r\f\t,()=<>&|+-=/*'^![]#~\\";
+ public static final BitSet HQL_SEPARATORS_BITSET = new BitSet( );
+ static {
+ for(int i=0;i<HQL_SEPARATORS.length();i++){
+ HQL_SEPARATORS_BITSET.set( HQL_SEPARATORS.charAt( i ) );
+ }
+ }
//NOTICE: no " or . since they are part of (compound) identifiers
public static final String PATH_SEPARATORS = ".";
View
27 hibernate-core/src/main/java/org/hibernate/internal/util/StringHelper.java
@@ -27,6 +27,7 @@
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.BitSet;
import java.util.Iterator;
import java.util.StringTokenizer;
@@ -471,21 +472,23 @@ public static String qualify(String prefix, String name) {
}
return qualified;
}
+ public static int firstIndexOfChar(String sqlString, BitSet keys, int startindex) {
+ for ( int i = startindex, size = sqlString.length(); i < size; i++ ) {
+ if ( keys.get( sqlString.charAt( i ) ) ) {
+ return i;
+ }
+ }
+ return -1;
+
+ }
public static int firstIndexOfChar(String sqlString, String string, int startindex) {
- int matchAt = -1;
- for ( int i = 0; i < string.length(); i++ ) {
- int curMatch = sqlString.indexOf( string.charAt( i ), startindex );
- if ( curMatch >= 0 ) {
- if ( matchAt == -1 ) { // first time we find match!
- matchAt = curMatch;
- }
- else {
- matchAt = Math.min( matchAt, curMatch );
- }
- }
+ BitSet keys = new BitSet();
+ for ( int i = 0, size = string.length(); i < size; i++ ) {
+ keys.set( string.charAt( i ) );
}
- return matchAt;
+ return firstIndexOfChar( sqlString, keys, startindex );
+
}
public static String truncate(String string, int length) {
Something went wrong with that request. Please try again.