Skip to content

Commit

Permalink
Avoid calling String droppers where possible
Browse files Browse the repository at this point in the history
In cases where we statically know a type to be String, we can bypass the
dropper and just drop the String directly.

Changelog: performance
  • Loading branch information
yorickpeterse committed Oct 22, 2022
1 parent 366d723 commit 2e39849
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions compiler/src/mir/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4509,16 +4509,29 @@ impl<'a> ExpandDrop<'a> {
value: RegisterId,
location: LocationId,
) {
let stype = self.method.id.self_type(self.db);
let drop_id = self.add_block();
let check_reg = self.method.registers.alloc(types::TypeRef::boolean());
let check = self.block_mut(before_id);

check.decrement_atomic(check_reg, value, location);
check.branch(check_reg, drop_id, after_id, location);

// Atomic values can't be pattern matched into sub-values, so we can
// call the dropper unconditionally.
self.call_dropper(drop_id, value, location);
if self.method.registers.value_type(value).is_string(self.db, stype) {
// Strings can be dropped directly instead of going through the
// dropper.
self.block_mut(drop_id).raw_instruction(
Opcode::StringDrop,
vec![value],
location,
);
self.block_mut(drop_id).free(value, location);
} else {
// Atomic values can't be pattern matched into sub-values, so we can
// call the dropper unconditionally.
self.call_dropper(drop_id, value, location);
}

self.block_mut(drop_id).goto(after_id, location);

self.method.body.add_edge(before_id, drop_id);
Expand Down

0 comments on commit 2e39849

Please sign in to comment.