Skip to content

Excessive stack allocation with multiple return points #19684

Closed
@Aatch

Description

@Aatch

This function uses ~1Kb of stack (with optimisations)

function test() -> [u8,...256] {
  let x : [u8, ..256];
  let val = get_val();
  if val == 0 {
    x = [1, ..256];
    return x;
  } else if val == 1 {
    x = [4, ..256];
    return x;
  } else {
    x = [9, ..256];
    return x;
  }
}

While this function uses ~256 bytes of stack:

function test() -> [u8,...256] {
  let x : [u8, ..256];
  let val = get_val();
  if val == 0 {
    x = [1, ..256];
  } else if val == 1 {
    x = [4, ..256];
  } else {
    x = [9, ..256];
  }

  return x;
}

As far as I can tell, each return statement gets given it's own slot which is copied into the return pointer at the end. This happens to also result in 4 memcpy calls in total.

/cc @gankro @cgaebel

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions