Skip to content

Commit

Permalink
HHH-15184 Improve efficiency of Component#getSelectables()
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanne committed Apr 6, 2022
1 parent 84cf452 commit 25aec8d
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions hibernate-core/src/main/java/org/hibernate/mapping/Component.java
Expand Up @@ -59,6 +59,9 @@ public class Component extends SimpleValue implements MetaAttributable, Sortable
// cache the status of the type
private volatile Type type;

// lazily computed based on 'properties' field: invalidate by setting to null when properties are modified
private List<Selectable> cachedSelectables;

public Component(MetadataBuildingContext metadata, PersistentClass owner) throws MappingException {
this( metadata, owner.getTable(), owner );
}
Expand Down Expand Up @@ -117,6 +120,11 @@ public List<Property> getProperties() {

public void addProperty(Property p) {
properties.add( p );
propertiesListModified();
}

private void propertiesListModified() {
cachedSelectables = null;
}

@Override
Expand Down Expand Up @@ -146,9 +154,15 @@ public Iterator<Selectable> getColumnIterator() {

@Override
public List<Selectable> getSelectables() {
return properties.stream()
.flatMap( p -> p.getSelectables().stream() )
.collect(Collectors.toList());
if ( cachedSelectables != null ) {
return cachedSelectables;
}
else {
cachedSelectables = properties.stream()
.flatMap( p -> p.getSelectables().stream() )
.collect( Collectors.toList() );
return cachedSelectables;
}
}

@Override
Expand Down Expand Up @@ -568,6 +582,7 @@ private int[] sortProperties(boolean forceRetainOriginalOrder) {
}
}
}
propertiesListModified();
return this.originalPropertyOrder = originalPropertyOrder;
}

Expand Down

0 comments on commit 25aec8d

Please sign in to comment.