Skip to content

Commit

Permalink
Merge pull request #786 from candy-lang/use-extract-if
Browse files Browse the repository at this point in the history
Use `vec.extract_if(…)` for constant lifting
  • Loading branch information
jwbot authored Nov 7, 2023
2 parents f6cb3bc + d5f3bda commit f79988f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/compiler.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ jobs:
benchmark:
name: Benchmark
runs-on: ubuntu-latest
# Benchmarks on pushes to main should run sequentially.
concurrency: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' && format('{0}-{1}-{2}', github.workflow, github.job, github.ref) || github.run_id }}
steps:
- uses: actions/checkout@v4
- uses: dsherret/rust-toolchain-file@v1
Expand Down
1 change: 1 addition & 0 deletions compiler/frontend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
anonymous_lifetime_in_impl_trait,
box_patterns,
entry_insert,
extract_if,
hasher_prefixfree_extras,
io_error_more,
let_chains,
Expand Down
54 changes: 30 additions & 24 deletions compiler/frontend/src/mir_optimize/constant_lifting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,41 +37,47 @@
use super::current_expression::{Context, CurrentExpression};
use crate::mir::Expression;
use itertools::Itertools;

pub fn lift_constants(context: &mut Context, expression: &mut CurrentExpression) {
let Expression::Function { body, .. } = &mut **expression else {
return;
};

let mut constants = vec![];
let return_value = body.return_value();
let mut new_return_reference_target = None;
let constants = body
.expressions
.extract_if(|(id, expression)| {
let id = *id;

let mut index = 0;
while index < body.expressions.len() {
let (id, expression) = &body.expressions[index];
let id = *id;
if !context.pureness.is_definition_const(expression) {
return false;
}

if !context.pureness.is_definition_const(expression) {
index += 1;
continue;
}
let is_return_value = id == return_value;
if is_return_value && let Expression::Reference(_) = expression {
// Returned references shouldn't be lifted. If we would lift one,
// we'd have to add a reference anyway.
return false;
}

let is_return_value = id == body.return_value();
if is_return_value && let Expression::Reference(_) = expression {
// Returned references shouldn't be lifted. If we would lift one,
// we'd have to add a reference anyway.
index += 1;
continue;
}
// This is a constant and should be lifted.

// This is a constant and should be lifted.
if is_return_value {
// The return value was removed. Add a reference to the lifted
// constant.
new_return_reference_target = Some(id);
}
true
})
.collect_vec();

constants.push(body.expressions.remove(index));

if is_return_value {
// The return value was removed. Add a reference to the lifted
// constant.
body.push(context.id_generator.generate(), Expression::Reference(id));
}
if let Some(new_return_reference_target) = new_return_reference_target {
body.push(
context.id_generator.generate(),
Expression::Reference(new_return_reference_target),
);
}

expression.prepend_optimized(context.visible, constants);
Expand Down

1 comment on commit f79988f

@jwbot
Copy link
Collaborator Author

@jwbot jwbot commented on f79988f Nov 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compiler

Benchmark suite Current: f79988f Previous: f6cb3bc Ratio
Time: Compiler/hello_world 26515438 ns/iter (± 1567297) 24576634 ns/iter (± 829356) 1.08
Time: Compiler/fibonacci 232722061 ns/iter (± 5044822) 181201489 ns/iter (± 1687475) 1.28
Time: VM Runtime/hello_world 56300 ns/iter (± 14330) 57952 ns/iter (± 3835) 0.97
Time: VM Runtime/fibonacci/15 96643421 ns/iter (± 3874637) 78439800 ns/iter (± 705845) 1.23
Time: VM Runtime/PLB/binarytrees/6 1503641251 ns/iter (± 27349683) 1200379040 ns/iter (± 2038348) 1.25

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.