Skip to content

Commit 5125063

Browse files
committed
Remove unnecessary cloning
1 parent c9c4053 commit 5125063

File tree

7 files changed

+54
-50
lines changed

7 files changed

+54
-50
lines changed

crates/hir-ty/src/consteval.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,15 @@ pub fn intern_const_ref(
142142
LiteralConstRef::Int(i) => {
143143
// FIXME: We should handle failure of layout better.
144144
let size = layout.map(|it| it.size.bytes_usize()).unwrap_or(16);
145-
ConstScalar::Bytes(i.to_le_bytes()[0..size].to_vec(), MemoryMap::default())
145+
ConstScalar::Bytes(i.to_le_bytes()[0..size].into(), MemoryMap::default())
146146
}
147147
LiteralConstRef::UInt(i) => {
148148
let size = layout.map(|it| it.size.bytes_usize()).unwrap_or(16);
149-
ConstScalar::Bytes(i.to_le_bytes()[0..size].to_vec(), MemoryMap::default())
149+
ConstScalar::Bytes(i.to_le_bytes()[0..size].into(), MemoryMap::default())
150150
}
151-
LiteralConstRef::Bool(b) => ConstScalar::Bytes(vec![*b as u8], MemoryMap::default()),
151+
LiteralConstRef::Bool(b) => ConstScalar::Bytes(Box::new([*b as u8]), MemoryMap::default()),
152152
LiteralConstRef::Char(c) => {
153-
ConstScalar::Bytes((*c as u32).to_le_bytes().to_vec(), MemoryMap::default())
153+
ConstScalar::Bytes((*c as u32).to_le_bytes().into(), MemoryMap::default())
154154
}
155155
LiteralConstRef::Unknown => ConstScalar::Unknown,
156156
};

