Skip to content

Commit cf93401

Browse files
committed
rustup: more flexible write_bytes avoids allocations and removes itertools dependency
1 parent a05f2aa commit cf93401

File tree

6 files changed

+11
-26
lines changed

6 files changed

+11
-26
lines changed

Cargo.lock

Lines changed: 0 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ log = "0.4"
4040
shell-escape = "0.1.4"
4141
hex = "0.3.2"
4242
rand = "0.7"
43-
itertools = "0.8"
4443

4544
# A noop dependency that changes in the Rust repository, it's a bit of a hack.
4645
# See the `src/tools/rustc-workspace-hack/README.md` file in `rust-lang/rust`

rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
f466f52c1bf8f2e4454e31c683a88625ad4b4033
1+
55e00631e5bc5b16d40232914e57deeea197a8e4

src/helpers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::mem;
1+
use std::{mem, iter};
22
use std::ffi::{OsStr, OsString};
33

44
use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX};
@@ -428,7 +428,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
428428
return Ok(false);
429429
}
430430
// FIXME: We should use `Iterator::chain` instead when rust-lang/rust#65704 lands.
431-
self.eval_context_mut().memory.write_bytes(scalar, [bytes, &[0]].concat())?;
431+
self.eval_context_mut().memory.write_bytes(scalar, bytes.iter().copied().chain(iter::once(0u8)))?;
432432
Ok(true)
433433
}
434434
}

src/shims/foreign_items.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::convert::TryInto;
1+
use std::{iter, convert::TryInto};
22

33
use rustc::hir::def_id::DefId;
44
use rustc::mir;
@@ -52,7 +52,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
5252
if zero_init {
5353
// We just allocated this, the access is definitely in-bounds.
5454
this.memory
55-
.write_bytes(ptr.into(), itertools::repeat_n(0u8, size as usize))
55+
.write_bytes(ptr.into(), iter::repeat(0u8).take(size as usize))
5656
.unwrap();
5757
}
5858
Scalar::Ptr(ptr)
@@ -227,7 +227,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
227227
);
228228
// We just allocated this, the access is definitely in-bounds.
229229
this.memory
230-
.write_bytes(ptr.into(), itertools::repeat_n(0u8, size as usize))
230+
.write_bytes(ptr.into(), iter::repeat(0u8).take(size as usize))
231231
.unwrap();
232232
this.write_scalar(Scalar::Ptr(ptr), dest)?;
233233
}
@@ -839,7 +839,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
839839
let system_info = this.deref_operand(args[0])?;
840840
// Initialize with `0`.
841841
this.memory
842-
.write_bytes(system_info.ptr, itertools::repeat_n(0, system_info.layout.size.bytes() as usize))?;
842+
.write_bytes(system_info.ptr, iter::repeat(0u8).take(system_info.layout.size.bytes() as usize))?;
843843
// Set number of processors.
844844
let dword_size = Size::from_bytes(4);
845845
let num_cpus = this.mplace_field(system_info, 6)?;

src/shims/intrinsics.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::iter;
2+
13
use rustc_apfloat::Float;
24
use rustc::mir;
35
use rustc::mir::interpret::{InterpResult, PointerArithmetic};
@@ -357,7 +359,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
357359
// Do it in memory
358360
let mplace = this.force_allocation(dest)?;
359361
mplace.meta.unwrap_none(); // must be sized
360-
this.memory.write_bytes(mplace.ptr, itertools::repeat_n(0, dest.layout.size.bytes() as usize))?;
362+
this.memory.write_bytes(mplace.ptr, iter::repeat(0u8).take(dest.layout.size.bytes() as usize))?;
361363
}
362364
}
363365
}
@@ -562,7 +564,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
562564
let ptr = this.read_scalar(args[0])?.not_undef()?;
563565
let count = this.read_scalar(args[2])?.to_usize(this)?;
564566
let byte_count = ty_layout.size * count;
565-
this.memory.write_bytes(ptr, itertools::repeat_n(val_byte, byte_count.bytes() as usize))?;
567+
this.memory.write_bytes(ptr, iter::repeat(val_byte).take(byte_count.bytes() as usize))?;
566568
}
567569

568570
name => throw_unsup_format!("unimplemented intrinsic: {}", name),

0 commit comments

Comments
 (0)