Skip to content

Commit 4d0c146

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

File tree

10 files changed

+65
-31
lines changed

10 files changed

+65
-31
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'}
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/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
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)

example/BenchmarkInodeRun.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ function BenchmarkInodeRun()
1818

1919
function result = run_bench(name)
2020
suite = testsuite(name);
21-
exp = matlab.perftest.TimeExperiment.limitingSamplingError(RelativeMarginOfError=0.1);
21+
exp = matlab.perftest.TimeExperiment.limitingSamplingError(RelativeMarginOfError=0.05);
2222
result = exp.run(suite);
2323
end

test/TestDisk.m

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,18 @@
1515
B_hard_link_count
1616
B_filesystem_type
1717
B_owner
18-
B_device
18+
B_device = {'java', 'python', 'sys'}
1919
B_is_dev_drive
2020
end
2121

2222
methods (TestParameterDefinition, Static)
23-
function [B_disk, B_is_removable, B_is_mount, B_hard_link_count, B_filesystem_type, B_owner, B_device, B_is_dev_drive] = setupBackends()
23+
function [B_disk, B_is_removable, B_is_mount, B_hard_link_count, B_filesystem_type, B_owner, B_is_dev_drive] = setupBackends()
2424
B_disk = init_backend("disk_available");
2525
B_is_removable = init_backend("is_removable");
2626
B_is_mount = init_backend("is_mount");
2727
B_hard_link_count = init_backend("hard_link_count");
2828
B_filesystem_type = init_backend("filesystem_type");
2929
B_owner = init_backend("get_owner");
30-
B_device = init_backend("device");
3130
B_is_dev_drive = init_backend('is_dev_drive');
3231
end
3332
end
@@ -130,8 +129,9 @@ function test_remove_file(tc)
130129

131130

132131
function test_device(tc, Po, B_device)
133-
i = stdlib.device(Po, B_device);
132+
[i, b] = stdlib.device(Po, B_device);
134133
tc.verifyClass(i, 'uint64')
134+
tc.assertEqual(char(b), B_device)
135135

136136
if ~stdlib.exists(Po)
137137
tc.verifyEmpty(i)
@@ -144,8 +144,9 @@ function test_device(tc, Po, B_device)
144144

145145
function test_inode(tc, Po, B_device)
146146

147-
i = stdlib.inode(Po, B_device);
147+
[i, b] = stdlib.inode(Po, B_device);
148148
tc.verifyClass(i, 'uint64')
149+
tc.assertEqual(char(b), B_device)
149150

150151
if ~stdlib.exists(Po)
151152
tc.verifyEmpty(i)

0 commit comments

Comments
 (0)