Skip to content

Automatic named return and move optimization when local variables are returned #55

@ramonasuncion

Description

@ramonasuncion

Returning a local variable currently copies, and avoiding the copy requires either an explicit return move(a); or use of named returns. The compiler could however transform returned local variables into safe named returns or moves on the user's behalf, like most C++ compilers do. For example,

func() {
    foo();
    var a = bar();
    bas();
    var b = zim();
    zang();
    return a, b, zung();
}

could be transformed into

func() --> a:Bar, b:Zim, c:Zung {
    foo();
    a <-- bar(); onerror destroy(a);
    bas();
    b <-- zim(); onerror destroy(b);
    zang();
    c <-- zung(); return;
}

In cases when a local variable cannot be safely emplaced in the return value, automatic move optimization can be used as a more general optimization.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions