Skip to content

Commit ac16473

Browse files
Sannebrmeyer
authored andcommitted
HHH-9003 Avoid allocating arrays in most methods of ComponentType
1 parent 64f6ea7 commit ac16473

File tree

2 files changed

+37
-43
lines changed

2 files changed

+37
-43
lines changed

hibernate-core/src/main/java/org/hibernate/tuple/entity/AbstractEntityTuplizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ protected Object getComponentValue(ComponentType type, Object component, String
677677
? propertyPath.substring( 0, loc )
678678
: propertyPath;
679679
final int index = findSubPropertyIndex( type, basePropertyName );
680-
final Object baseValue = type.getPropertyValue( component, index, getEntityMode() );
680+
final Object baseValue = type.getPropertyValue( component, index );
681681
if ( loc > 0 ) {
682682
if ( baseValue == null ) {
683683
return null;

hibernate-core/src/main/java/org/hibernate/type/ComponentType.java

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -182,52 +182,44 @@ public boolean isSame(Object x, Object y) throws HibernateException {
182182
}
183183

184184
@Override
185-
public boolean isEqual(Object x, Object y)
186-
throws HibernateException {
185+
public boolean isEqual(final Object x, final Object y) throws HibernateException {
187186
if ( x == y ) {
188187
return true;
189188
}
190189
if ( x == null || y == null ) {
191190
return false;
192191
}
193-
Object[] xvalues = getPropertyValues( x, entityMode );
194-
Object[] yvalues = getPropertyValues( y, entityMode );
195192
for ( int i = 0; i < propertySpan; i++ ) {
196-
if ( !propertyTypes[i].isEqual( xvalues[i], yvalues[i] ) ) {
193+
if ( !propertyTypes[i].isEqual( getPropertyValue( x, i ), getPropertyValue( y, i ) ) ) {
197194
return false;
198195
}
199196
}
200197
return true;
201198
}
202199

203200
@Override
204-
public boolean isEqual(Object x, Object y, SessionFactoryImplementor factory)
205-
throws HibernateException {
201+
public boolean isEqual(final Object x, final Object y, final SessionFactoryImplementor factory) throws HibernateException {
206202
if ( x == y ) {
207203
return true;
208204
}
209205
if ( x == null || y == null ) {
210206
return false;
211207
}
212-
Object[] xvalues = getPropertyValues( x, entityMode );
213-
Object[] yvalues = getPropertyValues( y, entityMode );
214208
for ( int i = 0; i < propertySpan; i++ ) {
215-
if ( !propertyTypes[i].isEqual( xvalues[i], yvalues[i], factory ) ) {
209+
if ( !propertyTypes[i].isEqual( getPropertyValue( x, i ), getPropertyValue( y, i ), factory ) ) {
216210
return false;
217211
}
218212
}
219213
return true;
220214
}
221215

222216
@Override
223-
public int compare(Object x, Object y) {
217+
public int compare(final Object x, final Object y) {
224218
if ( x == y ) {
225219
return 0;
226220
}
227-
Object[] xvalues = getPropertyValues( x, entityMode );
228-
Object[] yvalues = getPropertyValues( y, entityMode );
229221
for ( int i = 0; i < propertySpan; i++ ) {
230-
int propertyCompare = propertyTypes[i].compare( xvalues[i], yvalues[i] );
222+
int propertyCompare = propertyTypes[i].compare( getPropertyValue( x, i ), getPropertyValue( y, i ) );
231223
if ( propertyCompare != 0 ) {
232224
return propertyCompare;
233225
}
@@ -240,11 +232,10 @@ public boolean isMethodOf(Method method) {
240232
}
241233

242234
@Override
243-
public int getHashCode(Object x) {
235+
public int getHashCode(final Object x) {
244236
int result = 17;
245-
Object[] values = getPropertyValues( x, entityMode );
246237
for ( int i = 0; i < propertySpan; i++ ) {
247-
Object y = values[i];
238+
Object y = getPropertyValue( x, i );
248239
result *= 37;
249240
if ( y != null ) {
250241
result += propertyTypes[i].getHashCode( y );
@@ -254,11 +245,10 @@ public int getHashCode(Object x) {
254245
}
255246

256247
@Override
257-
public int getHashCode(Object x, SessionFactoryImplementor factory) {
248+
public int getHashCode(final Object x, final SessionFactoryImplementor factory) {
258249
int result = 17;
259-
Object[] values = getPropertyValues( x, entityMode );
260250
for ( int i = 0; i < propertySpan; i++ ) {
261-
Object y = values[i];
251+
Object y = getPropertyValue( x, i );
262252
result *= 37;
263253
if ( y != null ) {
264254
result += propertyTypes[i].getHashCode( y, factory );
@@ -268,48 +258,42 @@ public int getHashCode(Object x, SessionFactoryImplementor factory) {
268258
}
269259

270260
@Override
271-
public boolean isDirty(Object x, Object y, SessionImplementor session)
272-
throws HibernateException {
261+
public boolean isDirty(final Object x, final Object y, final SessionImplementor session) throws HibernateException {
273262
if ( x == y ) {
274263
return false;
275264
}
276265
if ( x == null || y == null ) {
277266
return true;
278267
}
279-
Object[] xvalues = getPropertyValues( x, entityMode );
280-
Object[] yvalues = getPropertyValues( y, entityMode );
281-
for ( int i = 0; i < xvalues.length; i++ ) {
282-
if ( propertyTypes[i].isDirty( xvalues[i], yvalues[i], session ) ) {
268+
for ( int i = 0; i < propertySpan; i++ ) {
269+
if ( propertyTypes[i].isDirty( getPropertyValue( x, i ), getPropertyValue( y, i ), session ) ) {
283270
return true;
284271
}
285272
}
286273
return false;
287274
}
288275

289-
public boolean isDirty(Object x, Object y, boolean[] checkable, SessionImplementor session)
290-
throws HibernateException {
276+
public boolean isDirty(final Object x, final Object y, final boolean[] checkable, final SessionImplementor session) throws HibernateException {
291277
if ( x == y ) {
292278
return false;
293279
}
294280
if ( x == null || y == null ) {
295281
return true;
296282
}
297-
Object[] xvalues = getPropertyValues( x, entityMode );
298-
Object[] yvalues = getPropertyValues( y, entityMode );
299283
int loc = 0;
300-
for ( int i = 0; i < xvalues.length; i++ ) {
284+
for ( int i = 0; i < propertySpan; i++ ) {
301285
int len = propertyTypes[i].getColumnSpan( session.getFactory() );
302286
if ( len <= 1 ) {
303287
final boolean dirty = ( len == 0 || checkable[loc] ) &&
304-
propertyTypes[i].isDirty( xvalues[i], yvalues[i], session );
288+
propertyTypes[i].isDirty( getPropertyValue( x, i ), getPropertyValue( y, i ), session );
305289
if ( dirty ) {
306290
return true;
307291
}
308292
}
309293
else {
310294
boolean[] subcheckable = new boolean[len];
311295
System.arraycopy( checkable, loc, subcheckable, 0, len );
312-
final boolean dirty = propertyTypes[i].isDirty( xvalues[i], yvalues[i], subcheckable, session );
296+
final boolean dirty = propertyTypes[i].isDirty( getPropertyValue( x, i ), getPropertyValue( y, i ), subcheckable, session );
313297
if ( dirty ) {
314298
return true;
315299
}
@@ -320,23 +304,20 @@ public boolean isDirty(Object x, Object y, boolean[] checkable, SessionImplement
320304
}
321305

322306
@Override
323-
public boolean isModified(Object old, Object current, boolean[] checkable, SessionImplementor session)
324-
throws HibernateException {
325-
307+
public boolean isModified(final Object old, final Object current, final boolean[] checkable, final SessionImplementor session) throws HibernateException {
326308
if ( current == null ) {
327309
return old != null;
328310
}
329311
if ( old == null ) {
330312
return true;
331313
}
332-
Object[] currentValues = getPropertyValues( current, session );
333314
Object[] oldValues = ( Object[] ) old;
334315
int loc = 0;
335-
for ( int i = 0; i < currentValues.length; i++ ) {
316+
for ( int i = 0; i < propertySpan; i++ ) {
336317
int len = propertyTypes[i].getColumnSpan( session.getFactory() );
337318
boolean[] subcheckable = new boolean[len];
338319
System.arraycopy( checkable, loc, subcheckable, 0, len );
339-
if ( propertyTypes[i].isModified( oldValues[i], currentValues[i], subcheckable, session ) ) {
320+
if ( propertyTypes[i].isModified( oldValues[i], getPropertyValue( current, i ), subcheckable, session ) ) {
340321
return true;
341322
}
342323
loc += len;
@@ -410,13 +391,26 @@ public Object nullSafeGet(ResultSet rs, String name, SessionImplementor session,
410391
@Override
411392
public Object getPropertyValue(Object component, int i, SessionImplementor session)
412393
throws HibernateException {
413-
return getPropertyValue( component, i, entityMode );
394+
return getPropertyValue( component, i );
414395
}
415-
416396
public Object getPropertyValue(Object component, int i, EntityMode entityMode)
417397
throws HibernateException {
418-
return componentTuplizer.getPropertyValue( component, i );
398+
return getPropertyValue( component, i );
419399
}
400+
401+
public Object getPropertyValue(Object component, int i)
402+
throws HibernateException {
403+
if ( component instanceof Object[] ) {
404+
// A few calls to hashCode pass the property values already in an
405+
// Object[] (ex: QueryKey hash codes for cached queries).
406+
// It's easiest to just check for the condition here prior to
407+
// trying reflection.
408+
return (( Object[] ) component)[i];
409+
} else {
410+
return componentTuplizer.getPropertyValue( component, i );
411+
}
412+
}
413+
420414
@Override
421415
public Object[] getPropertyValues(Object component, SessionImplementor session)
422416
throws HibernateException {

0 commit comments

Comments
 (0)