Skip to content

Commit 417ea73

Browse files
committed
Static patterns rather than implicit, for another 10% improvement in performance.
Signed-off-by: cpw <cpw+github@weeksfamily.ca>
1 parent c8bc75e commit 417ea73

2 files changed

Lines changed: 39 additions & 16 deletions

File tree

src/main/java/cpw/mods/niofs/union/UnionFileSystem.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,30 @@
22

33
import java.io.FileNotFoundException;
44
import java.io.IOException;
5-
import java.io.UncheckedIOException;
65
import java.lang.invoke.MethodHandle;
76
import java.lang.invoke.MethodHandles;
87
import java.lang.invoke.MethodType;
98
import java.nio.channels.SeekableByteChannel;
10-
import java.nio.file.*;
9+
import java.nio.file.AccessMode;
10+
import java.nio.file.DirectoryStream;
11+
import java.nio.file.FileStore;
12+
import java.nio.file.FileSystem;
13+
import java.nio.file.FileSystems;
14+
import java.nio.file.Files;
15+
import java.nio.file.LinkOption;
16+
import java.nio.file.Path;
17+
import java.nio.file.PathMatcher;
18+
import java.nio.file.StandardOpenOption;
19+
import java.nio.file.WatchService;
1120
import java.nio.file.attribute.BasicFileAttributes;
1221
import java.nio.file.attribute.UserPrincipalLookupService;
13-
import java.util.*;
22+
import java.util.Collections;
23+
import java.util.Iterator;
24+
import java.util.LinkedHashSet;
25+
import java.util.List;
26+
import java.util.Map;
27+
import java.util.Optional;
28+
import java.util.Set;
1429
import java.util.function.BiPredicate;
1530
import java.util.function.Function;
1631
import java.util.stream.Collectors;
@@ -20,6 +35,8 @@
2035

2136
public class UnionFileSystem extends FileSystem {
2237
private static final MethodHandle ZIPFS_EXISTS;
38+
static final String SEP_STRING = "/";
39+
2340
static {
2441
try {
2542
var hackfield = MethodHandles.Lookup.class.getDeclaredField("IMPL_LOOKUP");
@@ -118,7 +135,7 @@ public boolean isReadOnly() {
118135

119136
@Override
120137
public String getSeparator() {
121-
return "/";
138+
return SEP_STRING;
122139
}
123140

124141
@Override

src/main/java/cpw/mods/niofs/union/UnionPath.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
import java.util.stream.IntStream;
1212

1313
public class UnionPath implements Path {
14+
private static final Pattern SEPARATOR_BEGIN_END;
15+
private static final Pattern SEPARATOR_DUPLICATES;
16+
17+
static {
18+
var sep = "(?:"+ Pattern.quote(UnionFileSystem.SEP_STRING) + ")";
19+
SEPARATOR_BEGIN_END = Pattern.compile("^" + sep + "*|" + sep + "*$");
20+
SEPARATOR_DUPLICATES = Pattern.compile(sep + "(?=" + sep + ")");
21+
}
1422
private final UnionFileSystem fileSystem;
1523
private final boolean absolute;
1624
private final String[] pathParts;
@@ -24,8 +32,10 @@ public class UnionPath implements Path {
2432
this.absolute = false;
2533
this.pathParts = new String[0];
2634
} else {
27-
final var longstring = Arrays.stream(pathParts).filter(part -> !part.isEmpty()).collect(Collectors.joining(this.getFileSystem().getSeparator()));
28-
this.absolute = longstring.startsWith(this.getFileSystem().getSeparator());
35+
final var longstring = Arrays.stream(pathParts)
36+
.filter(part -> !part.isEmpty())
37+
.collect(Collectors.joining(UnionFileSystem.SEP_STRING));
38+
this.absolute = longstring.startsWith(UnionFileSystem.SEP_STRING);
2939
this.pathParts = getPathParts(longstring);
3040
}
3141
this.normalized = null;
@@ -47,17 +57,13 @@ private UnionPath(final UnionFileSystem fileSystem, boolean absolute, boolean is
4757
}
4858

4959
private String[] getPathParts(final String longstring) {
50-
var sep = "(?:" + Pattern.quote(this.getFileSystem().getSeparator()) + ")";
51-
String pathname = longstring
52-
.replace("\\", this.getFileSystem().getSeparator())
53-
// remove separators from start and end of longstring
54-
.replaceAll("^" + sep + "*|" + sep + "*$", "")
55-
// Remove duplicate separators
56-
.replaceAll(sep + "+(?=" + sep + ")", "");
57-
if (pathname.isEmpty())
60+
var clean = longstring.replace("\\", UnionFileSystem.SEP_STRING);
61+
clean = SEPARATOR_BEGIN_END.matcher(clean).replaceAll("");
62+
clean = SEPARATOR_DUPLICATES.matcher(clean).replaceAll("");
63+
if (clean.isEmpty())
5864
return new String[0];
5965
else
60-
return pathname.split(this.getFileSystem().getSeparator());
66+
return clean.split(UnionFileSystem.SEP_STRING);
6167
}
6268

6369
@Override
@@ -302,6 +308,6 @@ public int hashCode() {
302308

303309
@Override
304310
public String toString() {
305-
return (this.absolute ? fileSystem.getSeparator() : "") + String.join(fileSystem.getSeparator(), this.pathParts);
311+
return (this.absolute ? UnionFileSystem.SEP_STRING : "") + String.join(UnionFileSystem.SEP_STRING, this.pathParts);
306312
}
307313
}

0 commit comments

Comments
 (0)