Skip to content

storing in a variable results in needless copying #48533

Closed
@tspiteri

Description

@tspiteri

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: Code generationA-mir-optArea: MIR optimizationsC-enhancementCategory: An issue proposing an enhancement or a PR with one.I-slowIssue: Problems and improvements with respect to performance of generated code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions