Skip to content

Commit aadd54a

Browse files
committed
Few performance optimization based on Gemma profiling
Avoid using getLabel() when hashing or comparing ontology resources unless both URIs are null. Lazily cache a label in getLabel(). Ensure that parents or children are collected in a set.
1 parent ef94679 commit aadd54a

4 files changed

Lines changed: 44 additions & 36 deletions

File tree

src/ubic/basecode/ontology/jena/AbstractOntologyResource.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ abstract class AbstractOntologyResource implements OntologyResource {
4545
@Nullable
4646
private final Double score;
4747

48+
private String _label;
49+
private boolean _isLabelNull = false;
50+
4851
protected AbstractOntologyResource( OntResource resource ) {
4952
this.res = resource;
5053
this.score = null;
@@ -67,10 +70,15 @@ public String getLocalName() {
6770

6871
@Override
6972
public String getLabel() {
73+
if ( _label != null || _isLabelNull ) {
74+
return _label;
75+
}
7076
String label = res.getLabel( "EN" );
7177
if ( label == null ) {
7278
label = res.getLabel( null );
7379
}
80+
_label = label;
81+
_isLabelNull = label == null;
7482
return label;
7583
}
7684

@@ -104,19 +112,20 @@ public int compareTo( OntologyResource other ) {
104112
public boolean equals( Object obj ) {
105113
if ( this == obj ) return true;
106114
if ( obj == null ) return false;
107-
if ( getClass() != obj.getClass() ) return false;
115+
if ( !( obj instanceof OntologyResource ) ) {
116+
return false;
117+
}
108118
final OntologyResource other = ( OntologyResource ) obj;
109-
if ( getLabel() == null ) {
110-
if ( other.getLabel() != null ) return false;
111-
} else if ( !getLabel().equals( other.getLabel() ) ) return false;
112-
if ( getUri() == null ) {
113-
return other.getUri() == null;
114-
} else return getUri().equals( other.getUri() );
119+
if ( getUri() == null && other.getUri() == null ) {
120+
return Objects.equals( getLabel(), other.getLabel() );
121+
} else {
122+
return Objects.equals( getUri(), other.getUri() );
123+
}
115124
}
116125

117126
@Override
118127
public int hashCode() {
119-
return Objects.hash( getLabel(), getUri() );
128+
return Objects.hash( getUri() );
120129
}
121130

122131
@Override

src/ubic/basecode/ontology/jena/AnnotationPropertyImpl.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import ubic.basecode.ontology.model.AnnotationProperty;
2626

2727
import javax.annotation.Nullable;
28+
import java.util.Objects;
2829

2930
/**
3031
* Note that this is a concrete instance of the annotation.
@@ -47,20 +48,6 @@ public AnnotationPropertyImpl( com.hp.hpl.jena.ontology.AnnotationProperty prop,
4748
this.object = object;
4849
}
4950

50-
@Override
51-
public boolean equals( @Nullable Object obj ) {
52-
if ( this == obj ) return true;
53-
if ( obj == null ) return false;
54-
if ( getClass() != obj.getClass() ) return false;
55-
final AnnotationPropertyImpl other = ( AnnotationPropertyImpl ) obj;
56-
if ( object == null ) {
57-
if ( other.object != null ) return false;
58-
} else if ( !object.equals( other.object ) ) return false;
59-
if ( property == null ) {
60-
return other.property == null;
61-
} else return property.equals( other.property );
62-
}
63-
6451
@Override
6552
public String getProperty() {
6653
if ( property.getLabel( null ) != null ) {
@@ -95,13 +82,25 @@ public boolean isObsolete() {
9582
return super.isObsolete() || property.hasSuperProperty( OBO.ObsoleteProperty, false );
9683
}
9784

85+
@Override
86+
public boolean equals( @Nullable Object obj ) {
87+
if ( this == obj ) return true;
88+
if ( obj == null ) return false;
89+
if ( obj instanceof AnnotationPropertyImpl ) {
90+
final AnnotationPropertyImpl other = ( AnnotationPropertyImpl ) obj;
91+
return super.equals( other )
92+
&& Objects.equals( object, other.object );
93+
} else if ( obj instanceof AnnotationProperty ) {
94+
final AnnotationProperty other = ( AnnotationProperty ) obj;
95+
return super.equals( other )
96+
&& Objects.equals( getContents(), other.getContents() );
97+
}
98+
return false;
99+
}
100+
98101
@Override
99102
public int hashCode() {
100-
final int PRIME = 31;
101-
int result = 1;
102-
result = PRIME * result + ( ( object == null ) ? 0 : object.hashCode() );
103-
result = PRIME * result + ( ( property == null ) ? 0 : property.hashCode() );
104-
return result;
103+
return Objects.hash( super.hashCode(), object );
105104
}
106105

107106
@Override

src/ubic/basecode/ontology/jena/JenaUtils.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ private static Collection<OntClass> getParentsInternal( OntModel model, Collecti
4646
.map( t -> as( t, OntClass.class ) )
4747
.filter( Optional::isPresent )
4848
.map( Optional::get )
49-
.collect( Collectors.toList() );
49+
.collect( Collectors.toSet() );
5050
if ( ontClasses.isEmpty() ) {
5151
return Collections.emptySet();
5252
}
@@ -104,18 +104,18 @@ public static Collection<OntClass> getChildren( OntModel model, Collection<OntCl
104104
}
105105

106106
public static Collection<OntClass> getChildrenInternal( OntModel model, Collection<OntClass> terms, boolean direct, @Nullable Set<Restriction> additionalRestrictions ) {
107-
terms = terms.stream()
107+
Set<OntClass> termsSet = terms.stream()
108108
.map( t -> t.inModel( model ) )
109109
.filter( t -> t.canAs( OntClass.class ) )
110110
.map( t -> as( t, OntClass.class ) )
111111
.filter( Optional::isPresent )
112112
.map( Optional::get )
113-
.collect( Collectors.toList() );
114-
if ( terms.isEmpty() ) {
113+
.collect( Collectors.toSet() );
114+
if ( termsSet.isEmpty() ) {
115115
return Collections.emptySet();
116116
}
117117
StopWatch timer = StopWatch.createStarted();
118-
Iterator<OntClass> it = terms.iterator();
118+
Iterator<OntClass> it = termsSet.iterator();
119119
ExtendedIterator<OntClass> iterator = it.next().listSubClasses( direct );
120120
while ( it.hasNext() ) {
121121
iterator = iterator.andThen( it.next().listSubClasses( direct ) );
@@ -132,7 +132,7 @@ public static Collection<OntClass> getChildrenInternal( OntModel model, Collecti
132132
subClassOf = ResourceFactory.createProperty( makeDirect( subClassOf.getURI() ) );
133133
}
134134
Set<Restriction> restrictions = UniqueExtendedIterator.create( additionalRestrictions.iterator() )
135-
.filterKeep( new RestrictionWithValuesFromFilter( terms ) )
135+
.filterKeep( new RestrictionWithValuesFromFilter( termsSet ) )
136136
.toSet();
137137
for ( Restriction r : restrictions ) {
138138
result.addAll( model.listResourcesWithProperty( subClassOf, r )

src/ubic/basecode/ontology/jena/RestrictionWithValuesFromFilter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
import com.hp.hpl.jena.rdf.model.Resource;
55
import com.hp.hpl.jena.util.iterator.Filter;
66

7-
import java.util.Collection;
7+
import java.util.Set;
88

99
/**
1010
* Match {@link Restriction} with values from any of the given resources.
1111
*/
1212
class RestrictionWithValuesFromFilter extends Filter<Restriction> {
1313

14-
private final Collection<? extends Resource> resource;
14+
private final Set<? extends Resource> resource;
1515

16-
public RestrictionWithValuesFromFilter( Collection<? extends Resource> resource ) {
16+
public RestrictionWithValuesFromFilter( Set<? extends Resource> resource ) {
1717
this.resource = resource;
1818
}
1919

0 commit comments

Comments
 (0)