Skip to content

Commit 227a9fd

Browse files
authored
Cache HashSet in try_to_allocate_bundle_to_reg (#90)
Keep `conflict_set` allocated in `Env` instead of allocating a new one on every call. This improves register allocation performance by about 2%.
1 parent 67f5c16 commit 227a9fd

3 files changed

Lines changed: 10 additions & 3 deletions

File tree

src/ion/data_structures.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::{
2020
define_index, Allocation, Block, Edit, Function, Inst, MachineEnv, Operand, PReg, ProgPoint,
2121
RegClass, VReg,
2222
};
23+
use fxhash::FxHashSet;
2324
use smallvec::SmallVec;
2425
use std::cmp::Ordering;
2526
use std::collections::{BTreeMap, HashMap, HashSet};
@@ -430,6 +431,10 @@ pub struct Env<'a, F: Function> {
430431
// ProgPoint to insert into the final allocated program listing.
431432
pub debug_annotations: std::collections::HashMap<ProgPoint, Vec<String>>,
432433
pub annotations_enabled: bool,
434+
435+
// Cached allocation for `try_to_allocate_bundle_to_reg` to avoid allocating
436+
// a new HashSet on every call.
437+
pub conflict_set: FxHashSet<LiveBundleIndex>,
433438
}
434439

435440
impl<'a, F: Function> Env<'a, F> {

src/ion/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ impl<'a, F: Function> Env<'a, F> {
8787

8888
debug_annotations: std::collections::HashMap::new(),
8989
annotations_enabled,
90+
91+
conflict_set: Default::default(),
9092
}
9193
}
9294

src/ion/process.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl<'a, F: Function> Env<'a, F> {
6161
) -> AllocRegResult {
6262
trace!("try_to_allocate_bundle_to_reg: {:?} -> {:?}", bundle, reg);
6363
let mut conflicts = smallvec![];
64-
let mut conflict_set = FxHashSet::default();
64+
self.conflict_set.clear();
6565
let mut max_conflict_weight = 0;
6666
// Traverse the BTreeMap in order by requesting the whole
6767
// range spanned by the bundle and iterating over that
@@ -157,9 +157,9 @@ impl<'a, F: Function> Env<'a, F> {
157157
// conflicts list.
158158
let conflict_bundle = self.ranges[preg_range.index()].bundle;
159159
trace!(" -> conflict bundle {:?}", conflict_bundle);
160-
if !conflict_set.contains(&conflict_bundle) {
160+
if !self.conflict_set.contains(&conflict_bundle) {
161161
conflicts.push(conflict_bundle);
162-
conflict_set.insert(conflict_bundle);
162+
self.conflict_set.insert(conflict_bundle);
163163
max_conflict_weight = std::cmp::max(
164164
max_conflict_weight,
165165
self.bundles[conflict_bundle.index()].cached_spill_weight(),

0 commit comments

Comments
 (0)