Skip to content

Commit

Permalink
perf: Avoid collection copies in CtPackageImpl "empty" checks (#4848)
Browse files Browse the repository at this point in the history
  • Loading branch information
SirYwell authored Aug 23, 2022
1 parent 65ab20b commit 844beaa
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/main/java/spoon/IncrementalLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public IncrementalLauncher(Set<File> inputResources, Set<String> sourceClasspath

Collection<CtPackage> oldPackages = oldFactory.Package().getAll();
for (CtPackage pkg : oldPackages) {
if (pkg.getTypes().isEmpty() && pkg.getPackages().isEmpty() && !pkg.isUnnamedPackage()) {
if (pkg.isEmpty() && !pkg.isUnnamedPackage()) {
pkg.delete();
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/spoon/reflect/declaration/CtPackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public interface CtPackage extends CtNamedElement, CtShadowable {

/**
* Gets the set of included child packages.
* This method might take linear time (regarding the amount of packages in this package).
* For emptiness-checks, {@link #hasPackages()} should be preferred.
*/
@PropertyGetter(role = SUB_PACKAGE)
Set<CtPackage> getPackages();
Expand Down Expand Up @@ -91,6 +93,8 @@ public interface CtPackage extends CtNamedElement, CtShadowable {

/**
* Returns the set of the top-level types in this package.
* This method might take linear time (regarding the amount of types in this package).
* For emptiness-checks, {@link #hasTypes()} should be preferred.
*/
@PropertyGetter(role = CONTAINED_TYPE)
Set<CtType<?>> getTypes();
Expand Down Expand Up @@ -161,4 +165,24 @@ public interface CtPackage extends CtNamedElement, CtShadowable {
* @return true if the package contains no types nor any other packages
*/
boolean isEmpty();

/**
* Returns true if this package contains any types.
* This method is expected to provide constant-time performance
* and should be preferred over {@link #getTypes()}{@code .isEmpty()}.
*
* @return true if the package contains any types.
* @see #getTypes()
*/
boolean hasTypes();

/**
* Returns true if this package contains any sub-packages.
* This method is expected to provide constant-time performance
* and should be preferred over {@link #getPackages()}{@code .isEmpty()}.
*
* @return true if the package contains any sub-packages
* @see #getPackages()
*/
boolean hasPackages();
}
2 changes: 1 addition & 1 deletion src/main/java/spoon/reflect/factory/PackageFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public CtPackage get(String qualifiedName) {
continue;
}
lastNonNullPackage = aPackage;
if (!aPackage.getTypes().isEmpty()) {
if (aPackage.hasTypes()) {
packageWithTypes = aPackage;
foundPackageCount++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,17 @@ public boolean hasPackageInfo() {

@Override
public boolean isEmpty() {
return getPackages().isEmpty() && getTypes().isEmpty();
return !hasPackages() && !hasTypes();
}

@Override
public boolean hasTypes() {
return !types.isEmpty();
}

@Override
public boolean hasPackages() {
return !packs.isEmpty();
}

void updateTypeName(CtType<?> newType, String oldName) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/spoon/support/util/internal/ElementNameMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ public T remove(Object key) {
return removed;
}

@Override
public boolean isEmpty() {
return map.isEmpty();
}

@Override
public void clear() {
if (map.isEmpty()) {
Expand Down

0 comments on commit 844beaa

Please sign in to comment.