From 2c35c2d5dff76bc845190aa7e1fd6339d38495ef Mon Sep 17 00:00:00 2001 From: LoafyLemon Date: Tue, 3 Jan 2023 21:27:35 +0000 Subject: [PATCH] Optimizations * Disabled detection of orphaned scripts for non-devs * Added cache to renpy.list_files to reduce IO overhead (~14s -> ~0.01s per 10k calls) * Fixed wrong module call in weather. --- game/scripts/inventory/game.rpy | 2 +- game/scripts/utility/devtools.rpy | 27 +++++++++++++++------------ game/scripts/utility/engine.rpy | 18 +++++++++++++++++- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/game/scripts/inventory/game.rpy b/game/scripts/inventory/game.rpy index 9d7a6463..fd5edf62 100644 --- a/game/scripts/inventory/game.rpy +++ b/game/scripts/inventory/game.rpy @@ -59,7 +59,7 @@ init python: @weather.setter def weather(self, value): if value == "random": - value = renpy.python.random.choices(self.weather_types, weights=self.weather_weights)[0] + value = renpy.random.choices(self.weather_types, weights=self.weather_weights)[0] if not value in self.weather_types: raise ValueError("Unsupported weather type: '{}'".format(value)) diff --git a/game/scripts/utility/devtools.rpy b/game/scripts/utility/devtools.rpy index 185546f0..4cb4b696 100644 --- a/game/scripts/utility/devtools.rpy +++ b/game/scripts/utility/devtools.rpy @@ -14,22 +14,25 @@ init -1000 python early: def get_renderer(): return "DirectX" if preferences.renderer == "angle2" else "OpenGL" - def detect_orphaned_rpyc_files(): - excluded = ["tl/"] + if config.developer: + # Debug - files = renpy.list_files(common=True) - compiled = [x for x in files if x.endswith(".rpyc") if not any(x.startswith(i) for i in excluded)] - scripts = [x for x in files if x.endswith(".rpy")] - orphaned = [] + def detect_orphaned_rpyc_files(): + excluded = ["tl/"] - for i in compiled: - if not i[:-1] in scripts: - orphaned.append(i) + files = renpy.list_files(common=True) + compiled = [x for x in files if x.endswith(".rpyc") if not any(x.startswith(i) for i in excluded)] + scripts = [x for x in files if x.endswith(".rpy")] + orphaned = [] - if orphaned: - raise Exception("Orphaned compiled scripts detected, please delete them before continuing:\n{}".format(orphaned)) + for i in compiled: + if not i[:-1] in scripts: + orphaned.append(i) - detect_orphaned_rpyc_files() + if orphaned: + raise Exception("Orphaned compiled scripts detected, please delete them before continuing:\n{}".format(orphaned)) + + detect_orphaned_rpyc_files() init python: config.missing_image_callback = missing_image_func diff --git a/game/scripts/utility/engine.rpy b/game/scripts/utility/engine.rpy index 9cd83601..62c53f32 100644 --- a/game/scripts/utility/engine.rpy +++ b/game/scripts/utility/engine.rpy @@ -60,7 +60,7 @@ init python: config.statement_callbacks.append(crashdefender) -init -10 python: +init python early: if renpy.windows: # On windows, Renpy does not support backslashes in some of its functions, @@ -75,3 +75,19 @@ init -10 python: return renpy.loader.loadable(filename) renpy.loadable = _loadable + + # renpy.list_files does not use cached results, let's fix that. + + def _list_files(common=False, quick=True): + cache = "_list_files_common_cache" if common else "_list_files_cache" + files = getattr(renpy.store, cache, []) + + if not quick or not files: + rv = [fn for dir, fn in renpy.loader.listdirfiles(common) if not fn.startswith("saves/")] + rv.sort() + + setattr(renpy.store, cache, rv) + + return files + + renpy.list_files = _list_files