Compare commits

...

2 Commits

Author SHA1 Message Date
Vladimir Lagunov
5162945a42 JBR-8664 Optimize sun.nio.fs.WindowsPath.compareTo 2025-05-14 17:23:02 +02:00
Vladimir Lagunov
9d1b0c6bcc JBR-8664 Optimize sun.nio.fs.UnixPath.compareTo 2025-05-14 17:23:02 +02:00
2 changed files with 16 additions and 35 deletions

View File

@@ -37,6 +37,7 @@ import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.nio.file.spi.FileSystemProvider;
import java.util.Arrays;
import java.util.Objects;
import jdk.internal.access.JavaLangAccess;
@@ -718,23 +719,7 @@ class UnixPath implements Path {
@Override
public int compareTo(Path other) {
int len1 = path.length;
int len2 = ((UnixPath) other).path.length;
int n = Math.min(len1, len2);
byte v1[] = path;
byte v2[] = ((UnixPath) other).path;
int k = 0;
while (k < n) {
int c1 = v1[k] & 0xff;
int c2 = v2[k] & 0xff;
if (c1 != c2) {
return c1 - c2;
}
k++;
}
return len1 - len2;
return Arrays.compareUnsigned(path, ((UnixPath) other).path);
}
@Override

View File

@@ -64,6 +64,9 @@ class WindowsPath implements Path {
// paths and has a long path prefix for all paths longer than MAX_PATH.
private volatile WeakReference<String> pathForWin32Calls;
// Used for fast string comparison.
private volatile WeakReference<byte[]> uppercasePath;
// offsets into name components (computed lazily)
private volatile Integer[] offsets;
@@ -786,28 +789,12 @@ class WindowsPath implements Path {
}
return true;
}
@Override
public int compareTo(Path obj) {
if (obj == null)
throw new NullPointerException();
String s1 = path;
String s2 = ((WindowsPath)obj).path;
int n1 = s1.length();
int n2 = s2.length();
int min = Math.min(n1, n2);
for (int i = 0; i < min; i++) {
char c1 = s1.charAt(i);
char c2 = s2.charAt(i);
if (c1 != c2) {
c1 = Character.toUpperCase(c1);
c2 = Character.toUpperCase(c2);
if (c1 != c2) {
return c1 - c2;
}
}
}
return n1 - n2;
return Arrays.compareUnsigned(getUppercasePath(), ((WindowsPath)obj).getUppercasePath());
}
@Override
@@ -893,6 +880,15 @@ class WindowsPath implements Path {
}
}
private byte[] getUppercasePath() {
byte[] result = uppercasePath.get();
if (result == null) {
result = path.toUpperCase().getBytes();
uppercasePath = new WeakReference<>(result);
}
return result;
}
void checkRead() {
@SuppressWarnings("removal")
SecurityManager sm = System.getSecurityManager();