Skip to content

Commit 2acca67

Browse files
author
fangq
committed
massively accelerating loadjson for parsing large collection of unstructured small objects
git-svn-id: http://svn.code.sf.net/p/iso2mesh/code/trunk/jsonlab@489 786e58fb-9377-0410-9ff7-e4ac0ac0635c
1 parent 6dd1d9c commit 2acca67

File tree

1 file changed

+19
-68
lines changed

1 file changed

+19
-68
lines changed

loadjson.m

Lines changed: 19 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@
6969
try
7070
string = fileread(fname);
7171
catch
72-
string = urlread(['file:///',fullfile(pwd,fname)]);
72+
try
73+
string = urlread(['file://',fname]);
74+
catch
75+
string = urlread(['file://',fullfile(pwd,fname)]);
76+
end
7377
end
7478
else
7579
error('input file does not exist');
@@ -113,61 +117,6 @@
113117
close(opt.progressbar_);
114118
end
115119

116-
%%-------------------------------------------------------------------------
117-
function newdata=jstruct2array(data)
118-
fn=fieldnames(data);
119-
newdata=data;
120-
len=length(data);
121-
if(~isempty(strmatch('x0x5F_ArrayType_',fn)) && ~isempty(strmatch('x0x5F_ArrayData_',fn)))
122-
newdata=cell(len,1);
123-
for j=1:len
124-
ndata=cast(data(j).x0x5F_ArrayData_,data(j).x0x5F_ArrayType_);
125-
iscpx=0;
126-
if(~isempty(strmatch('x0x5F_ArrayIsComplex_',fn)))
127-
if(data(j).x0x5F_ArrayIsComplex_)
128-
iscpx=1;
129-
end
130-
end
131-
if(~isempty(strmatch('x0x5F_ArrayIsSparse_',fn)))
132-
if(data(j).x0x5F_ArrayIsSparse_)
133-
if(~isempty(strmatch('x0x5F_ArraySize_',fn)))
134-
dim=data(j).x0x5F_ArraySize_;
135-
if(iscpx && size(ndata,2)==4-any(dim==1))
136-
ndata(:,end-1)=complex(ndata(:,end-1),ndata(:,end));
137-
end
138-
if isempty(ndata)
139-
% All-zeros sparse
140-
ndata=sparse(dim(1),prod(dim(2:end)));
141-
elseif dim(1)==1
142-
% Sparse row vector
143-
ndata=sparse(1,ndata(:,1),ndata(:,2),dim(1),prod(dim(2:end)));
144-
elseif dim(2)==1
145-
% Sparse column vector
146-
ndata=sparse(ndata(:,1),1,ndata(:,2),dim(1),prod(dim(2:end)));
147-
else
148-
% Generic sparse array.
149-
ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3),dim(1),prod(dim(2:end)));
150-
end
151-
else
152-
if(iscpx && size(ndata,2)==4)
153-
ndata(:,3)=complex(ndata(:,3),ndata(:,4));
154-
end
155-
ndata=sparse(ndata(:,1),ndata(:,2),ndata(:,3));
156-
end
157-
end
158-
elseif(~isempty(strmatch('x0x5F_ArraySize_',fn)))
159-
if(iscpx && size(ndata,2)==2)
160-
ndata=complex(ndata(:,1),ndata(:,2));
161-
end
162-
ndata=reshape(ndata(:),data(j).x0x5F_ArraySize_);
163-
end
164-
newdata{j}=ndata;
165-
end
166-
if(len==1)
167-
newdata=newdata{1};
168-
end
169-
end
170-
171120
%%-------------------------------------------------------------------------
172121
function object = parse_object(varargin)
173122
parse_char('{');
@@ -189,7 +138,7 @@
189138
end
190139
parse_char('}');
191140
if(isstruct(object))
192-
object=jstruct2array(object);
141+
object=struct2jdata(object);
193142
end
194143

195144
%%-------------------------------------------------------------------------
@@ -200,7 +149,10 @@
200149
object = cell(0, 1);
201150
dim2=[];
202151
arraydepth=jsonopt('JSONLAB_ArrayDepth_',1,varargin{:});
203-
pbar=jsonopt('progressbar_',-1,varargin{:});
152+
pbar=-1;
153+
if(isfield(varargin{1},'progressbar_'))
154+
pbar=varargin{1}.progressbar_;
155+
end
204156

205157
if next_char ~= ']'
206158
if(jsonopt('FastArrayParser',1,varargin{:})>=1 && arraydepth>=jsonopt('FastArrayParser',1,varargin{:}))
@@ -291,19 +243,19 @@
291243

292244
function parse_char(c)
293245
global pos inStr len
294-
skip_whitespace;
246+
pos=skip_whitespace(pos,inStr,len);
295247
if pos > len || inStr(pos) ~= c
296248
error_pos(sprintf('Expected %c at position %%d', c));
297249
else
298250
pos = pos + 1;
299-
skip_whitespace;
251+
pos=skip_whitespace(pos,inStr,len);
300252
end
301253

302254
%%-------------------------------------------------------------------------
303255

304256
function c = next_char
305257
global pos inStr len
306-
skip_whitespace;
258+
pos=skip_whitespace(pos,inStr,len);
307259
if pos > len
308260
c = [];
309261
else
@@ -312,10 +264,10 @@ function parse_char(c)
312264

313265
%%-------------------------------------------------------------------------
314266

315-
function skip_whitespace
316-
global pos inStr len
317-
while pos <= len && isspace(inStr(pos))
318-
pos = pos + 1;
267+
function newpos=skip_whitespace(pos,inStr,len)
268+
newpos=pos;
269+
while newpos <= len && isspace(inStr(newpos))
270+
newpos = newpos + 1;
319271
end
320272

321273
%%-------------------------------------------------------------------------
@@ -401,9 +353,8 @@ function parse_char(c)
401353
function val = parse_value(varargin)
402354
global pos inStr len
403355

404-
pbar=jsonopt('progressbar_',-1,varargin{:});
405-
if(pbar>0)
406-
waitbar(pos/len,pbar,'loading ...');
356+
if(isfield(varargin{1},'progressbar_'))
357+
waitbar(pos/len,varargin{1}.progressbar_,'loading ...');
407358
end
408359

409360
switch(inStr(pos))

0 commit comments

Comments
 (0)