Skip to content

Commit 6967d50

Browse files
authored
Merge pull request #482 from ltratt/timeout_collect
Enforce the repair timeout when collecting repairs.
2 parents 8482641 + 9cab8a3 commit 6967d50

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

lrpar/src/lib/cpctplus.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,9 @@ where
244244
return (in_laidx, vec![]);
245245
}
246246

247-
let full_rprs = self.collect_repairs(in_laidx, astar_cnds);
247+
let Some(full_rprs) = self.collect_repairs(finish_by, in_laidx, astar_cnds) else {
248+
return (in_laidx, vec![]);
249+
};
248250
let mut rnk_rprs = rank_cnds(parser, finish_by, in_laidx, in_pstack, full_rprs);
249251
if rnk_rprs.is_empty() {
250252
return (in_laidx, vec![]);
@@ -372,19 +374,25 @@ where
372374
}
373375
}
374376

375-
/// Convert the output from `astar_all` into something more usable.
377+
/// Convert the output from `astar_all` into something more usable. Returns `None` if it timed
378+
/// out while doing so.
376379
fn collect_repairs(
377380
&self,
381+
finish_by: Instant,
378382
in_laidx: usize,
379383
cnds: Vec<PathFNode<StorageT>>,
380-
) -> Vec<Vec<Vec<ParseRepair<LexerTypesT::LexemeT, StorageT>>>> {
384+
) -> Option<Vec<Vec<Vec<ParseRepair<LexerTypesT::LexemeT, StorageT>>>>> {
381385
fn traverse<StorageT: PrimInt>(
386+
finish_by: Instant,
382387
rm: &Cactus<RepairMerge<StorageT>>,
383-
) -> Vec<Vec<Repair<StorageT>>> {
388+
) -> Option<Vec<Vec<Repair<StorageT>>>> {
389+
if Instant::now() >= finish_by {
390+
return None;
391+
}
384392
let mut out = Vec::new();
385393
match *rm.val().unwrap() {
386394
RepairMerge::Repair(r) => {
387-
let parents = traverse(&rm.parent().unwrap());
395+
let parents = traverse(finish_by, &rm.parent().unwrap())?;
388396
if parents.is_empty() {
389397
out.push(vec![r]);
390398
} else {
@@ -395,7 +403,7 @@ where
395403
}
396404
}
397405
RepairMerge::Merge(r, ref vc) => {
398-
let parents = traverse(&rm.parent().unwrap());
406+
let parents = traverse(finish_by, &rm.parent().unwrap())?;
399407
if parents.is_empty() {
400408
out.push(vec![r]);
401409
} else {
@@ -405,26 +413,26 @@ where
405413
}
406414
}
407415
for c in vc.vals() {
408-
for pc in traverse(c) {
416+
for pc in traverse(finish_by, c)? {
409417
out.push(pc);
410418
}
411419
}
412420
}
413421
RepairMerge::Terminator => (),
414422
}
415-
out
423+
Some(out)
416424
}
417425

418426
let mut all_rprs = Vec::with_capacity(cnds.len());
419427
for cnd in cnds {
420428
all_rprs.push(
421-
traverse(&cnd.repairs)
429+
traverse(finish_by, &cnd.repairs)?
422430
.into_iter()
423431
.map(|x| self.repair_to_parse_repair(in_laidx, &x))
424432
.collect::<Vec<_>>(),
425433
);
426434
}
427-
all_rprs
435+
Some(all_rprs)
428436
}
429437

430438
fn repair_to_parse_repair(

0 commit comments

Comments
 (0)