Skip to content

Commit

Permalink
Cache macro logics
Browse files Browse the repository at this point in the history
  • Loading branch information
nickdrozd committed May 20, 2024
1 parent 8cb83cd commit 54fd582
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions tm/macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ def make_block_macro(
blocks: int,
params: Params,
) -> MacroProg:
return MacroProg(comp, BlockLogic(blocks, *params))
return MacroProg(comp, make_block(blocks, params))


def make_backsymbol_macro(
comp: GetInstr,
backsym: int,
params: Params,
) -> MacroProg:
return MacroProg(comp, BacksymbolLogic(backsym, *params))
return MacroProg(comp, make_backsym(backsym, params))

########################################

Expand Down Expand Up @@ -377,3 +377,33 @@ def reconstruct_outputs(self, config: Config) -> Instr:
* self.backsymbols)))
,
)


BLOCKS: dict[
Params,
dict[int, BlockLogic]
] = defaultdict(dict)


BACKSYMS: dict[
Params,
dict[int, BacksymbolLogic]
] = defaultdict(dict)


def make_block(blocks: int, params: Params) -> BlockLogic:
if (cached := BLOCKS[params].get(blocks)) is not None:
return cached

block = BlockLogic(blocks, *params)
BLOCKS[params][blocks] = block
return block


def make_backsym(backsyms: int, params: Params) -> BacksymbolLogic:
if (cached := BACKSYMS[params].get(backsyms)) is not None:
return cached

backsym = BacksymbolLogic(backsyms, *params)
BACKSYMS[params][backsyms] = backsym
return backsym

0 comments on commit 54fd582

Please sign in to comment.