AlphaNumComparator: optimize usage and resolve signature problems

- Methods had ambiguous signatures (parameterized compare<T> overlapped the various typed compare()). This made the compiler very confused.
 - In several places throughout the app, AlphaNumComparator was getting repeatedly instantiated just to manually call a typed compare(). This was very wasteful

To solve both problems, the typed compare() methods have been been made static.

The performance gains of not instantiating the comparator repeatedly are very noticeable. In a test loading a local folder for upload with 1000 files in it,
time of load is reduced from 28 seconds to 17 seconds.

Signed-off-by: Álvaro Brey Vilas <alvaro.brey@nextcloud.com>
This commit is contained in:
Álvaro Brey Vilas 2022-01-26 19:00:56 +01:00
parent 462da35fdb
commit 2baf63d809
No known key found for this signature in database
GPG key ID: 2585783189A62105
4 changed files with 16 additions and 16 deletions

View file

@ -419,13 +419,13 @@ public class TestSorting {
if (sortedList.get(0) instanceof OCFile) { if (sortedList.get(0) instanceof OCFile) {
Collections.sort(unsortedList, (Comparator<OCFile>) (o1, o2) -> { Collections.sort(unsortedList, (Comparator<OCFile>) (o1, o2) -> {
if (o1.isFolder() && o2.isFolder()) { if (o1.isFolder() && o2.isFolder()) {
return new AlphanumComparator().compare(o1, o2); return AlphanumComparator.compare(o1, o2);
} else if (o1.isFolder()) { } else if (o1.isFolder()) {
return -1; return -1;
} else if (o2.isFolder()) { } else if (o2.isFolder()) {
return 1; return 1;
} }
return new AlphanumComparator().compare(o1, o2); return AlphanumComparator.compare(o1, o2);
}); });
} else { } else {
Collections.sort(unsortedList, new AlphanumComparator<>()); Collections.sort(unsortedList, new AlphanumComparator<>());

View file

@ -478,13 +478,13 @@ public class OCFile implements Parcelable, Comparable<OCFile>, ServerFileInterfa
@Override @Override
public int compareTo(@NonNull OCFile another) { public int compareTo(@NonNull OCFile another) {
if (isFolder() && another.isFolder()) { if (isFolder() && another.isFolder()) {
return new AlphanumComparator().compare(this, another); return AlphanumComparator.compare(this, another);
} else if (isFolder()) { } else if (isFolder()) {
return -1; return -1;
} else if (another.isFolder()) { } else if (another.isFolder()) {
return 1; return 1;
} }
return new AlphanumComparator().compare(this, another); return AlphanumComparator.compare(this, another);
} }
@Override @Override

View file

@ -51,13 +51,13 @@ public class FileSortOrderByName extends FileSortOrder {
Collections.sort(files, (o1, o2) -> { Collections.sort(files, (o1, o2) -> {
if (o1.isFolder() && o2.isFolder()) { if (o1.isFolder() && o2.isFolder()) {
return multiplier * new AlphanumComparator().compare(o1, o2); return multiplier * AlphanumComparator.compare(o1, o2);
} else if (o1.isFolder()) { } else if (o1.isFolder()) {
return -1; return -1;
} else if (o2.isFolder()) { } else if (o2.isFolder()) {
return 1; return 1;
} }
return multiplier * new AlphanumComparator().compare(o1, o2); return multiplier * AlphanumComparator.compare(o1, o2);
}); });
return super.sortCloudFiles(files); return super.sortCloudFiles(files);
@ -75,13 +75,13 @@ public class FileSortOrderByName extends FileSortOrder {
Collections.sort(files, (o1, o2) -> { Collections.sort(files, (o1, o2) -> {
if (o1.isFolder() && o2.isFolder()) { if (o1.isFolder() && o2.isFolder()) {
return multiplier * new AlphanumComparator().compare(o1, o2); return multiplier * AlphanumComparator.compare(o1, o2);
} else if (o1.isFolder()) { } else if (o1.isFolder()) {
return -1; return -1;
} else if (o2.isFolder()) { } else if (o2.isFolder()) {
return 1; return 1;
} }
return multiplier * new AlphanumComparator().compare(o1, o2); return multiplier * AlphanumComparator.compare(o1, o2);
}); });
return super.sortTrashbinFiles(files); return super.sortTrashbinFiles(files);
@ -105,7 +105,7 @@ public class FileSortOrderByName extends FileSortOrder {
} else if (o2.isDirectory()) { } else if (o2.isDirectory()) {
return 1; return 1;
} }
return multiplier * new AlphanumComparator().compare(o1.getPath() return multiplier * AlphanumComparator.compare(o1.getPath()
.toLowerCase(Locale.getDefault()), .toLowerCase(Locale.getDefault()),
o2.getPath().toLowerCase(Locale.getDefault())); o2.getPath().toLowerCase(Locale.getDefault()));
}); });

View file

@ -49,18 +49,18 @@ import java.util.Comparator;
* by Tobias Kaminsky * by Tobias Kaminsky
*/ */
public class AlphanumComparator<T> implements Comparator<T>, Serializable { public class AlphanumComparator<T> implements Comparator<T>, Serializable {
private boolean isDigit(char ch) { private static boolean isDigit(char ch) {
return ch >= 48 && ch <= 57; return ch >= 48 && ch <= 57;
} }
private boolean isSpecialChar(char ch) { private static boolean isSpecialChar(char ch) {
return ch <= 47 || ch >= 58 && ch <= 64 || ch >= 91 && ch <= 96 || ch >= 123 && ch <= 126; return ch <= 47 || ch >= 58 && ch <= 64 || ch >= 91 && ch <= 96 || ch >= 123 && ch <= 126;
} }
/** /**
* Length of string is passed in for improved efficiency (only need to calculate it once) * Length of string is passed in for improved efficiency (only need to calculate it once)
**/ **/
private String getChunk(String string, int stringLength, int marker) { private static String getChunk(String string, int stringLength, int marker) {
StringBuilder chunk = new StringBuilder(); StringBuilder chunk = new StringBuilder();
char c = string.charAt(marker); char c = string.charAt(marker);
chunk.append(c); chunk.append(c);
@ -87,14 +87,14 @@ public class AlphanumComparator<T> implements Comparator<T>, Serializable {
return chunk.toString(); return chunk.toString();
} }
public int compare(ServerFileInterface o1, ServerFileInterface o2) { public static int compare(ServerFileInterface o1, ServerFileInterface o2) {
String s1 = o1.getFileName(); String s1 = o1.getFileName();
String s2 = o2.getFileName(); String s2 = o2.getFileName();
return compare(s1, s2); return compare(s1, s2);
} }
public int compare(File f1, File f2) { public static int compare(File f1, File f2) {
String s1 = f1.getPath(); String s1 = f1.getPath();
String s2 = f2.getPath(); String s2 = f2.getPath();
@ -105,7 +105,7 @@ public class AlphanumComparator<T> implements Comparator<T>, Serializable {
return compare(t1.toString(), t2.toString()); return compare(t1.toString(), t2.toString());
} }
public int compare(String s1, String s2) { public static int compare(String s1, String s2) {
int thisMarker = 0; int thisMarker = 0;
int thatMarker = 0; int thatMarker = 0;
int s1Length = s1.length(); int s1Length = s1.length();