crates/hir-ty/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ impl MemoryMap {
219219
/// A concrete constant value
220220
#[derive(Debug, Clone, PartialEq, Eq)]
221221
pub enum ConstScalar {
222-
Bytes(Vec<u8>, MemoryMap),
222+
Bytes(Box<[u8]>, MemoryMap),
223223
// FIXME: this is a hack to get around chalk not being able to represent unevaluatable
224224
// constants
225225
UnevaluatedConst(GeneralConstId, Substitution),

crates/hir-ty/src/mir.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,16 @@ pub enum Operand {
9898
}
9999

100100
impl Operand {
101-
fn from_concrete_const(data: Vec<u8>, memory_map: MemoryMap, ty: Ty) -> Self {
101+
fn from_concrete_const(data: Box<[u8]>, memory_map: MemoryMap, ty: Ty) -> Self {
102102
Operand::Constant(intern_const_scalar(ConstScalar::Bytes(data, memory_map), ty))
103103
}
104104

105-
fn from_bytes(data: Vec<u8>, ty: Ty) -> Self {
105+
fn from_bytes(data: Box<[u8]>, ty: Ty) -> Self {
106106
Operand::from_concrete_const(data, MemoryMap::default(), ty)
107107
}
108108

109109
fn const_zst(ty: Ty) -> Operand {
110-
Self::from_bytes(vec![], ty)
110+
Self::from_bytes(Box::default(), ty)
111111
}
112112

113113
fn from_fn(
@@ -118,7 +118,7 @@ impl Operand {
118118
let ty =
119119
chalk_ir::TyKind::FnDef(CallableDefId::FunctionId(func_id).to_chalk(db), generic_args)
120120
.intern(Interner);
121-
Operand::from_bytes(vec![], ty)
121+
Operand::from_bytes(Box::default(), ty)
122122
}
123123
}
124124

crates/hir-ty/src/mir/eval.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -527,14 +527,15 @@ pub fn interpret_mir(
527527
if evaluator.ptr_size() != std::mem::size_of::<usize>() {
528528
not_supported!("targets with different pointer size from host");
529529
}
530-
let bytes = evaluator.interpret_mir(body.clone(), None.into_iter())?;
530+
let interval = evaluator.interpret_mir(body.clone(), None.into_iter())?;
531+
let bytes = interval.get(&evaluator)?;
531532
let mut memory_map = evaluator.create_memory_map(
532-
&bytes,
533+
bytes,
533534
&ty,
534535
&Locals { ptr: ArenaMap::new(), body, drop_flags: DropFlags::default() },
535536
)?;
536537
memory_map.vtable = evaluator.vtable_map.clone();
537-
return Ok(intern_const_scalar(ConstScalar::Bytes(bytes, memory_map), ty));
538+
return Ok(intern_const_scalar(ConstScalar::Bytes(bytes.into(), memory_map), ty));
538539
})();
539540
(
540541
it,
@@ -803,11 +804,11 @@ impl Evaluator<'_> {
803804
})
804805
}
805806

806-
fn interpret_mir(
807-
&mut self,
807+
fn interpret_mir<'slf>(
808+
&'slf mut self,
808809
body: Arc<MirBody>,
809810
args: impl Iterator<Item = IntervalOrOwned>,
810-
) -> Result<Vec<u8>> {
811+
) -> Result<Interval> {
811812
if let Some(it) = self.stack_depth_limit.checked_sub(1) {
812813
self.stack_depth_limit = it;
813814
} else {
@@ -957,7 +958,7 @@ impl Evaluator<'_> {
957958
None => {
958959
self.code_stack = prev_code_stack;
959960
self.stack_depth_limit += 1;
960-
return Ok(return_interval.get(self)?.to_vec());
961+
return Ok(return_interval);
961962
}
962963
Some(bb) => {
963964
// We don't support const promotion, so we can't truncate the stack yet.
@@ -2173,15 +2174,15 @@ impl Evaluator<'_> {
21732174
let arg_bytes = iter::once(Ok(closure_data))
21742175
.chain(args.iter().map(|it| Ok(it.get(&self)?.to_owned())))
21752176
.collect::<Result<Vec<_>>>()?;
2176-
let bytes = self
2177+
let interval = self
21772178
.interpret_mir(mir_body, arg_bytes.into_iter().map(IntervalOrOwned::Owned))
21782179
.map_err(|e| {
21792180
MirEvalError::InFunction(
21802181
Box::new(e),
21812182
vec![(Either::Right(closure), span, locals.body.owner)],
21822183
)
21832184
})?;
2184-
destination.write_from_bytes(self, &bytes)?;
2185+
destination.write_from_interval(self, interval)?;
21852186
Ok(None)
21862187
}
21872188

@@ -2374,7 +2375,7 @@ impl Evaluator<'_> {
23742375
vec![(Either::Left(def), span, locals.body.owner)],
23752376
)
23762377
})?;
2377-
destination.write_from_bytes(self, &result)?;
2378+
destination.write_from_interval(self, result)?;
23782379
None
23792380
})
23802381
}
@@ -2680,11 +2681,12 @@ pub fn render_const_using_debug_impl(
26802681
) else {
26812682
not_supported!("std::fmt::format not found");
26822683
};
2683-
let message_string = evaluator.interpret_mir(
2684+
let interval = evaluator.interpret_mir(
26842685
db.mir_body(format_fn.into()).map_err(|e| MirEvalError::MirLowerError(format_fn, e))?,
26852686
[IntervalOrOwned::Borrowed(Interval { addr: a3, size: evaluator.ptr_size() * 6 })]
26862687
.into_iter(),
26872688
)?;
2689+
let message_string = interval.get(&evaluator)?;
26882690
let addr =
26892691
Address::from_bytes(&message_string[evaluator.ptr_size()..2 * evaluator.ptr_size()])?;
26902692
let size = from_bytes!(usize, message_string[2 * evaluator.ptr_size()..]);

crates/hir-ty/src/mir/eval/shim.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,13 @@ impl Evaluator<'_> {
322322
let hir_def::resolver::ValueNs::FunctionId(format_fn) = format_fn else {
323323
not_supported!("std::fmt::format is not a function")
324324
};
325-
let message_string = self.interpret_mir(
325+
let interval = self.interpret_mir(
326326
self.db
327327
.mir_body(format_fn.into())
328328
.map_err(|e| MirEvalError::MirLowerError(format_fn, e))?,
329329
args.map(|x| IntervalOrOwned::Owned(x.clone())),
330330
)?;
331+
let message_string = interval.get(self)?;
331332
let addr =
332333
Address::from_bytes(&message_string[self.ptr_size()..2 * self.ptr_size()])?;
333334
let size = from_bytes!(usize, message_string[2 * self.ptr_size()..]);

crates/hir-ty/src/mir/lower.rs

+29-28
Original file line numberDiff line numberDiff line change
@@ -540,15 +540,15 @@ impl<'ctx> MirLowerCtx<'ctx> {
540540
self.write_bytes_to_place(
541541
then_target,
542542
place.clone(),
543-
vec![1],
543+
Box::new([1]),
544544
TyBuilder::bool(),
545545
MirSpan::Unknown,
546546
)?;
547547
if let Some(else_target) = else_target {
548548
self.write_bytes_to_place(
549549
else_target,
550550
place,
551-
vec![0],
551+
Box::new([0]),
552552
TyBuilder::bool(),
553553
MirSpan::Unknown,
554554
)?;
@@ -602,7 +602,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
602602
generic_args,
603603
)
604604
.intern(Interner);
605-
let func = Operand::from_bytes(vec![], ty);
605+
let func = Operand::from_bytes(Box::default(), ty);
606606
return self.lower_call_and_args(
607607
func,
608608
iter::once(*callee).chain(args.iter().copied()),
@@ -615,7 +615,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
615615
let callee_ty = self.expr_ty_after_adjustments(*callee);
616616
match &callee_ty.kind(Interner) {
617617
chalk_ir::TyKind::FnDef(..) => {
618-
let func = Operand::from_bytes(vec![], callee_ty.clone());
618+
let func = Operand::from_bytes(Box::default(), callee_ty.clone());
619619
self.lower_call_and_args(
620620
func,
621621
args.iter().copied(),
@@ -1113,7 +1113,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
11131113
Some("start") => lp.take(),
11141114
Some("end") => rp.take(),
11151115
Some("exhausted") => {
1116-
Some(Operand::from_bytes(vec![0], TyBuilder::bool()))
1116+
Some(Operand::from_bytes(Box::new([0]), TyBuilder::bool()))
11171117
}
11181118
_ => None,
11191119
};
@@ -1395,46 +1395,47 @@ impl<'ctx> MirLowerCtx<'ctx> {
13951395
}
13961396

13971397
fn lower_literal_to_operand(&mut self, ty: Ty, l: &Literal) -> Result<Operand> {
1398-
let size = self
1399-
.db
1400-
.layout_of_ty(ty.clone(), self.db.trait_environment_for_body(self.owner))?
1401-
.size
1402-
.bytes_usize();
1403-
let bytes = match l {
1398+
let size = || {
1399+
self.db
1400+
.layout_of_ty(ty.clone(), self.db.trait_environment_for_body(self.owner))
1401+
.map(|it| it.size.bytes_usize())
1402+
};
1403+
const USIZE_SIZE: usize = mem::size_of::<usize>();
1404+
let bytes: Box<[_]> = match l {
14041405
hir_def::hir::Literal::String(b) => {
14051406
let b = b.as_bytes();
1406-
let mut data = Vec::with_capacity(mem::size_of::<usize>() * 2);
1407-
data.extend(0usize.to_le_bytes());
1408-
data.extend(b.len().to_le_bytes());
1407+
let mut data = Box::new([0; { 2 * USIZE_SIZE }]);
1408+
data[..USIZE_SIZE].copy_from_slice(&0usize.to_le_bytes());
1409+
data[USIZE_SIZE..].copy_from_slice(&b.len().to_le_bytes());
14091410
let mut mm = MemoryMap::default();
14101411
mm.insert(0, b.to_vec());
14111412
return Ok(Operand::from_concrete_const(data, mm, ty));
14121413
}
14131414
hir_def::hir::Literal::CString(b) => {
14141415
let bytes = b.iter().copied().chain(iter::once(0)).collect::<Vec<_>>();
14151416

1416-
let mut data = Vec::with_capacity(mem::size_of::<usize>() * 2);
1417-
data.extend(0usize.to_le_bytes());
1418-
data.extend(bytes.len().to_le_bytes());
1417+
let mut data = Box::new([0; { 2 * USIZE_SIZE }]);
1418+
data[..USIZE_SIZE].copy_from_slice(&0usize.to_le_bytes());
1419+
data[USIZE_SIZE..].copy_from_slice(&bytes.len().to_le_bytes());
14191420
let mut mm = MemoryMap::default();
14201421
mm.insert(0, bytes);
14211422
return Ok(Operand::from_concrete_const(data, mm, ty));
14221423
}
14231424
hir_def::hir::Literal::ByteString(b) => {
1424-
let mut data = Vec::with_capacity(mem::size_of::<usize>() * 2);
1425-
data.extend(0usize.to_le_bytes());
1426-
data.extend(b.len().to_le_bytes());
1425+
let mut data = Box::new([0; { 2 * USIZE_SIZE }]);
1426+
data[..USIZE_SIZE].copy_from_slice(&0usize.to_le_bytes());
1427+
data[USIZE_SIZE..].copy_from_slice(&b.len().to_le_bytes());
14271428
let mut mm = MemoryMap::default();
14281429
mm.insert(0, b.to_vec());
14291430
return Ok(Operand::from_concrete_const(data, mm, ty));
14301431
}
1431-
hir_def::hir::Literal::Char(c) => u32::from(*c).to_le_bytes().into(),
1432-
hir_def::hir::Literal::Bool(b) => vec![*b as u8],
1433-
hir_def::hir::Literal::Int(it, _) => it.to_le_bytes()[0..size].into(),
1434-
hir_def::hir::Literal::Uint(it, _) => it.to_le_bytes()[0..size].into(),
1435-
hir_def::hir::Literal::Float(f, _) => match size {
1436-
8 => f.into_f64().to_le_bytes().into(),
1437-
4 => f.into_f32().to_le_bytes().into(),
1432+
hir_def::hir::Literal::Char(c) => Box::new(u32::from(*c).to_le_bytes()),
1433+
hir_def::hir::Literal::Bool(b) => Box::new([*b as u8]),
1434+
hir_def::hir::Literal::Int(it, _) => Box::from(&it.to_le_bytes()[0..size()?]),
1435+
hir_def::hir::Literal::Uint(it, _) => Box::from(&it.to_le_bytes()[0..size()?]),
1436+
hir_def::hir::Literal::Float(f, _) => match size()? {
1437+
8 => Box::new(f.into_f64().to_le_bytes()),
1438+
4 => Box::new(f.into_f32().to_le_bytes()),
14381439
_ => {
14391440
return Err(MirLowerError::TypeError("float with size other than 4 or 8 bytes"))
14401441
}
@@ -1483,7 +1484,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
14831484
&mut self,
14841485
prev_block: BasicBlockId,
14851486
place: Place,
1486-
cv: Vec<u8>,
1487+
cv: Box<[u8]>,
14871488
ty: Ty,
14881489
span: MirSpan,
14891490
) -> Result<()> {

crates/hir-ty/src/mir/lower/pattern_matching.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ impl MirLowerCtx<'_> {
244244
);
245245
} else {
246246
let c = Operand::from_concrete_const(
247-
pattern_len.to_le_bytes().to_vec(),
247+
pattern_len.to_le_bytes().into(),
248248
MemoryMap::default(),
249249
TyBuilder::usize(),
250250
);

0 commit comments

Comments
 (0)