Skip to content

Commit

Permalink
Cut down memory footprint
Browse files Browse the repository at this point in the history
  • Loading branch information
BlankSpruce committed Jan 10, 2025
1 parent e6ff4ef commit da593a7
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
11 changes: 7 additions & 4 deletions gersemi/builtin_commands.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# pylint: disable=too-many-lines
from typing import Iterable, List, Mapping
from gersemi.immutable import make_immutable
from gersemi.specializations.add_custom_target import add_custom_target
from gersemi.specializations.condition_syntax_command_invocation_dumper import (
condition_syntax_commands,
Expand Down Expand Up @@ -3140,10 +3141,12 @@ def add_canonical_name(value, canonical_name):


def preprocess_definitions(definitions):
return {
key.lower(): add_canonical_name(value, key)
for key, value in definitions.items()
}
return make_immutable(
{
key.lower(): add_canonical_name(value, key)
for key, value in definitions.items()
}
)


builtin_commands = preprocess_definitions(builtin_commands_impl)
3 changes: 2 additions & 1 deletion gersemi/command_invocation_dumper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import collections.abc
from contextlib import contextmanager
from functools import lru_cache
from lark import Tree
Expand Down Expand Up @@ -36,7 +37,7 @@ def _get_patch(self, raw_command_name):
command_name = raw_command_name.lower()
if command_name in builtin_commands:
command = builtin_commands[command_name]
if isinstance(command, dict):
if isinstance(command, collections.abc.Mapping):
return create_standard_dumper(command)

return command
Expand Down
3 changes: 2 additions & 1 deletion gersemi/custom_command_definition_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from lark import Discard
from lark.visitors import Interpreter, Transformer
from gersemi.ast_helpers import is_keyword
from gersemi.immutable import make_immutable
from gersemi.keywords import Hint, Keywords


Expand Down Expand Up @@ -208,4 +209,4 @@ def get_just_definitions(definitions):
sorted_info = list(sorted(info, key=lambda item: item[1]))
(canonical_name, (positional_arguments, keywords)), _ = sorted_info[0]
result[name] = create_command(canonical_name, positional_arguments, keywords)
return result
return make_immutable(result)
36 changes: 36 additions & 0 deletions gersemi/immutable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from collections import abc


class ImmutableDict(abc.Mapping):
def __init__(self, d):
self._d = d

def __getitem__(self, key):
return self._d[key]

def __iter__(self):
return iter(self._d)

def __len__(self):
return len(self._d)

def __contains__(self, item):
return item in self._d

def __hash__(self):
return hash(frozenset(self._d.items()))


def make_immutable(thing):
if isinstance(thing, abc.Mapping):
return ImmutableDict(
{key: make_immutable(value) for key, value in thing.items()}
)

if isinstance(thing, str):
return thing

if isinstance(thing, abc.Collection):
return tuple(map(make_immutable, thing))

return thing
2 changes: 2 additions & 0 deletions gersemi/specializations/standard_command_dumper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from functools import lru_cache
from lark import Tree
from gersemi.command_line_formatter import CommandLineFormatter
from gersemi.keyword_with_pairs_formatter import KeywordWithPairsFormatter
Expand All @@ -17,6 +18,7 @@ def custom_command(self, tree):
return self.visit(Tree("command_invocation", [command_name, arguments]))


@lru_cache(maxsize=None)
def create_standard_dumper(data, custom_command=False):
bases = [
SectionAwareCommandInvocationDumper,
Expand Down

0 comments on commit da593a7

Please sign in to comment.