Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Basic &mut noalias optimization in unsafe code #24

Open
@arielb1

Description

@arielb1
Contributor

One optimization we want for sure:

As long as <typeof(value) as Clone>::clone is known not to panic, this:

        unsafe {
            let mut ptr = self.buf.ptr();
            for i in 0..(self.len) {
                ptr::write(ptr, value.clone());
                ptr = ptr.offset(1);
                self.len += 1;
            }
        }

Must be optimizable to this:

        unsafe {
            let mut ptr = self.buf.ptr();
            for i in 0..(self.len) {
                ptr::write(ptr, value.clone());
                ptr = ptr.offset(1);
            }
            self.len += n;
        }

This optimization is important because the latter loop can be further optimized to a memcpy when value is Copy. However, this optimization requires that the memory referenced to by ptr not alias with self.len.

We used to do that optimization in older versions of rustc (using an LLVM noalias attribute, not in trans), but stopped emitting that attribute because LLVM's semantics miss the "is known not to panic" requirement. If we deal with that issue, we would like to apply noalias again.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @arielb1

        Issue actions

          Basic `&mut` noalias optimization in unsafe code · Issue #24 · rust-lang/rust-memory-model