Optimize large array creation in const-eval
This changes repeated memcpy's to a memset for the case that we're propagating a single byte into a region of memory.
This commit is contained in:
parent
c58a5da7d4
commit
e68f3039d4
1 changed files with 19 additions and 12 deletions
|
@ -1209,21 +1209,28 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
|
||||||
throw_ub_custom!(fluent::const_eval_copy_nonoverlapping_overlapping);
|
throw_ub_custom!(fluent::const_eval_copy_nonoverlapping_overlapping);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for i in 0..num_copies {
|
let size_in_bytes = size.bytes_usize();
|
||||||
ptr::copy(
|
// For particularly large arrays (where this is perf-sensitive) it's common that
|
||||||
src_bytes,
|
// we're writing a single byte repeatedly. So, optimize that case to a memset.
|
||||||
dest_bytes.add((size * i).bytes_usize()), // `Size` multiplication
|
if size_in_bytes == 1 && num_copies >= 1 {
|
||||||
size.bytes_usize(),
|
// SAFETY: `src_bytes` would be read from anyway by copies below (num_copies >= 1).
|
||||||
);
|
// Since size_in_bytes = 1, then the `init.no_bytes_init()` check above guarantees
|
||||||
|
// that this read at type `u8` is OK -- it must be an initialized byte.
|
||||||
|
let value = *src_bytes;
|
||||||
|
dest_bytes.write_bytes(value, (size * num_copies).bytes_usize());
|
||||||
|
} else if src_alloc_id == dest_alloc_id {
|
||||||
|
let mut dest_ptr = dest_bytes;
|
||||||
|
for _ in 0..num_copies {
|
||||||
|
ptr::copy(src_bytes, dest_ptr, size_in_bytes);
|
||||||
|
dest_ptr = dest_ptr.add(size_in_bytes);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for i in 0..num_copies {
|
let mut dest_ptr = dest_bytes;
|
||||||
ptr::copy_nonoverlapping(
|
for _ in 0..num_copies {
|
||||||
src_bytes,
|
ptr::copy_nonoverlapping(src_bytes, dest_ptr, size_in_bytes);
|
||||||
dest_bytes.add((size * i).bytes_usize()), // `Size` multiplication
|
dest_ptr = dest_ptr.add(size_in_bytes);
|
||||||
size.bytes_usize(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue