Skip to content

Commit

Permalink
Avoid slow Files#isDirectory call in testFilter (#63)
Browse files Browse the repository at this point in the history
On ZIP file systems, `Files.isDirectory` ends up being very slow if the
path does not exist (similarly to `Files.exists`, which is worked around
in UnionFS). We can take advantage of the existing workaround by using
our `getFileAttributes` method instead, which attempts to check if the
file exists using much more efficient logic.

See the profiler screenshot below for an example of the problem.


![zfas_neo](https://github.com/McModLauncher/securejarhandler/assets/42941056/f4001d99-a657-499d-8218-25e5ded84c4d)
  • Loading branch information
embeddedt authored Mar 27, 2024
1 parent 8d3b608 commit 38b063e
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/java/cpw/mods/niofs/union/UnionFileSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,8 @@ private boolean testFilter(final Path path, final Path basePath) {
var sPath = path.toString();
if (path.getFileSystem() == basePath.getFileSystem()) // Directories, zips will be different file systems.
sPath = basePath.relativize(path).toString().replace('\\', '/');
if (Files.isDirectory(path))
var attrs = getFileAttributes(path);
if (attrs.isPresent() && attrs.get().isDirectory())
sPath += '/';
if (sPath.length() > 1 && sPath.startsWith("/"))
sPath = sPath.substring(1);
Expand Down

0 comments on commit 38b063e

Please sign in to comment.