Skip to content

Commit 6b6f5f0

Browse files
committed
refactor: performance optimizations
Signed-off-by: Louis Mandel <[email protected]>
1 parent 307ea66 commit 6b6f5f0

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

src/pdl/pdl_interpreter.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -561,16 +561,14 @@ def process_advance_block_retry( # noqa: C901
561561
if requirements_satisfied is False:
562562
continue
563563
break
564+
except KeyboardInterrupt as exc:
565+
raise exc from exc
564566
except Exception as exc:
565-
err_msg = traceback.format_exc()
566-
do_retry = (
567-
block.retry
568-
and trial_idx + 1 < trial_total
569-
and "Keyboard Interrupt" not in err_msg
570-
)
567+
do_retry = block.retry and trial_idx + 1 < trial_total
571568
if block.fallback is None and not do_retry:
572569
raise exc from exc
573570
if do_retry:
571+
err_msg = traceback.format_exc()
574572
error = f"An error occurred in a PDL block. Error details: {err_msg}"
575573
print(
576574
f"\n\033[0;31m[Retry {trial_idx+1}/{max_retry}] {error}\033[0m\n",
@@ -1395,14 +1393,15 @@ def process_blocks( # pylint: disable=too-many-arguments,too-many-positional-ar
13951393
saved_background: LazyMessages = DependentContext([])
13961394
trace = []
13971395
pdl_context_init: LazyMessages = scope.data["pdl_context"]
1396+
is_last_of = isinstance(join_type, JoinLastOf)
13981397
try:
13991398
for i, block in enumerate(blocks):
14001399
iteration_state = iteration_state_init.with_iter(i)
14011400
scope = scope | {
14021401
"pdl_context": DependentContext([pdl_context_init, background])
14031402
}
14041403
new_loc = append(loc, "[" + str(i) + "]")
1405-
if isinstance(join_type, JoinLastOf) and state.yield_result:
1404+
if is_last_of and state.yield_result:
14061405
iteration_state = state.with_yield_result(i + 1 == len(blocks))
14071406
(
14081407
iteration_result,

src/pdl/pdl_parser.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from functools import lru_cache
23
from pathlib import Path
34
from typing import Any, Optional
45

@@ -20,6 +21,7 @@ def parse_file(pdl_file: str | Path) -> tuple[Program, PdlLocationType]:
2021
return parse_str(prog_str, file_name=str(pdl_file))
2122

2223

24+
@lru_cache(maxsize=128)
2325
def parse_str(
2426
pdl_str: str, file_name: Optional[str] = None
2527
) -> tuple[Program, PdlLocationType]:

0 commit comments

Comments
 (0)