Skip to content

Commit dafbbe2

Browse files
committed
java: increase efficiency with java internals
java.device: nearly twice as fast by using Java internally java.inode: nearly twice as fast by using Java internally
1 parent 11521c3 commit dafbbe2

File tree

6 files changed

+43
-43
lines changed

6 files changed

+43
-43
lines changed

+stdlib/+java/device.m

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
function i = device(file)
2-
arguments
3-
file (1,1) string
4-
end
52

63
i = uint64([]);
7-
if ~stdlib.exists(file), return, end
84

9-
opt = java.nio.file.LinkOption.values();
5+
if stdlib.strempty(file)
6+
return
7+
end
108

11-
jp = javaPathObject(stdlib.absolute(file));
129
% Java 1.8 benefits from the absolute() for stability
1310
% seen on older Matlab versions on HPC
11+
opt = java.nio.file.LinkOption.values();
1412

15-
try %#ok<TRYNC>
16-
i = java.nio.file.Files.getAttribute(jp, 'unix:dev', opt);
13+
try
14+
i = java.nio.file.Files.getAttribute(javaAbsolutePath(file), "unix:dev", opt);
15+
catch e
16+
if class(e.ExceptionObject) ~= "java.nio.file.NoSuchFileException"
17+
rethrow(e)
18+
end
1719
end
1820

1921
i = uint64(i);

+stdlib/+java/get_owner.m

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
% this try-catch is faster and more robust
88

99
function n = get_owner(file)
10-
arguments
11-
file (1,1) string
12-
end
1310

1411
% https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/nio/file/Files.html#getOwner(java.nio.file.Path,java.nio.file.LinkOption...)
1512
% https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/nio/file/LinkOption.html

+stdlib/+java/hard_link_count.m

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
function c = hard_link_count(file)
2-
arguments
3-
file (1,1) string
4-
end
1+
function i = hard_link_count(file)
52

63
opt = java.nio.file.LinkOption.values();
74
try
8-
c = java.nio.file.Files.getAttribute(javaPathObject(file), "unix:nlink", opt);
9-
catch
10-
c = [];
5+
i = java.nio.file.Files.getAttribute(javaAbsolutePath(file), "unix:nlink", opt);
6+
catch e
7+
if class(e.ExceptionObject) ~= "java.nio.file.NoSuchFileException"
8+
rethrow(e)
9+
end
10+
i = [];
1111
end
1212
end

+stdlib/+java/inode.m

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
function i = inode(file)
2-
arguments
3-
file (1,1) string
4-
end
52

63
i = uint64([]);
7-
if ~stdlib.exists(file), return, end
84

9-
opt = java.nio.file.LinkOption.values();
5+
if stdlib.strempty(file)
6+
return
7+
end
108

11-
jp = javaPathObject(stdlib.absolute(file));
12-
% Java 1.8 benefits from the absolute() for stability--it's not an issue
13-
% on every computer.
9+
% Java 1.8 benefits from the absolute() for stability
10+
% seen on older Matlab versions on HPC
11+
opt = java.nio.file.LinkOption.values();
1412

15-
try %#ok<TRYNC>
16-
i = java.nio.file.Files.getAttribute(jp, "unix:ino", opt);
13+
try
14+
i = java.nio.file.Files.getAttribute(javaAbsolutePath(file), "unix:ino", opt);
15+
catch e
16+
if class(e.ExceptionObject) ~= "java.nio.file.NoSuchFileException"
17+
rethrow(e)
18+
end
1719
end
1820

1921
i = uint64(i);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function javaPath = javaAbsolutePath(file)
2+
% java.lang.System.getProperty('user.path') is stuck to where Java started
3+
4+
if ischar(file) || isstring(file)
5+
file = java.io.File(file);
6+
end
7+
javaPath = file.toPath();
8+
9+
if ~javaPath.isAbsolute()
10+
javaPath = javaPathObject(pwd()).resolve(javaPath);
11+
end
12+
13+
end

+stdlib/+java/samepath.m

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,9 @@
66
f2 = java.io.File(path2);
77

88
if f1.exists() && f2.exists()
9-
p1 = file2absPath(f1);
10-
p2 = file2absPath(f2);
11-
y = java.nio.file.Files.isSameFile(p1, p2);
9+
y = java.nio.file.Files.isSameFile(javaAbsolutePath(f1), javaAbsolutePath(f2));
1210
else
1311
y = false;
1412
end
1513

1614
end
17-
18-
19-
function jpath = file2absPath(jfile)
20-
% java.lang.System.getProperty('user.path') is stuck to where Java started
21-
22-
jpath = jfile.toPath();
23-
24-
if ~jfile.isAbsolute()
25-
jpath = javaPathObject(pwd()).resolve(jpath);
26-
end
27-
28-
end

0 commit comments

Comments
 (0)