@@ -201,7 +201,10 @@ fn putFn(self: *Plan9, decl_index: Module.Decl.Index, out: FnDeclOutput) !void {
201201 const decl = mod .declPtr (decl_index );
202202 const fn_map_res = try self .fn_decl_table .getOrPut (gpa , decl .getFileScope ());
203203 if (fn_map_res .found_existing ) {
204- try fn_map_res .value_ptr .functions .put (gpa , decl_index , out );
204+ if (try fn_map_res .value_ptr .functions .fetchPut (gpa , decl_index , out )) | old_entry | {
205+ gpa .free (old_entry .value .code );
206+ gpa .free (old_entry .value .lineinfo );
207+ }
205208 } else {
206209 const file = decl .getFileScope ();
207210 const arena = self .path_arena .allocator ();
@@ -408,9 +411,11 @@ pub fn updateDecl(self: *Plan9, module: *Module, decl_index: Module.Decl.Index)
408411 return ;
409412 },
410413 };
411- var duped_code = try self .base .allocator .dupe (u8 , code );
412- errdefer self .base .allocator .free (duped_code );
413- try self .data_decl_table .put (self .base .allocator , decl_index , duped_code );
414+ try self .data_decl_table .ensureUnusedCapacity (self .base .allocator , 1 );
415+ const duped_code = try self .base .allocator .dupe (u8 , code );
416+ if (self .data_decl_table .fetchPutAssumeCapacity (decl_index , duped_code )) | old_entry | {
417+ self .base .allocator .free (old_entry .value );
418+ }
414419 return self .updateFinish (decl );
415420}
416421/// called at the end of update{Decl,Func}
@@ -743,14 +748,19 @@ pub fn freeDecl(self: *Plan9, decl_index: Module.Decl.Index) void {
743748 if (is_fn ) {
744749 var symidx_and_submap = self .fn_decl_table .get (decl .getFileScope ()).? ;
745750 var submap = symidx_and_submap .functions ;
746- _ = submap .swapRemove (decl_index );
751+ if (submap .fetchSwapRemove (decl_index )) | removed_entry | {
752+ self .base .allocator .free (removed_entry .value .code );
753+ self .base .allocator .free (removed_entry .value .lineinfo );
754+ }
747755 if (submap .count () == 0 ) {
748756 self .syms .items [symidx_and_submap .sym_index ] = aout .Sym .undefined_symbol ;
749757 self .syms_index_free_list .append (self .base .allocator , symidx_and_submap .sym_index ) catch {};
750758 submap .deinit (self .base .allocator );
751759 }
752760 } else {
753- _ = self .data_decl_table .swapRemove (decl_index );
761+ if (self .data_decl_table .fetchSwapRemove (decl_index )) | removed_entry | {
762+ self .base .allocator .free (removed_entry .value );
763+ }
754764 }
755765 if (decl .link .plan9 .got_index ) | i | {
756766 // TODO: if this catch {} is triggered, an assertion in flushModule will be triggered, because got_index_free_list will have the wrong length
0 commit comments