Skip to content

Commit cbfcb2e

Browse files
committed
fix emscript funcs caching and add testing
1 parent cd80d52 commit cbfcb2e

2 files changed

Lines changed: 17 additions & 16 deletions

File tree

emscripten.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,22 +175,22 @@ def emscript(infile, settings, outfile, libraries=[]):
175175

176176
if jcache:
177177
# load chunks from cache where we can # TODO: ignore small chunks
178-
cached_funcs = []
178+
cached_outputs = []
179179
def load_from_cache(chunk):
180180
keys = [settings_text, forwarded_data, chunk]
181181
shortkey = shared.JCache.get_shortkey(keys) # TODO: share shortkeys with later code
182182
out = shared.JCache.get(shortkey, keys)
183183
if out:
184-
cached_funcs.append(out)
184+
cached_outputs.append(out)
185185
return False
186186
return True
187187
chunks = filter(load_from_cache, chunks)
188-
if len(cached_funcs) > 0:
189-
if out and DEBUG: print >> sys.stderr, ' loading %d funcchunks from jcache' % len(cached_funcs)
190-
cached_funcs_js = ''.join(cached_funcs)
191-
cached_funcs = None
188+
if len(cached_outputs) > 0:
189+
if out and DEBUG: print >> sys.stderr, ' loading %d funcchunks from jcache' % len(cached_outputs)
192190
else:
193-
cached_funcs_js = ''
191+
cached_outputs = []
192+
193+
# TODO: minimize size of forwarded data from funcs to what we actually need
194194

195195
if cores == 1 and total_ll_size < MAX_CHUNK_SIZE: assert len(chunks) == 1, 'no point in splitting up without multiple cores'
196196

@@ -216,9 +216,8 @@ def load_from_cache(chunk):
216216
shared.JCache.set(shortkey, keys, outputs[i])
217217
if out and DEBUG and len(chunks) > 0: print >> sys.stderr, ' saving %d funcchunks to jcache' % len(chunks)
218218

219+
if jcache: outputs += cached_outputs # TODO: preserve order
219220
funcs_js = ''.join([output[0] for output in outputs])
220-
if jcache:
221-
funcs_js += cached_funcs_js # TODO insert them in the original order
222221

223222
for func_js, curr_forwarded_data in outputs:
224223
# merge forwarded data

tests/runner.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10461,7 +10461,9 @@ def test_jcache(self):
1046110461

1046210462
assert not os.path.exists(JCache.get_cachename('emscript_files'))
1046310463

10464-
src = None
10464+
srcs = {}
10465+
used_jcache = False
10466+
1046510467
for args, input_file, expect_save, expect_load in [
1046610468
([], 'hello_world_loop.cpp', False, False),
1046710469
(['--jcache'], 'hello_world_loop.cpp', True, False),
@@ -10473,20 +10475,20 @@ def test_jcache(self):
1047310475
([], 'hello_world.cpp', False, False),
1047410476
(['--jcache'], 'hello_world_loop.cpp', False, True), # go back to old file, experience caching
1047510477
]:
10476-
print args, input_file, expect_save, expect_load
10478+
print >> sys.stderr, args, input_file, expect_save, expect_load
1047710479
self.clear()
1047810480
out, err = Popen(['python', EMCC, path_from_root('tests', input_file)] + args, stdout=PIPE, stderr=PIPE).communicate()
1047910481
assert (PRE_SAVE_MSG in err) == expect_save, err
1048010482
assert (PRE_LOAD_MSG in err) == expect_load, err
1048110483
assert (FUNC_CHUNKS_SAVE_MSG in err) == expect_save, err
1048210484
assert (FUNC_CHUNKS_LOAD_MSG in err) == expect_load, err
1048310485
curr = open('a.out.js').read()
10484-
if src is None:
10485-
src = None
10486+
if input_file not in srcs:
10487+
srcs[input_file] = curr
1048610488
else:
10487-
assert src == curr, 'caching must not affect codegen'
10488-
10489-
assert os.path.exists(JCache.get_cachename('emscript_files'))
10489+
assert curr == srcs[input_file], err
10490+
used_jcache = used_jcache or ('--jcache' in args)
10491+
assert used_jcache == os.path.exists(JCache.get_cachename('emscript_files'))
1049010492

1049110493
finally:
1049210494
del os.environ['EMCC_DEBUG']

0 commit comments

Comments
 (0)