Skip to content

Commit d7724f9

Browse files
committed
Add cache for allocate_str
1 parent 03c2100 commit d7724f9

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

compiler/rustc_const_eval/src/interpret/place.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,20 @@ where
10011001
kind: MemoryKind<M::MemoryKind>,
10021002
mutbl: Mutability,
10031003
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
1004-
let ptr = self.allocate_bytes_ptr(str.as_bytes(), Align::ONE, kind, mutbl)?;
1004+
let tcx = self.tcx.tcx;
1005+
1006+
// Use cache for immutable strings.
1007+
let ptr = if mutbl.is_not()
1008+
&& matches!(kind, MemoryKind::CallerLocation | MemoryKind::Machine(_))
1009+
{
1010+
// Same id for cached allocations.
1011+
let id = tcx.allocate_bytes(str.as_bytes());
1012+
1013+
// Turn untagged "global" pointers (obtained via `tcx`) into the machine pointer to the allocation.
1014+
M::adjust_alloc_root_pointer(&self, Pointer::from(id), Some(kind))?
1015+
} else {
1016+
self.allocate_bytes_ptr(str.as_bytes(), Align::ONE, kind, mutbl)?
1017+
};
10051018
let meta = Scalar::from_target_usize(u64::try_from(str.len()).unwrap(), self);
10061019
let layout = self.layout_of(self.tcx.types.str_).unwrap();
10071020
Ok(self.ptr_with_meta_to_mplace(ptr.into(), MemPlaceMeta::Meta(meta), layout))

compiler/rustc_middle/src/mir/interpret/mod.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,6 @@ pub(crate) struct AllocMap<'tcx> {
393393
alloc_map: FxHashMap<AllocId, GlobalAlloc<'tcx>>,
394394

395395
/// Used to ensure that statics and functions only get one associated `AllocId`.
396-
/// Should never contain a `GlobalAlloc::Memory`!
397396
//
398397
// FIXME: Should we just have two separate dedup maps for statics and functions each?
399398
dedup: FxHashMap<GlobalAlloc<'tcx>, AllocId>,
@@ -433,14 +432,8 @@ impl<'tcx> TyCtxt<'tcx> {
433432
}
434433

435434
/// Reserves a new ID *if* this allocation has not been dedup-reserved before.
436-
/// Should only be used for "symbolic" allocations (function pointers, vtables, statics), we
437-
/// don't want to dedup IDs for "real" memory!
438435
fn reserve_and_set_dedup(self, alloc: GlobalAlloc<'tcx>) -> AllocId {
439436
let mut alloc_map = self.alloc_map.lock();
440-
match alloc {
441-
GlobalAlloc::Function { .. } | GlobalAlloc::Static(..) | GlobalAlloc::VTable(..) => {}
442-
GlobalAlloc::Memory(..) => bug!("Trying to dedup-reserve memory with real data!"),
443-
}
444437
if let Some(&alloc_id) = alloc_map.dedup.get(&alloc) {
445438
return alloc_id;
446439
}
@@ -451,6 +444,12 @@ impl<'tcx> TyCtxt<'tcx> {
451444
id
452445
}
453446

447+
/// Generates an `AllocId` for a memory allocation. If the exact same memory has been
448+
/// allocated before, this will return the same `AllocId`.
449+
pub fn reserve_and_set_memory_dedup(self, mem: ConstAllocation<'tcx>) -> AllocId {
450+
self.reserve_and_set_dedup(GlobalAlloc::Memory(mem))
451+
}
452+
454453
/// Generates an `AllocId` for a static or return a cached one in case this function has been
455454
/// called on the same static before.
456455
pub fn reserve_and_set_static_alloc(self, static_id: DefId) -> AllocId {

compiler/rustc_middle/src/ty/context.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1442,11 +1442,12 @@ impl<'tcx> TyCtxt<'tcx> {
14421442
}
14431443

14441444
/// Allocates a read-only byte or string literal for `mir::interpret`.
1445+
/// Returns the same `AllocId` if called again with the same bytes.
14451446
pub fn allocate_bytes(self, bytes: &[u8]) -> interpret::AllocId {
14461447
// Create an allocation that just contains these bytes.
14471448
let alloc = interpret::Allocation::from_bytes_byte_aligned_immutable(bytes);
14481449
let alloc = self.mk_const_alloc(alloc);
1449-
self.reserve_and_set_memory_alloc(alloc)
1450+
self.reserve_and_set_memory_dedup(alloc)
14501451
}
14511452

14521453
/// Returns a range of the start/end indices specified with the

0 commit comments

Comments
 (0)