Why rustc cannot optimize sequence of copies while clang does

I'm writing an open source library for Rust, and would like to serve an API that writes &'static str wrapped with double quotation mark (") into memory. Here is a simplified code:

Assembly output says it writes double quotation mark first, write contents, and then double quotation mark again.

I implemented the exactly same operation in C, but clang optimize this as I expected.

What surprised me more is, if I removed the test2 function from above Rust code, LLVM properly optimize the operations.

Could you tell me why things happen, and how can I make things work?

1 Like

The direct translation from C to Rust is compiled correctly, so this might be a LLVM bug or some weird codegen around &'static str

@SkiFire13 Thank you very much for your prompt response.

It's a stupid question but, where can I report this bug to the compiler team or LLVM?

Issues · rust-lang/rust · GitHub – please search for related issues first to make sure it's not a duplicate.

@H2CO3 Thank you for the advice. I'll try to search similar issues.

After some tests, looks like that using a &str as argument for write_unquoted allows it to be better optimized because it guarantees that src and dst aren't overlapped, however this probably comes back and bite us when it is inlined in test1 and test2.

@SkiFire13 Sorry for the delayed reply.

In my case the data argument is mostly evaluatable at compile time. Thus I'd like to join a " character with string contents at compile time.

I created a new issue in the Rust official repository.