From e551e59c1caacdf0fbafcb8b15a3902520833d59 Mon Sep 17 00:00:00 2001 From: Brendan Hansknecht Date: Tue, 11 Oct 2022 07:56:34 -0700 Subject: [PATCH] refactor string reallocation --- crates/compiler/builtins/bitcode/src/str.zig | 28 ++++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/crates/compiler/builtins/bitcode/src/str.zig b/crates/compiler/builtins/bitcode/src/str.zig index 3b3463f5f4..669a0131c6 100644 --- a/crates/compiler/builtins/bitcode/src/str.zig +++ b/crates/compiler/builtins/bitcode/src/str.zig @@ -157,27 +157,27 @@ pub const RocStr = extern struct { const element_width = 1; const old_capacity = self.getCapacity(); + if (self.isSmallStr() or !self.isUnique()) { + return self.reallocateFresh(new_length); + } + if (self.str_bytes) |source_ptr| { - const unique = self.isUnique(); - if (unique and old_capacity > new_length) { + if (old_capacity > new_length) { var output = self; output.setLen(new_length); return output; } - if (unique and !self.isSmallStr()) { - const new_capacity = utils.calculateCapacity(old_capacity, new_length, element_width); - const new_source = utils.unsafeReallocate( - source_ptr, - RocStr.alignment, - old_capacity, - new_capacity, - element_width, - ); + const new_capacity = utils.calculateCapacity(old_capacity, new_length, element_width); + const new_source = utils.unsafeReallocate( + source_ptr, + RocStr.alignment, + old_capacity, + new_capacity, + element_width, + ); - return RocStr{ .str_bytes = new_source, .str_len = new_length, .str_capacity = new_capacity }; - } + return RocStr{ .str_bytes = new_source, .str_len = new_length, .str_capacity = new_capacity }; } - return self.reallocateFresh(new_length); }