Skip to content

Commit b9e0ecd

Browse files
committed
transmutability: merge contiguous runs with a common destination
1 parent 88a8679 commit b9e0ecd

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

compiler/rustc_transmute/src/layout/dfa.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ where
266266
}
267267

268268
#[cfg(test)]
269-
pub(crate) fn from_edges<B: Copy + Into<Byte>>(
269+
pub(crate) fn from_edges<B: Clone + Into<Byte>>(
270270
start: u32,
271271
accept: u32,
272272
edges: &[(u32, B, u32)],
@@ -275,8 +275,8 @@ where
275275
let accept = State(accept);
276276
let mut transitions: Map<State, Vec<(Byte, State)>> = Map::default();
277277

278-
for (src, edge, dst) in edges.iter().copied() {
279-
transitions.entry(State(src)).or_default().push((edge.into(), State(dst)));
278+
for &(src, ref edge, dst) in edges.iter() {
279+
transitions.entry(State(src)).or_default().push((edge.clone().into(), State(dst)));
280280
}
281281

282282
let transitions = transitions
@@ -401,12 +401,24 @@ mod edge_set {
401401
mut join: impl FnMut(Option<S>, Option<S>) -> S,
402402
) -> EdgeSet<S>
403403
where
404-
S: Copy,
404+
S: Copy + Eq,
405405
{
406+
let mut runs: SmallVec<[(Byte, S); 1]> = SmallVec::new();
406407
let xs = self.runs.iter().copied();
407408
let ys = other.runs.iter().copied();
408-
// FIXME(@joshlf): Merge contiguous runs with common destination.
409-
EdgeSet { runs: union(xs, ys).map(|(range, (x, y))| (range, join(x, y))).collect() }
409+
for (range, (x, y)) in union(xs, ys) {
410+
let state = join(x, y);
411+
match runs.last_mut() {
412+
// Merge contiguous runs with a common destination.
413+
Some(&mut (ref mut last_range, ref mut last_state))
414+
if last_range.end == range.start && *last_state == state =>
415+
{
416+
last_range.end = range.end
417+
}
418+
_ => runs.push((range, state)),
419+
}
420+
}
421+
EdgeSet { runs }
410422
}
411423
}
412424
}

compiler/rustc_transmute/src/maybe_transmutable/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ mod union {
314314
let u = s.clone().union(t.clone(), new_state);
315315

316316
let expected_u =
317-
Dfa::from_edges(b, a, &[(b, 0, c), (b, 1, d), (d, 1, a), (d, 0, a), (c, 0, a)]);
317+
Dfa::from_edges(b, a, &[(b, 0..=0, c), (b, 1..=1, d), (d, 0..=1, a), (c, 0..=0, a)]);
318318

319319
assert_eq!(u, expected_u);
320320

0 commit comments

Comments
 (0)