Skip to content

Commit 7f127b3

Browse files
authored
Merge pull request #58 from microsoft/optimizations
Optimizations
2 parents 005034c + 09cedba commit 7f127b3

File tree

8 files changed

+29
-11
lines changed

8 files changed

+29
-11
lines changed

flowquery-py/src/graph/data.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ def next(self) -> bool:
3131
return False
3232

3333
def clone(self) -> "IndexEntry":
34-
"""Create a copy of this index entry."""
35-
return IndexEntry(list(self._positions))
34+
"""Create a copy of this index entry.
35+
36+
Shares the underlying positions list since it is immutable after
37+
index construction. Only the iteration cursor is independent.
38+
"""
39+
return IndexEntry(self._positions)
3640

3741

3842
class Layer:

flowquery-py/src/graph/node.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ def _matches_properties(self, hop: int = 0) -> bool:
8383
raise ValueError("No current node data available")
8484
if key not in record:
8585
return False
86-
return bool(record[key] == expression.value())
86+
if record[key] != expression.value():
87+
return False
8788
return True
8889

8990
def set_value(self, value: Optional[Dict[str, Any]]) -> None:

flowquery-py/src/graph/relationship.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ def _matches_properties(self, hop: int = 0) -> bool:
8181
raise ValueError("No current relationship data available")
8282
if key not in record:
8383
raise ValueError("Relationship does not have property")
84-
return bool(record[key] == expression.value())
84+
if record[key] != expression.value():
85+
return False
8586
return True
8687

8788
@property

flowquery-py/src/graph/relationship_match_collector.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class RelationshipMatchCollector:
2121
def __init__(self) -> None:
2222
self._matches: List[RelationshipMatchRecord] = []
2323
self._node_ids: List[str] = []
24+
self._node_id_set: set[str] = set()
2425

2526
def push(self, relationship: 'Relationship', traversal_id: str = "") -> RelationshipMatchRecord:
2627
"""Push a new match onto the collector."""
@@ -42,6 +43,7 @@ def push(self, relationship: 'Relationship', traversal_id: str = "") -> Relation
4243
}
4344
self._matches.append(match)
4445
self._node_ids.append(traversal_id)
46+
self._node_id_set.add(traversal_id)
4547
return match
4648

4749
@property
@@ -61,7 +63,8 @@ def end_node(self, node: 'Node') -> None:
6163
def pop(self) -> Optional[RelationshipMatchRecord]:
6264
"""Pop the last match from the collector."""
6365
if self._node_ids:
64-
self._node_ids.pop()
66+
removed_id = self._node_ids.pop()
67+
self._node_id_set.discard(removed_id)
6568
if self._matches:
6669
return self._matches.pop()
6770
return None
@@ -82,4 +85,4 @@ def matches(self) -> List[RelationshipMatchRecord]:
8285

8386
def is_circular(self, next_id: str = "") -> bool:
8487
"""Check if traversing to the given node id would form a cycle."""
85-
return next_id in self._node_ids
88+
return next_id in self._node_id_set

src/graph/data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class IndexEntry {
2222
return false;
2323
}
2424
public clone(): IndexEntry {
25-
return new IndexEntry([...this._positions]);
25+
return new IndexEntry(this._positions);
2626
}
2727
}
2828

src/graph/node.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ class Node extends ASTNode {
6666
if (!(key in record)) {
6767
return false;
6868
}
69-
return record[key] === expression.value();
69+
if (record[key] !== expression.value()) {
70+
return false;
71+
}
7072
}
7173
return true;
7274
}

src/graph/relationship.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ class Relationship extends ASTNode {
6363
if (!(key in record)) {
6464
throw new Error("Relationship does not have property");
6565
}
66-
return record[key] === expression.value();
66+
if (record[key] !== expression.value()) {
67+
return false;
68+
}
6769
}
6870
return true;
6971
}

src/graph/relationship_match_collector.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export type RelationshipMatchRecord = {
1111
class RelationshipMatchCollector {
1212
private _matches: RelationshipMatchRecord[] = [];
1313
private _nodeIds: Array<string> = [];
14+
private _nodeIdSet: Set<string> = new Set();
1415

1516
public push(relationship: Relationship, traversalId: string): RelationshipMatchRecord {
1617
const data = relationship.getData();
@@ -27,6 +28,7 @@ class RelationshipMatchCollector {
2728
};
2829
this._matches.push(match);
2930
this._nodeIds.push(traversalId);
31+
this._nodeIdSet.add(traversalId);
3032
return match;
3133
}
3234
public set endNode(node: any) {
@@ -35,7 +37,10 @@ class RelationshipMatchCollector {
3537
}
3638
}
3739
public pop(): RelationshipMatchRecord | undefined {
38-
this._nodeIds.pop();
40+
const removedId = this._nodeIds.pop();
41+
if (removedId !== undefined) {
42+
this._nodeIdSet.delete(removedId);
43+
}
3944
return this._matches.pop();
4045
}
4146
public value(): RelationshipMatchRecord | RelationshipMatchRecord[] | null {
@@ -57,7 +62,7 @@ class RelationshipMatchCollector {
5762
** in the current traversal path
5863
*/
5964
public isCircular(nextId: string): boolean {
60-
return this._nodeIds.includes(nextId);
65+
return this._nodeIdSet.has(nextId);
6166
}
6267
}
6368

0 commit comments

Comments
 (0)