Skip to content

Commit

Permalink
Fix array closurization just forgetting common contracts (#1678)
Browse files Browse the repository at this point in the history
* Fix array closurization just forgetting common contracts

* Reduce the number of iterator combinators
  • Loading branch information
yannham authored Oct 13, 2023
1 parent 97e18af commit 7e4ecd9
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions core/src/eval/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1836,7 +1836,7 @@ impl<R: ImportResolver, C: Cache> VirtualMachine<R, C> {

// We basically compute the intersection (`ctr_common`),
// `ctrs_left - ctr_common`, and `ctrs_right - ctr_common`.
let ctrs_left : Vec<_> =
let ctrs_left_dedup : Vec<_> =
ctrs_left
.into_iter()
.filter(|ctr| {
Expand All @@ -1851,20 +1851,38 @@ impl<R: ImportResolver, C: Cache> VirtualMachine<R, C> {
initial_env: &self.initial_env,
};

// We check if there is a remaining contract in
// `ctrs_right_sieve` which matches `ctr`: in this case,
// `twin_index` will hold its index.
let twin_index = ctrs_right_sieve
.iter()
.filter_map(|ctr| ctr.as_ref())
.position(|other_ctr| {
let envs_right = EvalEnvsRef {
eval_env: &env2,
initial_env: &self.initial_env,
};

contract_eq::<EvalEnvsRef>(0, &ctr.contract, envs_left, &other_ctr.contract, envs_right)
});
other_ctr
.as_ref()
.map_or(false, |other_ctr| {
let envs_right = EvalEnvsRef {
eval_env: &env2,
initial_env: &self.initial_env,
};

contract_eq::<EvalEnvsRef>(
0,
&ctr.contract,
envs_left,
&other_ctr.contract,
envs_right,
)
})
});

if let Some(index) = twin_index {
ctrs_right_sieve[index] = None;
// unwrap(): we know that the contract at this index is
// `Some`, because all elements are initially some when
// creating `ctrs_right_sieve` and then we don't
// consider `None` values when computing a new `index`
// in the `position` above.
let common = ctrs_right_sieve[index].take().unwrap();
ctrs_common.push(common);
false
}
else {
Expand All @@ -1873,15 +1891,15 @@ impl<R: ImportResolver, C: Cache> VirtualMachine<R, C> {
})
.collect();

let ctrs_right = ctrs_right_sieve.into_iter().flatten();
let ctrs_right_dedup = ctrs_right_sieve.into_iter().flatten();

ts.extend(ts1.into_iter().map(|t|
RuntimeContract::apply_all(t, ctrs_left.iter().cloned(), pos1)
RuntimeContract::apply_all(t, ctrs_left_dedup.iter().cloned(), pos1)
.closurize(&mut self.cache, &mut env, env1.clone())
));

ts.extend(ts2.into_iter().map(|t|
RuntimeContract::apply_all(t, ctrs_right.clone(), pos2)
RuntimeContract::apply_all(t, ctrs_right_dedup.clone(), pos2)
.closurize(&mut self.cache, &mut env, env2.clone())
));

Expand Down

0 comments on commit 7e4ecd9

Please sign in to comment.