Skip to content

Inefficient ref counting in else if expressions #385

Closed
@brson

Description

@brson

The way else if is currently translated the following code

auto x = if (a) {
  b
} else if (c) {
  d
} else {
  e
};

translates the same as

auto x = if (a) {
  b
} else {
  if (c) {
    d
  } else {
    e
  }
};

This creates a new sub-scope for each else if branch.

The mechanism for returning results from blocks entails copying the result into a new alloca and dropping any refs for it in the parent scope. These two things combined mean that an else if chain with n else if branches will generate allocas for n results and n take/drop pairs.

I suppose it could be possible to eliminate them in an optimization pass that eliminated redundant take/drops, but this also might be a good argument for reverting to the previous, non-recursive definition of else if.

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