How to know where moves are memcpys?

Is there a way to get information about where in a Rust app moves are likely to be implemented by memory copy instead of a no-op? I know that the Rust compiler reserves the right to decide this differently based on optimization level and whim. But I'd like to know which cases I can expect to not be memcpys in a release, so that I can address those that might be memcpys by re-organizing the code. I'm only concerned with a few large-ish arrays I have.

I'm trying to get away from the C-style pass a pointer to raw memory and let the array get initialized there idiom and use the Rust preferred call-and-return-by-value style. But for some arrays that's making me wonder about memcpys I should try to avoid.

Inspect the assembly. Really, there is no other way (but godbolt is a goldmine).

Also, moves doesn't have to be memcpys, they can also be direct moving instructions in assembly.

5 Likes

@chrefr is right that the only truly reliable way is to look at the emitted code.

There's this (de-facto unstable) lint https://doc.rust-lang.org/rustc/lints/listing/warn-by-default.html#large-assignments, but it will also warn about things that are actually not an issue at all, just happen to be big.

1 Like

That would be helpful. But, for some reason, I'm not getting that lint warning in my IDE (emacs rustic mode).

As others have said, read the assembly, but also the compiler is free to optimize the return value so even returning a large object doesn't necessarily entail a memcpy.

2 Likes

How do I turn on the unstable large assignments lint?

I can't get it to work in the playground:

This playground, when disassembled, shows a large memcpy in foo. But the large assignment lint doesn't show up - it builds without any warnings at all.

Is there a way to annotate a spot in Rust code so as to make it easy to find that spot in optimized assembly output, without impacting the Rust compiler's optimization decisions?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.