Skip to content

Commit

Permalink
Add support for code caching
Browse files Browse the repository at this point in the history
  • Loading branch information
bill88t committed Dec 29, 2023
1 parent 355ab10 commit ac361e4
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions source/ljinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ def systemprints(mod: int, tx1: str, tx2: str = None) -> None:
class ljinux:
modules = {}
devices = {}
code_cache = {}

def deinit_consoles() -> None:
for i in vr("consoles", pid=0).keys():
Expand Down Expand Up @@ -673,11 +674,15 @@ def __enter__(self):
# print(f"DEBUG FOPEN: {self.fn}:{self.mod}")
try:
rm = False # remount
fname = ljinux.api.betterpath(self.fn)
if "w" in self.mod or "a" in self.mod:
if fname in ljinux.code_cache:
ljinux.code_cache.pop(fname)
rm = True
if rm and not pv[0]["sdcard_fs"]:
remount("/", False)
self.file = open(ljinux.api.betterpath(self.fn), self.mod)
self.file = open(fname, self.mod)
del fname
if rm and not pv[0]["sdcard_fs"]:
remount("/", True)
except RuntimeError:
Expand Down Expand Up @@ -1463,17 +1468,28 @@ def fpexec(inpt): # Python script exec
return

prog = None
with ljinux.api.fopen(inpt[offs]) as f:
if f is None:
raise OSError
prog = f.read()

launch_process(ljinux.api.betterpath(inpt[offs]))
fname = ljinux.api.betterpath(inpt[offs])
if use_compiler and fname not in ljinux.code_cache:
with ljinux.api.fopen(inpt[offs]) as f:
if f is None:
raise OSError
prog = f.read()
del inpt
launch_process(fname)

try:
if use_compiler:
prog = compile(prog, "fpexec", "exec")
if fname not in ljinux.code_cache:
prog = compile(prog, "fpexec", "exec")
if gc.mem_free() > 200_000:
# Only cache when we have ample ram.
ljinux.code_cache[fname] = prog
elif len(ljinux.code_cache):
# We should clear the cache.
ljinux.code_cache.clear()
else:
prog = ljinux.code_cache[fname]
del fname
if not ("t" in fpargs or "l" in fpargs):
del fpargs
gc.collect()
Expand Down

0 comments on commit ac361e4

Please sign in to comment.