]> source.dussan.org Git - jgit.git/commitdiff
ObjectDirectory: avoid using File.getCanonicalPath() 86/195186/8
authorJörg Kubitz <jkubitz-eclipse@gmx.de>
Mon, 15 Aug 2022 09:05:04 +0000 (11:05 +0200)
committerMatthias Sohn <matthias.sohn@sap.com>
Thu, 22 Sep 2022 08:48:50 +0000 (10:48 +0200)
On java 17 + Windows OS java.io.File.getCanonicalPath is a very slow
system call which uses most time during clone.

That is since JDK 12 the result of File.getCanonicalPath is not cached
anymore by default:
https://bugs.openjdk.java.net/browse/JDK-8207005

* Use toRealPath() to follow symbolic links also on windows.
* Cache the result.

Bug: 580568
Change-Id: I95f4f5b2babefd7210ee4740646230225ebf3788

org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/ObjectDirectory.java

index 7699439128c0e0fe239af4854fd386c506fd0dfb..ff7ef932784373f0aa3dc4067f88a0c4a71725ff 100644 (file)
@@ -724,14 +724,17 @@ public class ObjectDirectory extends FileObjectDatabase {
 
        static class AlternateHandle {
                static class Id {
-                       String alternateId;
+                       private final String alternateId;
 
                        public Id(File object) {
+                               String id = null;
                                try {
-                                       this.alternateId = object.getCanonicalPath();
-                               } catch (Exception e) {
-                                       alternateId = null;
+                                       // resolve symbolic links to their final target:
+                                       id = object.toPath().toRealPath().normalize().toString();
+                               } catch (Exception ignored) {
+                                       // id == null
                                }
+                               this.alternateId = id;
                        }
 
                        @Override
@@ -757,6 +760,8 @@ public class ObjectDirectory extends FileObjectDatabase {
 
                final ObjectDirectory db;
 
+               private AlternateHandle.Id id;
+
                AlternateHandle(ObjectDirectory db) {
                        this.db = db;
                }
@@ -765,8 +770,11 @@ public class ObjectDirectory extends FileObjectDatabase {
                        db.close();
                }
 
-               public Id getId(){
-                       return db.getAlternateId();
+               public synchronized Id getId() {
+                       if (id == null) {
+                               id = new AlternateHandle.Id(db.objects);
+                       }
+                       return id;
                }
        }
 
@@ -795,6 +803,6 @@ public class ObjectDirectory extends FileObjectDatabase {
        }
 
        AlternateHandle.Id getAlternateId() {
-               return new AlternateHandle.Id(objects);
+               return handle.getId();
        }
 }