8208077: File.listRoots performance degradation

Reviewed-by: lancea, bpb
This commit is contained in:
Alan Bateman 2023-01-19 06:59:38 +00:00
parent 2e9cb4b1f6
commit 5b0af1a80b
2 changed files with 28 additions and 12 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2001, 2022, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2001, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -745,7 +745,7 @@ class WinNTFileSystem extends FileSystem {
.valueOf(new long[] {listRoots0()}) .valueOf(new long[] {listRoots0()})
.stream() .stream()
.mapToObj(i -> new File((char)('A' + i) + ":" + slash)) .mapToObj(i -> new File((char)('A' + i) + ":" + slash))
.filter(f -> access(f.getPath()) && f.exists()) .filter(f -> access(f.getPath()))
.toArray(File[]::new); .toArray(File[]::new);
} }
private static native int listRoots0(); private static native int listRoots0();

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1998, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -23,11 +23,17 @@
/* @test /* @test
@bug 4071322 @bug 4071322
@summary Basic test for listRoots method @summary Basic test for File.listRoots method
*/ */
import java.io.*; import java.io.File;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
public class ListRoots { public class ListRoots {
@ -37,15 +43,25 @@ public class ListRoots {
System.out.println(i + ": " + rs[i]); System.out.println(i + ": " + rs[i]);
} }
File f = new File(System.getProperty("test.src", "."), File f = new File(System.getProperty("test.src", "."), "ListRoots.java");
"ListRoots.java");
String cp = f.getCanonicalPath(); String cp = f.getCanonicalPath();
for (int i = 0; i < rs.length; i++) { boolean found = Stream.of(rs)
if (cp.startsWith(rs[i].getPath())) break; .map(File::getPath)
if (i == rs.length - 1) .anyMatch(p -> cp.startsWith(p));
throw new Exception(cp + " does not have a recognized root"); if (!found) {
throw new RuntimeException(cp + " does not have a recognized root");
} }
// the list of roots should match FileSystem::getRootDirectories
Set<File> roots1 = Stream.of(rs).collect(Collectors.toSet());
FileSystem fs = FileSystems.getDefault();
Set<File> roots2 = StreamSupport.stream(fs.getRootDirectories().spliterator(), false)
.map(Path::toFile)
.collect(Collectors.toSet());
if (!roots1.equals(roots2)) {
System.out.println(roots2);
throw new RuntimeException("Does not match FileSystem::getRootDirectories");
}
} }
} }