Skip to content

Commit 60d668d

Browse files
authored
Merge pull request #55 from beda-software/optimize-performance
Optimize performance 4x
2 parents 744ec20 + 3dcaa5c commit 60d668d

7 files changed

Lines changed: 22 additions & 21 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.0.2
2+
3+
- Increase performance of parsing #54 @ruscoder
4+
15
## 2.0.1
26

37
- Fix of the bug with multiple user-defined functions @kpcurai @ruscoder

fhirpathpy/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from fhirpathpy.engine.nodes import FP_Type, ResourceNode
66

77
__title__ = "fhirpathpy"
8-
__version__ = "2.0.1"
8+
__version__ = "2.0.2"
99
__author__ = "beda.software"
1010
__license__ = "MIT"
1111
__copyright__ = "Copyright 2025 beda.software"
@@ -103,6 +103,4 @@ def compile(path, model=None, options=None):
103103
104104
For example, you could pass in the result of require("fhirpath/fhir-context/r4")
105105
"""
106-
return set_paths(
107-
apply_parsed_path, parsedPath=parse(path), model=model, options=options
108-
)
106+
return set_paths(apply_parsed_path, parsedPath=parse(path), model=model, options=options)

fhirpathpy/parser/ASTPathListener.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,32 @@
22
from fhirpathpy.parser.generated.FHIRPathListener import FHIRPathListener
33

44

5+
def has_node_type_text(node_type):
6+
# In general we need mostly terminal nodes (e.g. Identifier and any Literal)
7+
# But the code also uses TypeSpecifier, InvocationExpression and TermExpression
8+
return node_type.endswith("Literal") or node_type in [
9+
"LiteralTerm",
10+
"Identifier",
11+
"TypeSpecifier",
12+
"InvocationExpression",
13+
"TermExpression",
14+
]
15+
16+
517
class ASTPathListener(FHIRPathListener):
618
def __init__(self):
719
self.parentStack = [{}]
820

921
def pushNode(self, nodeType, ctx):
1022
parentNode = self.parentStack[-1]
11-
node = {"type": nodeType, "text": ctx.getText(), "terminalNodeText": []}
12-
23+
node = {"type": nodeType, "terminalNodeText": []}
24+
if has_node_type_text(nodeType):
25+
node["text"] = ctx.getText()
1326
for child in ctx.children:
1427
if isinstance(child, TerminalNodeImpl):
1528
node["terminalNodeText"].append(child.getText())
1629

17-
if not "children" in parentNode:
30+
if "children" not in parentNode:
1831
parentNode["children"] = []
1932

2033
parentNode["children"].append(node)

tests/fixtures/ast/%v+2.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
{
22
"children": [{
33
"type": "AdditiveExpression",
4-
"text": "%v+2",
54
"terminalNodeText": ["+"],
65
"children": [{
76
"type": "TermExpression",
87
"text": "%v",
98
"terminalNodeText": [],
109
"children": [{
1110
"type": "ExternalConstantTerm",
12-
"text": "%v",
1311
"terminalNodeText": [],
1412
"children": [{
1513
"type": "ExternalConstant",
16-
"text": "%v",
1714
"terminalNodeText": ["%"],
1815
"children": [{
1916
"type": "Identifier",

tests/fixtures/ast/Observation.value.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99
"terminalNodeText": [],
1010
"children": [{
1111
"type": "InvocationTerm",
12-
"text": "Observation",
1312
"terminalNodeText": [],
1413
"children": [{
1514
"type": "MemberInvocation",
16-
"text": "Observation",
1715
"terminalNodeText": [],
1816
"children": [{
1917
"type": "Identifier",
@@ -24,7 +22,6 @@
2422
}]
2523
}, {
2624
"type": "MemberInvocation",
27-
"text": "value",
2825
"terminalNodeText": [],
2926
"children": [{
3027
"type": "Identifier",

tests/fixtures/ast/Patient.name.given.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313
"terminalNodeText": [],
1414
"children": [{
1515
"type": "InvocationTerm",
16-
"text": "Patient",
1716
"terminalNodeText": [],
1817
"children": [{
1918
"type": "MemberInvocation",
20-
"text": "Patient",
2119
"terminalNodeText": [],
2220
"children": [{
2321
"type": "Identifier",
@@ -28,7 +26,6 @@
2826
}]
2927
}, {
3028
"type": "MemberInvocation",
31-
"text": "name",
3229
"terminalNodeText": [],
3330
"children": [{
3431
"type": "Identifier",
@@ -38,7 +35,6 @@
3835
}]
3936
}, {
4037
"type": "MemberInvocation",
41-
"text": "given",
4238
"terminalNodeText": [],
4339
"children": [{
4440
"type": "Identifier",

tests/fixtures/ast/a.b+2.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"children": [{
33
"type": "AdditiveExpression",
4-
"text": "a.b+2",
54
"terminalNodeText": ["+"],
65
"children": [{
76
"type": "InvocationExpression",
@@ -13,11 +12,9 @@
1312
"terminalNodeText": [],
1413
"children": [{
1514
"type": "InvocationTerm",
16-
"text": "a",
1715
"terminalNodeText": [],
1816
"children": [{
1917
"type": "MemberInvocation",
20-
"text": "a",
2118
"terminalNodeText": [],
2219
"children": [{
2320
"type": "Identifier",
@@ -28,7 +25,6 @@
2825
}]
2926
}, {
3027
"type": "MemberInvocation",
31-
"text": "b",
3228
"terminalNodeText": [],
3329
"children": [{
3430
"type": "Identifier",

0 commit comments

Comments
 (0)