Skip to content

Commit

Permalink
Optimize pathing jar generation
Browse files Browse the repository at this point in the history
Manually generate a file name rather than using Files.createTempFile()
since this latter queries network interfaces which takes more time
than a good old System.nanotime().

Change-Id: Ib1e8765f010e8332e882b77d5a6ccac1d4860b9e
  • Loading branch information
TheItivitist committed Sep 6, 2023
1 parent 4765b39 commit dcd5a4b
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion capsule/src/main/java/Capsule.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import java.nio.channels.FileLock;
import java.nio.charset.Charset;
import java.nio.file.DirectoryStream;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
Expand Down Expand Up @@ -81,6 +82,7 @@
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Properties;
import java.util.Random;
import java.util.RandomAccess;
import java.util.Set;
import java.util.StringTokenizer;
Expand Down Expand Up @@ -5459,7 +5461,8 @@ static Path createPathingJar(Path dir, List<Path> cp) {
try {
final Path absolutePath = dir.toAbsolutePath();
final List<String> paths = createPathingClassPath(absolutePath, cp);
final Path pathingJar = Files.createTempFile(absolutePath, "capsule_pathing_jar", ".jar");

final Path pathingJar = createTempFile(absolutePath, "capsule_pathing_jar", ".jar");
final Manifest man = new Manifest();
man.getMainAttributes().putValue(ATTR_MANIFEST_VERSION, "1.0");
man.getMainAttributes().putValue(ATTR_CLASS_PATH, join(paths, " "));
Expand All @@ -5471,6 +5474,22 @@ static Path createPathingJar(Path dir, List<Path> cp) {
}
}

private static Path createTempFile(Path dir, String prefix, String suffix) throws IOException
{
for (;;)
{
try
{
String rand = Long.toUnsignedString(new Random().nextLong(), 16);
return Files.createFile(dir.resolve(prefix + rand + suffix));
}
catch (FileAlreadyExistsException ex)
{
// ignore
}
}
}

private static List<String> createPathingClassPath(Path dir, List<Path> cp) {
boolean allPathsHaveSameRoot = true;
for (Path p : cp) {
Expand Down

0 comments on commit dcd5a4b

Please sign in to comment.