From 56dce9ca36d09237c7cb539a4e88511398ff225e Mon Sep 17 00:00:00 2001 From: Bernhard Stoeckner Date: Sat, 17 Feb 2024 12:54:47 +0100 Subject: [PATCH] scriptcomp: cache resdir instances so they don't reload every script access, fixes #109 --- nwn_script_comp.nim | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/nwn_script_comp.nim b/nwn_script_comp.nim index e997e07..587ff0c 100644 --- a/nwn_script_comp.nim +++ b/nwn_script_comp.nim @@ -124,13 +124,19 @@ let params: ptr Params = globalState.params.addr # over a channel and services them sequentially. This was preferable to having one # resman per worker thread, since bringing up rm takes quite a bit of IO and cpu. +# Cache container instances so resDir does not reload every time a file is queried. +# Variable is only valid on service thread. +var resContainerCache {.threadvar.}: Table[string, ResContainer] + proc serviceRmDemand(rm: ResMan, resref: ResRef, searchPath: seq[RMSearchPathEntry]): string = var containers: seq[ResContainer] for q in searchPath: - case q[0] - of pcDir: containers.add newResDir(q[1]) - of pcFile: containers.add newResFile(q[1]) - else: continue + if not resContainerCache.hasKey(q[1]): + case q[0] + of pcDir: resContainerCache[q[1]] = newResDir(q[1]) + of pcFile: resContainerCache[q[1]] = newResFile(q[1]) + else: continue + containers.add resContainerCache[q[1]] for c in containers: rm.add(c) defer: