Skip to content

storing in a variable results in needless copying #48533

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

Closed
tspiteri opened this issue Feb 25, 2018 · 4 comments
Closed

storing in a variable results in needless copying #48533

tspiteri opened this issue Feb 25, 2018 · 4 comments
Labels
A-codegen Area: Code generation A-mir-opt Area: MIR optimizations C-enhancement Category: An issue proposing an enhancement or a PR with one. I-slow Issue: Problems and improvements with respect to performance of generated code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@tspiteri
Copy link
Contributor

With this lib.rs

pub struct S([i64; 2]);
pub fn foo(create: fn() -> S) -> S {
    create()
}
pub fn bar(create: fn() -> S) -> S {
    let s = create();
    s
}

rustc lib.rs --crate-type lib -C opt-level=3 --emit=asm produces this lib.s (cleaned up)

foo:
	pushq	%rbx
	movq	%rdi, %rbx
	callq	*%rsi
	movq	%rbx, %rax
	popq	%rbx
	retq
bar:
	pushq	%rbx
	subq	$16, %rsp
	movq	%rdi, %rbx
	movq	%rsp, %rdi
	callq	*%rsi
	movups	(%rsp), %xmm0
	movups	%xmm0, (%rbx)
	movq	%rbx, %rax
	addq	$16, %rsp
	popq	%rbx
	retq

The assembly for bar should not be longer than the assembly for foo.

This is similar to #42870. I don't know if it's a duplicate or just similar.

@pietroalbini pietroalbini added I-slow Issue: Problems and improvements with respect to performance of generated code. C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Feb 27, 2018
@dotdash
Copy link
Contributor

dotdash commented Mar 5, 2018

@steveklabnik
Copy link
Member

Triage: no change.

@slightlyoutofphase
Copy link
Contributor

slightlyoutofphase commented Dec 19, 2019

Currently those do generate identical assembly when you actually call them in a program. Playground link to demonstrate.

@jonas-schievink jonas-schievink added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-codegen Area: Code generation A-mir-opt Area: MIR optimizations labels Apr 20, 2020
@tspiteri
Copy link
Contributor Author

This is fixed in nightly, possibly due to #72205.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation A-mir-opt Area: MIR optimizations C-enhancement Category: An issue proposing an enhancement or a PR with one. I-slow Issue: Problems and improvements with respect to performance of generated code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants