Skip to content

Commit 5d516f9

Browse files
committed
Use Vec instead of HashSet for ccx.todo
This reduces runtime of ConstantCx::finalize for the coercions rustc bench by ~65% cc #878
1 parent efe3d84 commit 5d516f9

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

src/constant.rs

+10-18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc::mir::interpret::{
77
read_target_uint, AllocId, Allocation, ConstValue, GlobalAlloc, InterpResult, Scalar,
88
};
99
use rustc::ty::{layout::Align, Const, ConstKind};
10+
use rustc_data_structures::fx::FxHashSet;
1011
use rustc_mir::interpret::{
1112
ImmTy, InterpCx, Machine, Memory, MemoryKind, OpTy, PlaceTy, Pointer,
1213
StackPopCleanup, StackPopInfo,
@@ -19,11 +20,11 @@ use crate::prelude::*;
1920

2021
#[derive(Default)]
2122
pub struct ConstantCx {
22-
todo: HashSet<TodoItem>,
23-
done: HashSet<DataId>,
23+
todo: Vec<TodoItem>,
24+
done: FxHashSet<DataId>,
2425
}
2526

26-
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
27+
#[derive(Copy, Clone, Debug)]
2728
enum TodoItem {
2829
Alloc(AllocId),
2930
Static(DefId),
@@ -39,7 +40,7 @@ impl ConstantCx {
3940
}
4041

4142
pub fn codegen_static(constants_cx: &mut ConstantCx, def_id: DefId) {
42-
constants_cx.todo.insert(TodoItem::Static(def_id));
43+
constants_cx.todo.push(TodoItem::Static(def_id));
4344
}
4445

4546
fn codegen_static_ref<'tcx>(
@@ -109,7 +110,7 @@ pub fn trans_const_value<'tcx>(
109110
let alloc_kind = fx.tcx.alloc_map.lock().get(ptr.alloc_id);
110111
let base_addr = match alloc_kind {
111112
Some(GlobalAlloc::Memory(alloc)) => {
112-
fx.constants_cx.todo.insert(TodoItem::Alloc(ptr.alloc_id));
113+
fx.constants_cx.todo.push(TodoItem::Alloc(ptr.alloc_id));
113114
let data_id = data_id_for_alloc_id(fx.module, ptr.alloc_id, alloc.align);
114115
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
115116
#[cfg(debug_assertions)]
@@ -139,7 +140,7 @@ pub fn trans_const_value<'tcx>(
139140
}
140141
ConstValue::ByRef { alloc, offset } => {
141142
let alloc_id = fx.tcx.alloc_map.lock().create_memory_alloc(alloc);
142-
fx.constants_cx.todo.insert(TodoItem::Alloc(alloc_id));
143+
fx.constants_cx.todo.push(TodoItem::Alloc(alloc_id));
143144
let data_id = data_id_for_alloc_id(fx.module, alloc_id, alloc.align);
144145
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
145146
let global_ptr = fx.bcx.ins().global_value(fx.pointer_type, local_data_id);
@@ -195,7 +196,7 @@ fn trans_const_place<'tcx>(
195196

196197
//println!("const value: {:?} allocation: {:?}", value, alloc);
197198
let alloc_id = fx.tcx.alloc_map.lock().create_memory_alloc(alloc);
198-
fx.constants_cx.todo.insert(TodoItem::Alloc(alloc_id));
199+
fx.constants_cx.todo.push(TodoItem::Alloc(alloc_id));
199200
let data_id = data_id_for_alloc_id(fx.module, alloc_id, alloc.align);
200201
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
201202
#[cfg(debug_assertions)]
@@ -287,7 +288,7 @@ fn cplace_for_dataid<'tcx>(
287288
fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut Module<impl Backend>, cx: &mut ConstantCx) {
288289
let memory = Memory::<TransPlaceInterpreter>::new(tcx.at(DUMMY_SP), ());
289290

290-
while let Some(todo_item) = pop_set(&mut cx.todo) {
291+
while let Some(todo_item) = cx.todo.pop() {
291292
let (data_id, alloc) = match todo_item {
292293
TodoItem::Alloc(alloc_id) => {
293294
//println!("alloc_id {}", alloc_id);
@@ -356,7 +357,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut Module<impl Backend>, cx: &mu
356357
continue;
357358
}
358359
GlobalAlloc::Memory(_) => {
359-
cx.todo.insert(TodoItem::Alloc(reloc));
360+
cx.todo.push(TodoItem::Alloc(reloc));
360361
data_id_for_alloc_id(module, reloc, alloc.align)
361362
}
362363
GlobalAlloc::Static(def_id) => {
@@ -387,15 +388,6 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut Module<impl Backend>, cx: &mu
387388
assert!(cx.todo.is_empty(), "{:?}", cx.todo);
388389
}
389390

390-
fn pop_set<T: Copy + Eq + ::std::hash::Hash>(set: &mut HashSet<T>) -> Option<T> {
391-
if let Some(elem) = set.iter().next().map(|elem| *elem) {
392-
set.remove(&elem);
393-
Some(elem)
394-
} else {
395-
None
396-
}
397-
}
398-
399391
struct TransPlaceInterpreter;
400392

401393
impl<'mir, 'tcx> Machine<'mir, 'tcx> for TransPlaceInterpreter {

0 commit comments

Comments
 (0)