-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Speed up compilation of large constant arrays #51833
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
202aea5
63ab0cb
429bc8d
1ffa99d
8f969ed
c431f3f
84fe0c4
faef6a3
46512e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -594,6 +594,19 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> { | |
dest_align: Align, | ||
size: Size, | ||
nonoverlapping: bool, | ||
) -> EvalResult<'tcx> { | ||
self.copy_repeatedly(src, src_align, dest, dest_align, size, 1, nonoverlapping) | ||
} | ||
|
||
pub fn copy_repeatedly( | ||
&mut self, | ||
src: Scalar, | ||
src_align: Align, | ||
dest: Scalar, | ||
dest_align: Align, | ||
size: Size, | ||
length: u64, | ||
nonoverlapping: bool, | ||
) -> EvalResult<'tcx> { | ||
// Empty accesses don't need to be valid pointers, but they should still be aligned | ||
self.check_align(src, src_align)?; | ||
|
@@ -617,7 +630,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> { | |
.collect(); | ||
|
||
let src_bytes = self.get_bytes_unchecked(src, size, src_align)?.as_ptr(); | ||
let dest_bytes = self.get_bytes_mut(dest, size, dest_align)?.as_mut_ptr(); | ||
let dest_bytes = self.get_bytes_mut(dest, size * length, dest_align)?.as_mut_ptr(); | ||
|
||
// SAFE: The above indexing would have panicked if there weren't at least `size` bytes | ||
// behind `src` and `dest`. Also, we use the overlapping-safe `ptr::copy` if `src` and | ||
|
@@ -634,13 +647,18 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> { | |
)); | ||
} | ||
} | ||
ptr::copy(src_bytes, dest_bytes, size.bytes() as usize); | ||
|
||
for i in 0..length { | ||
ptr::copy(src_bytes, dest_bytes.offset((size.bytes() * i) as isize), size.bytes() as usize); | ||
} | ||
} else { | ||
ptr::copy_nonoverlapping(src_bytes, dest_bytes, size.bytes() as usize); | ||
for i in 0..length { | ||
ptr::copy_nonoverlapping(src_bytes, dest_bytes.offset((size.bytes() * i) as isize), size.bytes() as usize); | ||
} | ||
} | ||
} | ||
|
||
self.copy_undef_mask(src, dest, size)?; | ||
self.copy_undef_mask(src, dest, size * length)?; | ||
// copy back the relocations | ||
self.get_mut(dest.alloc_id)?.relocations.insert_presorted(relocations); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you need to reapeat this, too (and offset the indices). Try a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it, thanks! Can you double check my math? |
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -240,6 +240,7 @@ impl Size { | |
} | ||
} | ||
|
||
#[inline] | ||
pub fn bytes(self) -> u64 { | ||
self.raw | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this results in the correct result, it does
n^2/2
copies instead ofn
copies. Inside the function itself we should probably move theself.get(src.alloc_id)?
out of the loops, too. We can probably improve the nonoverlapping case enormously, too by not requiring an intermediate allocation.