Skip to content

Commit aace842

Browse files
committed
Get rid of literal_alloc_cache
1 parent 56e541d commit aace842

File tree

6 files changed

+63
-28
lines changed

6 files changed

+63
-28
lines changed

src/librustc/ty/context.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -960,10 +960,6 @@ struct InterpretInternerInner<'tcx> {
960960
/// Inverse map of `statics`
961961
/// Used so we don't allocate a new pointer every time we need one
962962
static_cache: FxHashMap<DefId, interpret::AllocId>,
963-
964-
/// A cache for basic byte allocations keyed by their contents. This is used to deduplicate
965-
/// allocations for string and bytestring literals.
966-
literal_alloc_cache: FxHashMap<Vec<u8>, interpret::AllocId>,
967963
}
968964

969965
impl<'tcx> InterpretInterner<'tcx> {
@@ -1123,22 +1119,14 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
11231119
}
11241120

11251121
/// Allocates a byte or string literal for `mir::interpret`
1126-
pub fn allocate_cached(self, bytes: &[u8]) -> interpret::AllocId {
1127-
// check whether we already allocated this literal or a constant with the same memory
1128-
if let Some(&alloc_id) = self.interpret_interner.inner.borrow()
1129-
.literal_alloc_cache.get(bytes) {
1130-
return alloc_id;
1131-
}
1122+
pub fn allocate_bytes(self, bytes: &[u8]) -> interpret::AllocId {
11321123
// create an allocation that just contains these bytes
11331124
let alloc = interpret::Allocation::from_byte_aligned_bytes(bytes);
11341125
let alloc = self.intern_const_alloc(alloc);
11351126

11361127
// the next unique id
11371128
let id = self.interpret_interner.reserve();
1138-
// make the allocation identifiable
1139-
self.interpret_interner.inner.borrow_mut().alloc_by_id.insert(id, alloc);
1140-
// cache it for the future
1141-
self.interpret_interner.inner.borrow_mut().literal_alloc_cache.insert(bytes.to_owned(), id);
1129+
self.interpret_interner.intern_at_reserved(id, alloc);
11421130
id
11431131
}
11441132

src/librustc/ty/sty.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,6 +1860,14 @@ impl<'tcx> Const<'tcx> {
18601860
}
18611861
}
18621862

