use copy_from_slice instead of for loop (#5647)
This commit is contained in:
parent
b1eab698d2
commit
6d81b8a49a
@ -86,10 +86,7 @@ impl Memory for Vec<u8> {
|
|||||||
fn write_slice(&mut self, offset: U256, slice: &[u8]) {
|
fn write_slice(&mut self, offset: U256, slice: &[u8]) {
|
||||||
let off = offset.low_u64() as usize;
|
let off = offset.low_u64() as usize;
|
||||||
|
|
||||||
// TODO [todr] Optimize?
|
self[off..off+slice.len()].copy_from_slice(slice);
|
||||||
for pos in off..off+slice.len() {
|
|
||||||
self[pos] = slice[pos - off];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write(&mut self, offset: U256, value: U256) {
|
fn write(&mut self, offset: U256, value: U256) {
|
||||||
@ -114,31 +111,57 @@ impl Memory for Vec<u8> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use util::U256;
|
||||||
|
use super::Memory;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_memory_read_and_write() {
|
fn test_memory_read_and_write() {
|
||||||
// given
|
// given
|
||||||
let mem: &mut Memory = &mut vec![];
|
let mem: &mut Memory = &mut vec![];
|
||||||
mem.resize(0x80 + 32);
|
mem.resize(0x80 + 32);
|
||||||
|
|
||||||
// when
|
// when
|
||||||
mem.write(U256::from(0x80), U256::from(0xabcdef));
|
mem.write(U256::from(0x80), U256::from(0xabcdef));
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assert_eq!(mem.read(U256::from(0x80)), U256::from(0xabcdef));
|
assert_eq!(mem.read(U256::from(0x80)), U256::from(0xabcdef));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_memory_read_and_write_byte() {
|
fn test_memory_read_and_write_byte() {
|
||||||
// given
|
// given
|
||||||
let mem: &mut Memory = &mut vec![];
|
let mem: &mut Memory = &mut vec![];
|
||||||
mem.resize(32);
|
mem.resize(32);
|
||||||
|
|
||||||
// when
|
// when
|
||||||
mem.write_byte(U256::from(0x1d), U256::from(0xab));
|
mem.write_byte(U256::from(0x1d), U256::from(0xab));
|
||||||
mem.write_byte(U256::from(0x1e), U256::from(0xcd));
|
mem.write_byte(U256::from(0x1e), U256::from(0xcd));
|
||||||
mem.write_byte(U256::from(0x1f), U256::from(0xef));
|
mem.write_byte(U256::from(0x1f), U256::from(0xef));
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assert_eq!(mem.read(U256::from(0x00)), U256::from(0xabcdef));
|
assert_eq!(mem.read(U256::from(0x00)), U256::from(0xabcdef));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_memory_read_slice_and_write_slice() {
|
||||||
|
let mem: &mut Memory = &mut vec![];
|
||||||
|
mem.resize(32);
|
||||||
|
|
||||||
|
{
|
||||||
|
let slice = "abcdefghijklmnopqrstuvwxyz012345".as_bytes();
|
||||||
|
mem.write_slice(U256::from(0), slice);
|
||||||
|
|
||||||
|
assert_eq!(mem.read_slice(U256::from(0), U256::from(32)), slice);
|
||||||
|
}
|
||||||
|
|
||||||
|
// write again
|
||||||
|
{
|
||||||
|
let slice = "67890".as_bytes();
|
||||||
|
mem.write_slice(U256::from(0x1), slice);
|
||||||
|
|
||||||
|
assert_eq!(mem.read_slice(U256::from(0), U256::from(7)), "a67890g".as_bytes());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user