Skip to content

Commit f6bcac8

Browse files
committed
use of jmespath.compile to go slightly faster
1 parent 202c4f3 commit f6bcac8

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

src/mmirage/core/process/variables.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,24 @@
88
from types import MappingProxyType
99
from typing import Any, Dict, List, Literal, Optional, Sequence
1010

11-
from jmespath import search
11+
import jmespath
1212
from PIL import Image
1313

14+
# Cache compiled JMESPath expressions to avoid recompilation across samples.
15+
_JMESPATH_CACHE: Dict[str, jmespath.parser.ParsedResult] = dict()
16+
17+
18+
def _get_compiled_query(key: str):
19+
"""Get (and cache) a compiled JMESPath expression for "key".
20+
21+
This avoids recompiling the same expression for each sample.
22+
"""
23+
expr: Optional[jmespath.parser.ParsedResult] = _JMESPATH_CACHE.get(key)
24+
if expr is None:
25+
expr = jmespath.compile(key)
26+
_JMESPATH_CACHE[key] = expr
27+
return expr
28+
1429

1530
@dataclass
1631
class BaseVar(abc.ABC):
@@ -236,7 +251,8 @@ def from_input_variables(sample: Dict[str, Any], input_vars: List[InputVar], ima
236251
image_vars: set = set()
237252

238253
for input_var in input_vars:
239-
value = search(input_var.key, sample)
254+
compiled_query = _get_compiled_query(input_var.key)
255+
value = compiled_query.search(sample)
240256
if value is None:
241257
raise ValueError(
242258
f"Input variable '{input_var.name}' with key '{input_var.key}' not found in the sample."

0 commit comments

Comments
 (0)