From 2e398490e1f8d5ee29941633e8427d1a17cf6ce7 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Sat, 22 Oct 2022 22:20:38 +0200 Subject: [PATCH] Avoid calling String droppers where possible In cases where we statically know a type to be String, we can bypass the dropper and just drop the String directly. Changelog: performance --- compiler/src/mir/passes.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/compiler/src/mir/passes.rs b/compiler/src/mir/passes.rs index b9489fed4..3ed5d27aa 100644 --- a/compiler/src/mir/passes.rs +++ b/compiler/src/mir/passes.rs @@ -4509,6 +4509,7 @@ 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); @@ -4516,9 +4517,21 @@ impl<'a> ExpandDrop<'a> { 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);