Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
move dataclass def (10x speedup)
Browse files Browse the repository at this point in the history
  • Loading branch information
iopapamanoglou committed Sep 20, 2024
1 parent 7f2191b commit a28043a
Showing 1 changed file with 59 additions and 65 deletions.
124 changes: 59 additions & 65 deletions src/faebryk/core/moduleinterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,61 @@ class PathStackElement:
name: str
up: bool

@dataclass
class UnresolvedStackElement:
elem: "_PathFinder.PathStackElement"
promise: bool

def match(self, elem: "_PathFinder.PathStackElement"):
return (
self.elem.parent_type == elem.parent_type
and self.elem.child_type == elem.child_type
and self.elem.name == elem.name
and self.elem.up != elem.up
)

def try_compress(self, elem: "_PathFinder.PathStackElement"):
"""
```
last = MIF -> Power
elem = Power -> ElectricPower
=> MIF -> ElectricPower
```
"""
partial_resolution_allowed = isinstance(
elem.parent_gif, GraphInterfaceHierarchicalModuleSpecial
) and isinstance(
self.elem.parent_gif, GraphInterfaceHierarchicalModuleSpecial
)
if not partial_resolution_allowed:
return False

if elem.up and self.elem.up and elem.child_type is self.elem.parent_type:
self.elem.parent_type = elem.parent_type
return True

if (
not elem.up
and not self.elem.up
and elem.parent_type is self.elem.child_type
):
self.elem.child_type = elem.child_type
return True

if (
not elem.up
and self.elem.up
and elem.parent_type is self.elem.parent_type
):
self.elem.parent_type = elem.child_type
return True

if elem.up and not self.elem.up and elem.child_type is self.elem.child_type:
self.elem.child_type = elem.parent_type
return True

return False

type PathStack = list[PathStackElement]
type Path = list[GraphInterface]

Expand Down Expand Up @@ -125,70 +180,7 @@ def _get_path_hierarchy_stack(path: Path) -> PathStack:

@staticmethod
def _fold_stack(stack: PathStack):
@dataclass
class UnresolvedStackElement:
elem: _PathFinder.PathStackElement
promise: bool

def match(self, elem: _PathFinder.PathStackElement):
return (
self.elem.parent_type == elem.parent_type
and self.elem.child_type == elem.child_type
and self.elem.name == elem.name
and self.elem.up != elem.up
)

def try_compress(self, elem: _PathFinder.PathStackElement):
"""
```
last = MIF -> Power
elem = Power -> ElectricPower
=> MIF -> ElectricPower
```
"""
partial_resolution_allowed = isinstance(
elem.parent_gif, GraphInterfaceHierarchicalModuleSpecial
) and isinstance(
self.elem.parent_gif, GraphInterfaceHierarchicalModuleSpecial
)
if not partial_resolution_allowed:
return False

if (
elem.up
and self.elem.up
and elem.child_type is self.elem.parent_type
):
self.elem.parent_type = elem.parent_type
return True

if (
not elem.up
and not self.elem.up
and elem.parent_type is self.elem.child_type
):
self.elem.child_type = elem.child_type
return True

if (
not elem.up
and self.elem.up
and elem.parent_type is self.elem.parent_type
):
self.elem.parent_type = elem.child_type
return True

if (
elem.up
and not self.elem.up
and elem.child_type is self.elem.child_type
):
self.elem.child_type = elem.parent_type
return True

return False

unresolved_stack: list[UnresolvedStackElement] = []
unresolved_stack: list[_PathFinder.UnresolvedStackElement] = []
promise_stack: list[_PathFinder.PathStackElement] = []
for elem in stack:
if unresolved_stack and unresolved_stack[-1].match(elem):
Expand All @@ -208,7 +200,9 @@ def try_compress(self, elem: _PathFinder.PathStackElement):
> 1
)
if not unresolved_stack or not unresolved_stack[-1].try_compress(elem):
unresolved_stack.append(UnresolvedStackElement(elem, promise))
unresolved_stack.append(
_PathFinder.UnresolvedStackElement(elem, promise)
)

if promise:
promise_stack.append(elem)
Expand Down

0 comments on commit a28043a

Please sign in to comment.