1863+
#[inline]
1864+
pub fn to_byval_value(&self) -> Option<Value> {
1865+
match self.val {
1866+
ConstVal::Value(val) => val.to_byval_value(),
1867+
_ => None,
1868+
}
1869+
}
1870+
18631871
#[inline]
18641872
pub fn to_primval(&self) -> Option<PrimVal> {
18651873
match self.val {

src/librustc_mir/hair/cx/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,15 @@ impl<'a, 'gcx, 'tcx> Cx<'a, 'gcx, 'tcx> {
181181
let lit = match *lit {
182182
LitKind::Str(ref s, _) => {
183183
let s = s.as_str();
184-
let id = self.tcx.allocate_cached(s.as_bytes());
184+
let id = self.tcx.allocate_bytes(s.as_bytes());
185185
let ptr = MemoryPointer::new(id, Size::from_bytes(0));
186186
ConstValue::ByValPair(
187187
PrimVal::Ptr(ptr),
188188
PrimVal::from_u128(s.len() as u128),
189189
)
190190
},
191191
LitKind::ByteStr(ref data) => {
192-
let id = self.tcx.allocate_cached(data);
192+
let id = self.tcx.allocate_bytes(data);
193193
let ptr = MemoryPointer::new(id, Size::from_bytes(0));
194194
ConstValue::ByVal(PrimVal::Ptr(ptr))
195195
},

src/librustc_mir/hair/pattern/mod.rs

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use interpret::{const_val_field, const_variant_index, self};
2020

2121
use rustc::middle::const_val::ConstVal;
2222
use rustc::mir::{fmt_const_val, Field, BorrowKind, Mutability};
23-
use rustc::mir::interpret::{PrimVal, GlobalId, ConstValue};
23+
use rustc::mir::interpret::{PrimVal, GlobalId, ConstValue, Value};
2424
use rustc::ty::{self, TyCtxt, AdtDef, Ty, Region};
2525
use rustc::ty::layout::Size;
2626
use rustc::ty::subst::{Substs, Kind};
@@ -1040,11 +1040,27 @@ pub fn compare_const_vals<'a, 'tcx>(
10401040
ty: Ty<'tcx>,
10411041
) -> Option<Ordering> {
10421042
trace!("compare_const_vals: {:?}, {:?}", a, b);
1043+
1044+
let from_bool = |v: bool| {
1045+
if v {
1046+
Some(Ordering::Equal)
1047+
} else {
1048+
None
1049+
}
1050+
};
1051+
1052+
let fallback = || from_bool(a == b);
1053+
1054+
// Use the fallback if any type differs
1055+
if a.ty != b.ty || a.ty != ty {
1056+
return fallback();
1057+
}
1058+
10431059
// FIXME: This should use assert_bits(ty) instead of use_bits
10441060
// but triggers possibly bugs due to mismatching of arrays and slices
10451061
if let (Some(a), Some(b)) = (a.to_bits(ty), b.to_bits(ty)) {
10461062
use ::rustc_apfloat::Float;
1047-
match ty.sty {
1063+
return match ty.sty {
10481064
ty::TyFloat(ast::FloatTy::F32) => {
10491065
let l = ::rustc_apfloat::ieee::Single::from_bits(a);
10501066
let r = ::rustc_apfloat::ieee::Single::from_bits(b);
@@ -1062,13 +1078,36 @@ pub fn compare_const_vals<'a, 'tcx>(
10621078
},
10631079
_ => Some(a.cmp(&b)),
10641080
}
1065-
} else {
1066-
if a == b {
1067-
Some(Ordering::Equal)
1068-
} else {
1069-
None
1081+
}
1082+
1083+
if let ty::TyRef(_, rty, _) = ty.sty {
1084+
if let ty::TyStr = rty.sty {
1085+
match (a.to_byval_value(), b.to_byval_value()) {
1086+
(
1087+
Some(Value::ByValPair(
1088+
PrimVal::Ptr(ptr_a),
1089+
PrimVal::Bytes(size_a))
1090+
),
1091+
Some(Value::ByValPair(
1092+
PrimVal::Ptr(ptr_b),
1093+
PrimVal::Bytes(size_b))
1094+
)
1095+
) if size_a == size_b => {
1096+
if ptr_a.offset == Size::from_bytes(0) && ptr_b.offset == Size::from_bytes(0) {
1097+
let map = tcx.alloc_map.lock();
1098+
let alloc_a = map.unwrap_memory(ptr_a.alloc_id);
1099+
let alloc_b = map.unwrap_memory(ptr_b.alloc_id);
1100+
if alloc_a.bytes.len() as u64 == size_a as u64 {
1101+
return from_bool(alloc_a == alloc_b);
1102+
}
1103+
}
1104+
}
1105+
_ => (),
1106+
}
10701107
}
10711108
}
1109+
1110+
fallback()
10721111
}
10731112

10741113
// FIXME: Combine with rustc_mir::hair::cx::const_eval_literal
@@ -1083,15 +1122,15 @@ fn lit_to_const<'a, 'tcx>(lit: &'tcx ast::LitKind,
10831122
let lit = match *lit {
10841123
LitKind::Str(ref s, _) => {
10851124
let s = s.as_str();
1086-
let id = tcx.allocate_cached(s.as_bytes());
1125+
let id = tcx.allocate_bytes(s.as_bytes());
10871126
let ptr = MemoryPointer::new(id, Size::from_bytes(0));
10881127
ConstValue::ByValPair(
10891128
PrimVal::Ptr(ptr),
10901129
PrimVal::from_u128(s.len() as u128),
10911130
)
10921131
},
10931132
LitKind::ByteStr(ref data) => {
1094-
let id = tcx.allocate_cached(data);
1133+
let id = tcx.allocate_bytes(data);
10951134
let ptr = MemoryPointer::new(id, Size::from_bytes(0));
10961135
ConstValue::ByVal(PrimVal::Ptr(ptr))
10971136
},

src/librustc_mir/interpret/eval_context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
229229
}
230230

231231
pub fn str_to_value(&mut self, s: &str) -> EvalResult<'tcx, Value> {
232-
let ptr = self.memory.allocate_cached(s.as_bytes());
232+
let ptr = self.memory.allocate_bytes(s.as_bytes());
233233
Ok(Value::ByValPair(
234234
PrimVal::Ptr(ptr),
235235
PrimVal::from_u128(s.len() as u128),

src/librustc_mir/interpret/memory.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
7676
MemoryPointer::new(id, Size::from_bytes(0))
7777
}
7878

79-
pub fn allocate_cached(&mut self, bytes: &[u8]) -> MemoryPointer {
80-
let id = self.tcx.allocate_cached(bytes);
79+
pub fn allocate_bytes(&mut self, bytes: &[u8]) -> MemoryPointer {
80+
let id = self.tcx.allocate_bytes(bytes);
8181
MemoryPointer::new(id, Size::from_bytes(0))
8282
}
8383

0 commit comments

Comments
 (0)