Skip to content

Commit a5fd49f

Browse files
committed
inode, device: 20x faster
1 parent 419a096 commit a5fd49f

File tree

12 files changed

+90
-41
lines changed

12 files changed

+90
-41
lines changed

+stdlib/+java/device.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
% Java 1.8 benefits from the absolute() for stability
1010
% seen on older Matlab versions on HPC
11-
opt = java.nio.file.LinkOption.values();
1211

1312
try
13+
opt = java.nio.file.LinkOption.values();
1414
i = java.nio.file.Files.getAttribute(javaAbsolutePath(file), "unix:dev", opt);
1515
catch e
1616
javaException(e)

+stdlib/+java/inode.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
% Java 1.8 benefits from the absolute() for stability
1010
% seen on older Matlab versions on HPC
11-
opt = java.nio.file.LinkOption.values();
1211

1312
try
13+
opt = java.nio.file.LinkOption.values();
1414
i = java.nio.file.Files.getAttribute(javaAbsolutePath(file), "unix:ino", opt);
1515
catch e
1616
javaException(e)
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
function javaException(e)
22

3-
switch class(e.ExceptionObject)
4-
case {'java.nio.file.NoSuchFileException', 'java.nio.file.NotLinkException'}
5-
otherwise, rethrow(e)
3+
switch e.identifier
4+
case 'MATLAB:Java:GenericException'
5+
switch class(e.ExceptionObject)
6+
case {'java.nio.file.NoSuchFileException', 'java.nio.file.NotLinkException', 'java.lang.UnsupportedOperationException'}
7+
% pass
8+
otherwise
9+
rethrow(e)
10+
end
11+
case 'MATLAB:undefinedVarOrClass'
12+
% Java not enabled -nojvm
13+
otherwise
14+
rethrow(e)
615
end
716

817
end

+stdlib/device.m

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,35 @@
22
%
33
%%% Inputs
44
% * file: path to file
5+
% * backend: backend to use
56
%%% Outputs
67
% * i: device index
78
% * b: backend used
89

910
function [i, b] = device(file, backend)
1011
arguments
1112
file string
12-
backend (1,:) string = ["java", "python", "perl", "sys"]
13+
backend (1,:) string = ["java", "python", "sys"]
1314
end
1415

15-
o = stdlib.Backend(mfilename(), backend);
16-
i = o.func(file);
16+
i = uint64.empty;
17+
18+
for b = backend
19+
switch b
20+
case "java"
21+
i = stdlib.java.device(file);
22+
case "python"
23+
if stdlib.matlabOlderThan('R2022a'), continue, end
24+
i = stdlib.python.device(file);
25+
case "sys"
26+
i = stdlib.sys.device(file);
27+
otherwise
28+
error("stdlib:device:ValueError", "Unknown backend: %s", b)
29+
end
30+
31+
if ~isempty(i)
32+
return
33+
end
34+
end
1735

18-
b = o.backend;
1936
end

+stdlib/hard_link_count.m

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,39 @@
22
%
33
%%% inputs
44
% * file: path to check
5+
% * backend: backend to use
56
%%% Outputs
6-
% * c: number of hard links
7+
% * i: number of hard links
78
% * b: backend used
89
%% Java backend references:
910
%
1011
% * https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#getPosixFileAttributes(java.nio.file.Path,java.nio.file.LinkOption...)
1112
% * https://docs.oracle.com/javase/tutorial/essential/io/links.html
1213

13-
function [c, b] = hard_link_count(file, backend)
14+
function [i, b] = hard_link_count(file, backend)
1415
arguments
1516
file string
1617
backend (1,:) string = ["java", "python", "sys"]
1718
end
1819

19-
o = stdlib.Backend(mfilename(), backend);
20+
i = [];
2021

21-
if isscalar(file)
22-
c = o.func(file);
23-
else
24-
c = arrayfun(o.func, file);
22+
for b = backend
23+
switch b
24+
case "java"
25+
i = stdlib.java.hard_link_count(file);
26+
case "python"
27+
if stdlib.matlabOlderThan('R2022a'), continue, end
28+
i = stdlib.python.hard_link_count(file);
29+
case "sys"
30+
i = stdlib.sys.hard_link_count(file);
31+
otherwise
32+
error("stdlib:hard_link_count:ValueError", "Unknown backend: %s", b)
33+
end
34+
35+
if ~isempty(i)
36+
return
37+
end
2538
end
2639

27-
b = o.backend;
2840
end

+stdlib/inode.m

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,35 @@
22
%
33
%%% inputs
44
% * file: path to check
5+
% * backend: backend to use
56
%%% Outputs
67
% * i: inode number
78
% * b: backend used
89

910
function [i, b] = inode(file, backend)
1011
arguments
1112
file string
12-
backend (1,:) string = ["java", "python", "perl", "sys"]
13+
backend (1,:) string = ["java", "python", "sys"]
1314
end
1415

15-
o = stdlib.Backend(mfilename(), backend);
16+
i = uint64.empty;
1617

17-
if isscalar(file)
18-
i = o.func(file);
19-
else
20-
i = arrayfun(o.func, file);
21-
end
18+
for b = backend
19+
switch b
20+
case "java"
21+
i = stdlib.java.inode(file);
22+
case "python"
23+
if stdlib.matlabOlderThan('R2022a'), continue, end
24+
i = stdlib.python.inode(file);
25+
case "sys"
26+
i = stdlib.sys.inode(file);
27+
otherwise
28+
error("stdlib:inode:ValueError", "Unknown backend: %s", b)
29+
end
2230

23-
b = o.backend;
31+
if ~isempty(i)
32+
return
33+
end
34+
end
2435

2536
end

+stdlib/samepath.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
%
1212
%%% inputs
1313
% * path1, path2: paths to compare
14-
% * backend: backend to use, default is "python"
14+
% * backend: backend to use
1515
%%% Outputs
1616
% * ok: true if paths are the same
1717
% * b: backend used
File renamed without changes.

+stdlib/+perl/inode.m renamed to example/+perl/inode.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
%% PERL.INODE
2+
% several times slower than stdlib.sys.inode
3+
14
function [r, cmd] = inode(file)
25

36
r = uint64.empty;

example/BenchmarkInode.m

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,9 @@
88
end
99

1010
properties(TestParameter)
11-
backend
11+
backend = cellstr(["java", "python", "sys"])
1212
end
1313

14-
methods (TestParameterDefinition, Static)
15-
function backend = setupBackend()
16-
backend = init_backend('inode');
17-
end
18-
end
19-
20-
2114
methods (Test)
2215

2316
function bench_exist(tc, backend)

0 commit comments

Comments
 (0)