Skip to content

Commit ee98cc8

Browse files
fangqclaude
andcommitted
[perf] loadbj: a big payload skips the run tables and the whole-buffer string mask
In Octave, the jump tables are not built when typed arrays fill most of the buffer (their payload is stepped over at once), and the string bytes are gathered by run expansion when they are a small part of it. A 32 MiB zlib-compressed array: 0.57 -> 0.21 s (the original: 0.20 s). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 79cee3f commit ee98cc8

1 file changed

Lines changed: 42 additions & 7 deletions

File tree

‎loadbj.m‎

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,28 @@
14531453
% Octave's interpreter; MATLAB's compiled loop is faster without these whole-buffer passes
14541454
vn = zeros(1, 0, 'int32');
14551455
pn = vn;
1456+
if (runs && n > 2^20)
1457+
% not when typed arrays ([$T#n, a payload the loop steps over at once) fill most of the
1458+
% buffer: their bytes need no tables (a compressed array, a large numeric array)
1459+
j = strfind(s, '[$');
1460+
j = j(j + 5 <= n);
1461+
j = j(s(j + 3) == '#');
1462+
if (~isempty(j))
1463+
ls = isz(min(max(double(s(j + 4)), 1), 256));
1464+
es = vsz(min(max(double(s(j + 2)), 1), 256));
1465+
j = j(ls > 0 & es > 0);
1466+
es = es(ls > 0 & es > 0);
1467+
ls = ls(ls > 0);
1468+
cnt = zeros(size(j));
1469+
w = 1;
1470+
for b = 1:8 % the counts: little-endian, 1 to 8 bytes
1471+
k = ls >= b & j + 4 + b <= n;
1472+
cnt(k) = cnt(k) + double(s(j(k) + 4 + b)) * w;
1473+
w = w * 256;
1474+
end
1475+
runs = sum(cnt .* es) < n / 2;
1476+
end
1477+
end
14561478
if (runs)
14571479
% (in blocks of 1 MiB: the temporaries stay small)
14581480
vn = zeros(1, n + 1, 'int32');
@@ -1734,13 +1756,26 @@
17341756
j = find(isk | tm == 83 | tm == 72);
17351757
if (~isempty(j))
17361758
L = tlen(j);
1737-
mk = zeros(1, n + 1, 'int8'); % (1 byte a byte: the spans do not overlap)
1738-
mk(tpos(j)) = 1;
1739-
e = tpos(j) + L;
1740-
mk(e) = mk(e) - 1;
1741-
mk = cumsum(mk(1:n)) > 0;
1742-
g = in(mk);
1743-
clear mk;
1759+
if (sum(L) > n / 8)
1760+
mk = zeros(1, n + 1, 'int8'); % (1 byte a byte: the spans do not overlap)
1761+
mk(tpos(j)) = 1;
1762+
e = tpos(j) + L;
1763+
mk(e) = mk(e) - 1;
1764+
mk = cumsum(mk(1:n)) > 0;
1765+
g = in(mk);
1766+
clear mk;
1767+
else
1768+
% few string bytes in a large buffer (a big payload): their positions, run by run
1769+
nz = L > 0;
1770+
Lz = L(nz);
1771+
P = tpos(j(nz));
1772+
g = in([]);
1773+
if (~isempty(P))
1774+
d = ones(1, sum(Lz));
1775+
d([1, cumsum(Lz(1:end - 1)) + 1]) = [P(1), P(2:end) - (P(1:end - 1) + Lz(1:end - 1) - 1)];
1776+
g = in(cumsum(d));
1777+
end
1778+
end
17441779
if (opt.utf8_)
17451780
% the strings with bytes above 127, from the gathered bytes' positions
17461781
nz = find(g > 127);

0 commit comments

Comments
 (0)