Skip to content

Commit 3b249f8

Browse files
committed
optimize writing in emscripten.py
1 parent f1b8f8f commit 3b249f8

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

emscripten.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ def emscript(infile, settings, outfile, libraries=[]):
136136
open(pre_file, 'w').write(''.join(pre) + '\n' + meta)
137137
out = shared.run_js(compiler, shared.COMPILER_ENGINE, [settings_file, pre_file, 'pre'] + libraries, stdout=subprocess.PIPE, cwd=path_from_root('src'))
138138
js, forwarded_data = out.split('//FORWARDED_DATA:')
139-
#print 'js', js
140-
#print >> sys.stderr, 'FORWARDED_DATA 1:', forwarded_data, type(forwarded_data)
139+
outfile.write(js)
140+
js = None
141141
forwarded_file = temp_files.get('.json').name
142142
open(forwarded_file, 'w').write(forwarded_data)
143143
if DEBUG: print >> sys.stderr, ' emscript: phase 1 took %s seconds' % (time.time() - t)
@@ -180,15 +180,17 @@ def emscript(infile, settings, outfile, libraries=[]):
180180
else:
181181
outputs = [process_funcs(commands[0])]
182182

183-
for funcs_js, curr_forwarded_data in outputs:
184-
js += funcs_js
183+
funcs_js = ''.join([output[0] for output in outputs])
184+
185+
for func_js, curr_forwarded_data in outputs:
185186
# merge forwarded data
186187
curr_forwarded_json = json.loads(curr_forwarded_data)
187188
forwarded_json['Types']['preciseI64MathUsed'] = forwarded_json['Types']['preciseI64MathUsed'] or curr_forwarded_json['Types']['preciseI64MathUsed']
188189
for key, value in curr_forwarded_json['Functions']['blockAddresses'].iteritems():
189190
forwarded_json['Functions']['blockAddresses'][key] = value
190191
for key in curr_forwarded_json['Functions']['indexedFunctions'].iterkeys():
191192
indexed_functions.add(key)
193+
outputs = None
192194
if DEBUG: print >> sys.stderr, ' emscript: phase 2 took %s seconds' % (time.time() - t)
193195
if DEBUG: t = time.time()
194196

@@ -202,6 +204,9 @@ def emscript(infile, settings, outfile, libraries=[]):
202204
indexing = forwarded_json['Functions']['indexedFunctions']
203205
def indexize(js):
204206
return re.sub(r'{{{ FI_([\w\d_$]+) }}}', lambda m: str(indexing[m.groups(0)[0]]), js)
207+
outfile.write(indexize(funcs_js))
208+
funcs_js = None
209+
205210
# forward
206211
forwarded_data = json.dumps(forwarded_json)
207212
forwarded_file = temp_files.get('.2.json').name
@@ -213,11 +218,9 @@ def indexize(js):
213218
post_file = temp_files.get('.post.ll').name
214219
open(post_file, 'w').write(''.join(post) + '\n' + meta)
215220
out = shared.run_js(compiler, shared.COMPILER_ENGINE, [settings_file, post_file, 'post', forwarded_file] + libraries, stdout=subprocess.PIPE, cwd=path_from_root('src'))
216-
js += out
217-
js = indexize(js)
221+
outfile.write(indexize(out))
218222
if DEBUG: print >> sys.stderr, ' emscript: phase 3 took %s seconds' % (time.time() - t)
219223

220-
outfile.write(js) # TODO: write in parts (see previous line though)
221224
outfile.close()
222225

223226

0 commit comments

Comments
 (0)