| ... | @@ -43,9 +43,10 @@ pub const BufMap = struct { | ... | @@ -43,9 +43,10 @@ pub const BufMap = struct { |
| 43 | pub fn set(self: *BufMap, key: []const u8, value: []const u8) !void { | 43 | pub fn set(self: *BufMap, key: []const u8, value: []const u8) !void { |
| 44 | const value_copy = try self.copy(value); | 44 | const value_copy = try self.copy(value); |
| 45 | errdefer self.free(value_copy); | 45 | errdefer self.free(value_copy); |
| 46 | // Avoid copying key if it already exists | | |
| 47 | const get_or_put = try self.hash_map.getOrPut(key); | 46 | const get_or_put = try self.hash_map.getOrPut(key); |
| 48 | if (!get_or_put.found_existing) { | 47 | if (get_or_put.found_existing) { |
| | 48 | self.free(get_or_put.kv.value); |
| | 49 | } else { |
| 49 | get_or_put.kv.key = self.copy(key) catch |err| { | 50 | get_or_put.kv.key = self.copy(key) catch |err| { |
| 50 | _ = self.hash_map.remove(key); | 51 | _ = self.hash_map.remove(key); |
| 51 | return err; | 52 | return err; |
| ... | @@ -83,8 +84,7 @@ pub const BufMap = struct { | ... | @@ -83,8 +84,7 @@ pub const BufMap = struct { |
| 83 | }; | 84 | }; |
| 84 | | 85 | |
| 85 | test "BufMap" { | 86 | test "BufMap" { |
| 86 | // TODO: uncomment and fix the leak | 87 | var bufmap = BufMap.init(std.testing.allocator); |
| 87 | var bufmap = BufMap.init(std.heap.page_allocator); | | |
| 88 | defer bufmap.deinit(); | 88 | defer bufmap.deinit(); |
| 89 | | 89 | |
| 90 | try bufmap.set("x", "1"); | 90 | try bufmap.set("x", "1"); |