From e620d8e1cfec4e49bbb3b2afbd88b36a8a5f508c Mon Sep 17 00:00:00 2001 From: VAN BOSSUYT Nicolas Date: Wed, 8 Feb 2023 23:37:28 +0100 Subject: [PATCH] Context caching --- osdk/builder.py | 8 ++++---- osdk/cmds.py | 2 +- osdk/context.py | 12 ++++++++++-- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/osdk/builder.py b/osdk/builder.py index 7685128..aec51ad 100644 --- a/osdk/builder.py +++ b/osdk/builder.py @@ -76,8 +76,8 @@ def gen(out: TextIO, context: Context): writer.newline() -def build(componentSpec: str, targetSpec: str, props: Props = {}) -> str: - context = contextFor(targetSpec, props) +def build(componentSpec: str, targetSpec: str) -> str: + context = contextFor(targetSpec) target = context.target shell.mkdir(context.builddir()) @@ -107,8 +107,8 @@ class Paths: self.obj = obj -def buildAll(targetSpec: str, props: Props = {}) -> Paths: - context = contextFor(targetSpec, props) +def buildAll(targetSpec: str) -> Paths: + context = contextFor(targetSpec) target = context.target shell.mkdir(context.builddir()) diff --git a/osdk/cmds.py b/osdk/cmds.py index 538dbba..c6f02e7 100644 --- a/osdk/cmds.py +++ b/osdk/cmds.py @@ -156,7 +156,7 @@ def graphCmd(args: Args): scope: str | None = cast(str | None, args.tryConsumeOpt("scope")) onlyLibs: bool = args.consumeOpt("only-libs", False) == True - context = contextFor(targetSpec, {}) + context = contextFor(targetSpec) graph.view(context, scope=scope, showExe=not onlyLibs) diff --git a/osdk/context.py b/osdk/context.py index b0a729b..d3fd46e 100644 --- a/osdk/context.py +++ b/osdk/context.py @@ -183,7 +183,13 @@ def instanciate(componentSpec: str, components: list[ComponentManifest], target: return ComponentInstance(manifest, sources, resolved[1:]) -def contextFor(targetSpec: str, props: Props) -> Context: +context: dict = {} + + +def contextFor(targetSpec: str) -> Context: + if targetSpec in context: + return context[targetSpec] + logger.log(f"Loading context for {targetSpec}") targetEls = targetSpec.split(":") @@ -221,8 +227,10 @@ def contextFor(targetSpec: str, props: Props) -> Context: instances = cast(list[ComponentInstance], list(filter(lambda e: e != None, map(lambda c: instanciate( c.id, components, target), components)))) - return Context( + context[targetSpec] = Context( target, instances, tools, ) + + return context[targetSpec]