mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
performance: Cache some properties
This commit is contained in:
parent
3f27e92eb0
commit
402677f687
2 changed files with 13 additions and 9 deletions
|
@ -76,6 +76,7 @@ class AstNode:
|
||||||
"""Base class for nodes in the abstract syntax tree."""
|
"""Base class for nodes in the abstract syntax tree."""
|
||||||
|
|
||||||
completers: T.List = []
|
completers: T.List = []
|
||||||
|
attrs_by_type: T.Dict[T.Type, T.List] = {}
|
||||||
|
|
||||||
def __init__(self, group, children, tokens, incomplete=False):
|
def __init__(self, group, children, tokens, incomplete=False):
|
||||||
self.group = group
|
self.group = group
|
||||||
|
@ -92,12 +93,13 @@ class AstNode:
|
||||||
cls.validators = [
|
cls.validators = [
|
||||||
getattr(cls, f) for f in dir(cls) if hasattr(getattr(cls, f), "_validator")
|
getattr(cls, f) for f in dir(cls) if hasattr(getattr(cls, f), "_validator")
|
||||||
]
|
]
|
||||||
|
cls.attrs_by_type = {}
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def context(self):
|
def context(self):
|
||||||
return Ctx(self)
|
return Ctx(self)
|
||||||
|
|
||||||
@property
|
@cached_property
|
||||||
def root(self):
|
def root(self):
|
||||||
if self.parent is None:
|
if self.parent is None:
|
||||||
return self
|
return self
|
||||||
|
@ -140,13 +142,14 @@ class AstNode:
|
||||||
for child in self.children:
|
for child in self.children:
|
||||||
yield from child._get_errors()
|
yield from child._get_errors()
|
||||||
|
|
||||||
def _attrs_by_type(
|
def _attrs_by_type(self, attr_type: T.Type[TAttr]) -> T.List[T.Tuple[str, TAttr]]:
|
||||||
self, attr_type: T.Type[TAttr]
|
if attr_type not in self.attrs_by_type:
|
||||||
) -> T.Iterator[T.Tuple[str, TAttr]]:
|
self.attrs_by_type[attr_type] = []
|
||||||
for name in dir(type(self)):
|
for name in dir(type(self)):
|
||||||
item = getattr(type(self), name)
|
item = getattr(type(self), name)
|
||||||
if isinstance(item, attr_type):
|
if isinstance(item, attr_type):
|
||||||
yield name, item
|
self.attrs_by_type[attr_type].append((name, item))
|
||||||
|
return self.attrs_by_type[attr_type]
|
||||||
|
|
||||||
def get_docs(self, idx: int) -> T.Optional[str]:
|
def get_docs(self, idx: int) -> T.Optional[str]:
|
||||||
for name, attr in self._attrs_by_type(Docs):
|
for name, attr in self._attrs_by_type(Docs):
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
#
|
#
|
||||||
# SPDX-License-Identifier: LGPL-3.0-or-later
|
# SPDX-License-Identifier: LGPL-3.0-or-later
|
||||||
|
|
||||||
|
from functools import cached_property
|
||||||
|
|
||||||
from .. import gir
|
from .. import gir
|
||||||
from .imports import GtkDirective, Import
|
from .imports import GtkDirective, Import
|
||||||
|
@ -82,7 +83,7 @@ class UI(AstNode):
|
||||||
or isinstance(child, Menu)
|
or isinstance(child, Menu)
|
||||||
]
|
]
|
||||||
|
|
||||||
@property
|
@cached_property
|
||||||
def objects_by_id(self):
|
def objects_by_id(self):
|
||||||
return {
|
return {
|
||||||
obj.tokens["id"]: obj
|
obj.tokens["id"]: obj
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue