Skip to content

Commit d9c3189

Browse files
@facebook-github-bot
authored andcommitted
Return Cow instead of value for get_state_at_or_bottom in fixpoint iterator
Summary: Return a Cow value instead of always cloning for get_state_at_or_bottom. Reviewed By: NicholasGorski, yuxuanchen1997 Differential Revision: D40204793 fbshipit-source-id: 57139fe6e72a0e084770331cde4ffd75def43a34
1 parent af5fc7e commit d9c3189

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

rust/src/fixpoint_iter.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
use std::borrow::Cow;
89
use std::collections::HashMap;
910
use std::collections::HashSet;
1011
use std::collections::VecDeque;
@@ -218,15 +219,19 @@ where
218219
}
219220
}
220221

221-
fn get_state_at_or_bottom(states: &HashMap<G::NodeId, D>, n: G::NodeId) -> D {
222-
states.get(&n).cloned().unwrap_or_else(D::bottom)
222+
fn get_state_at_or_bottom(states: &HashMap<G::NodeId, D>, n: G::NodeId) -> Cow<'_, D> {
223+
if let Some(state) = states.get(&n) {
224+
Cow::Borrowed(state)
225+
} else {
226+
Cow::Owned(D::bottom())
227+
}
223228
}
224229

225-
pub fn get_entry_state_at(&self, n: G::NodeId) -> D {
230+
pub fn get_entry_state_at(&self, n: G::NodeId) -> Cow<'_, D> {
226231
Self::get_state_at_or_bottom(&self.entry_states, n)
227232
}
228233

229-
pub fn get_exit_state_at(&self, n: G::NodeId) -> D {
234+
pub fn get_exit_state_at(&self, n: G::NodeId) -> Cow<'_, D> {
230235
Self::get_state_at_or_bottom(&self.exit_states, n)
231236
}
232237

rust/tests/fixpoint_iter_test.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//! Tests borrowed from the MonotonicFixpointIteratorTest.cpp in the C++ version.
99
1010
mod liveness {
11+
use std::borrow::Cow;
1112
use std::collections::HashMap;
1213

1314
use im::HashSet;
@@ -147,8 +148,8 @@ mod liveness {
147148
}
148149

149150
trait LivenessAnalysis {
150-
fn get_live_in_vars_at(&self, n: NodeId) -> LivenessDomain;
151-
fn get_live_out_vars_at(&self, n: NodeId) -> LivenessDomain;
151+
fn get_live_in_vars_at(&self, n: NodeId) -> Cow<'_, LivenessDomain>;
152+
fn get_live_out_vars_at(&self, n: NodeId) -> Cow<'_, LivenessDomain>;
152153
}
153154

154155
type LivenessFixpointIterator<'g> = MonotonicFixpointIterator<
@@ -159,11 +160,11 @@ mod liveness {
159160
>;
160161

161162
impl<'g> LivenessAnalysis for LivenessFixpointIterator<'g> {
162-
fn get_live_in_vars_at(&self, n: NodeId) -> LivenessDomain {
163+
fn get_live_in_vars_at(&self, n: NodeId) -> Cow<'_, LivenessDomain> {
163164
self.get_exit_state_at(n)
164165
}
165166

166-
fn get_live_out_vars_at(&self, n: NodeId) -> LivenessDomain {
167+
fn get_live_out_vars_at(&self, n: NodeId) -> Cow<'_, LivenessDomain> {
167168
self.get_entry_state_at(n)
168169
}
169170
}
@@ -283,12 +284,12 @@ mod liveness {
283284
macro_rules! assert_analysis {
284285
( $fp:ident, $index:literal, [$($live_in_vars:literal),*], [$($live_out_vars:literal),*] ) => {
285286
assert!(matches!(
286-
$fp.get_exit_state_at($index),
287-
LivenessDomain::Value(_)
287+
$fp.get_exit_state_at($index).as_ref(),
288+
&LivenessDomain::Value(_)
288289
));
289290
assert!(matches!(
290-
$fp.get_entry_state_at($index),
291-
LivenessDomain::Value(_)
291+
$fp.get_entry_state_at($index).as_ref(),
292+
&LivenessDomain::Value(_)
292293
));
293294
assert_analysis_values!($fp, $index, get_live_in_vars_at, [$($live_in_vars),*]);
294295
assert_analysis_values!($fp, $index, get_live_out_vars_at, [$($live_out_vars),*]);
@@ -351,8 +352,14 @@ mod liveness {
351352
assert_analysis!(fp, 5, ["a", "b", "y"], ["a", "b", "x", "y"]);
352353

353354
// 7: x = y + a;
354-
assert!(matches!(fp.get_exit_state_at(6), LivenessDomain::Bottom));
355-
assert!(matches!(fp.get_entry_state_at(6), LivenessDomain::Bottom));
355+
assert!(matches!(
356+
fp.get_exit_state_at(6).as_ref(),
357+
&LivenessDomain::Bottom
358+
));
359+
assert!(matches!(
360+
fp.get_entry_state_at(6).as_ref(),
361+
&LivenessDomain::Bottom
362+
));
356363
}
357364

358365
#[test]
@@ -771,7 +778,7 @@ mod numerical {
771778

772779
let bb1 = 0;
773780
assert_eq!(
774-
fp.get_entry_state_at(bb1),
781+
fp.get_entry_state_at(bb1).into_owned(),
775782
IntegerSetAbstractEnvironment::top()
776783
);
777784
assert_eq!(
@@ -829,7 +836,7 @@ mod numerical {
829836

830837
let bb1 = 0;
831838
assert_eq!(
832-
fp.get_entry_state_at(bb1),
839+
fp.get_entry_state_at(bb1).into_owned(),
833840
IntegerSetAbstractEnvironment::top()
834841
);
835842
assert_eq!(

0 commit comments

Comments
 (0)