Skip to content

Commit efaaea7

Browse files
fangqclaude
andcommitted
[feat] savemsgpack: the jdai MEX backend; compress encoded arrays deliberately
savemsgpack uses jdai.savemsgpack when the MEX file is on the path (Backend 'auto'|'mex'|'m'): the same bytes, 20-30x faster. Complex and sparse arrays (through jdataencode) are compressed with the compression in effect (zlib by default), as savebj does; before, that happened only when the option name's case made varargin2struct keep both fields. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 6e9fde4 commit efaaea7

1 file changed

Lines changed: 27 additions & 2 deletions

File tree

‎savemsgpack.m‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
% tables, maps, ... as JData annotations)
3333
% UnpackHex [1|0]: field names decoded with decodevarname (x0x20_ is a space)
3434
% ForceRootName [0|1]: a struct or cell with no root name is written as {"root": ...}
35+
% Backend ['auto'|'mex'|'m']: 'auto' uses the compiled jdai backend (jdai.savemsgpack)
36+
% when it is on the path, else this file's code; 'mex' requires jdai;
37+
% 'm' never uses it
3538
% output:
3639
% msgpk: the MessagePack bytes (a char row, as savebj gives its bytes)
3740
%
@@ -63,6 +66,24 @@
6366
opt = varargin2struct(varargin{:});
6467
end
6568

69+
% the compiled backend (jdai) when it is on the path: the same bytes, much faster; options and values
70+
% it does not handle, and any error, fall back to the code below
71+
backend = lower(jsonopt('Backend', 'auto', opt));
72+
if (~strcmp(backend, 'm') && exist('jdai_mex', 'file') == 3)
73+
try
74+
if (nargout > 0 || isempty(jsonopt('FileName', '', opt)))
75+
msgpk = jdai.savemsgpack(rootname, obj, opt);
76+
else
77+
jdai.savemsgpack(rootname, obj, opt);
78+
end
79+
return
80+
catch err
81+
if (strcmp(backend, 'mex'))
82+
rethrow(err);
83+
end
84+
end
85+
end
86+
6687
o.packed = jsonopt('PackedArray', 0, opt) == 1;
6788
o.keeptype = jsonopt('KeepType', 0, opt) == 1;
6889
o.nestarray = jsonopt('NestArray', 0, opt) == 1;
@@ -86,8 +107,12 @@
86107
% classes): on those values only, as they are met (it gives each value what it gives it within
87108
% the whole), or on the whole value first when an option changes ordinary arrays too
88109
o.preencode = jsonopt('PreEncode', 1, opt) == 1;
89-
o.jdopt = opt;
90-
o.jdopt.Compression = ''; % (arrays are compressed below, as savebj does)
110+
% (jdataencode compresses the data of what it encodes, complex and sparse arrays, as savebj does:
111+
% with the compression in effect, zlib by default)
112+
fn = fieldnames(opt);
113+
o.jdopt = rmfield(opt, fn(strcmpi(fn, 'Compression') | strcmpi(fn, 'CompressArraySize')));
114+
o.jdopt.Compression = o.zip;
115+
o.jdopt.CompressArraySize = o.zipsize;
91116
if (o.preencode && (jsonopt('UseArrayShape', 0, opt) || jsonopt('ArrayToStruct', 0, opt) || jsonopt('AnnotateArray', 0, opt) || ...
92117
jsonopt('NestArray', 0, opt) || ~isempty(jsonopt('ArrayChunks', [], opt)) || jsonopt('FormatVersion', 4, opt) < 4))
93118
obj = jdataencode(obj, 'Base64', 0, 'UseArrayZipSize', 0, o.jdopt);

0 commit comments

Comments
 (0)