| ... | @@ -7,6 +7,8 @@ const mem = std.mem; | ... | @@ -7,6 +7,8 @@ const mem = std.mem; |
| 7 | const Allocator = std.mem.Allocator; | 7 | const Allocator = std.mem.Allocator; |
| 8 | const assert = std.debug.assert; | 8 | const assert = std.debug.assert; |
| 9 | const ArrayListUnmanaged = std.ArrayListUnmanaged; | 9 | const ArrayListUnmanaged = std.ArrayListUnmanaged; |
| | 10 | const StringIndexAdapter = std.hash_map.StringIndexAdapter; |
| | 11 | const StringIndexContext = std.hash_map.StringIndexContext; |
| 10 | | 12 | |
| 11 | const Zir = @import("Zir.zig"); | 13 | const Zir = @import("Zir.zig"); |
| 12 | const trace = @import("tracy.zig").trace; | 14 | const trace = @import("tracy.zig").trace; |
| ... | @@ -30,7 +32,7 @@ source_column: u32 = 0, | ... | @@ -30,7 +32,7 @@ source_column: u32 = 0, |
| 30 | /// Used for temporary allocations; freed after AstGen is complete. | 32 | /// Used for temporary allocations; freed after AstGen is complete. |
| 31 | /// The resulting ZIR code has no references to anything in this arena. | 33 | /// The resulting ZIR code has no references to anything in this arena. |
| 32 | arena: *Allocator, | 34 | arena: *Allocator, |
| 33 | string_table: std.StringHashMapUnmanaged(u32) = .{}, | 35 | string_table: std.HashMapUnmanaged(u32, void, StringIndexContext, std.hash_map.default_max_load_percentage) = .{}, |
| 34 | compile_errors: ArrayListUnmanaged(Zir.Inst.CompileErrors.Item) = .{}, | 36 | compile_errors: ArrayListUnmanaged(Zir.Inst.CompileErrors.Item) = .{}, |
| 35 | /// The topmost block of the current function. | 37 | /// The topmost block of the current function. |
| 36 | fn_block: ?*GenZir = null, | 38 | fn_block: ?*GenZir = null, |
| ... | @@ -8781,16 +8783,16 @@ fn identAsString(astgen: *AstGen, ident_token: Ast.TokenIndex) !u32 { | ... | @@ -8781,16 +8783,16 @@ fn identAsString(astgen: *AstGen, ident_token: Ast.TokenIndex) !u32 { |
| 8781 | const str_index = @intCast(u32, string_bytes.items.len); | 8783 | const str_index = @intCast(u32, string_bytes.items.len); |
| 8782 | try astgen.appendIdentStr(ident_token, string_bytes); | 8784 | try astgen.appendIdentStr(ident_token, string_bytes); |
| 8783 | const key = string_bytes.items[str_index..]; | 8785 | const key = string_bytes.items[str_index..]; |
| 8784 | const gop = try astgen.string_table.getOrPut(gpa, key); | 8786 | const gop = try astgen.string_table.getOrPutContextAdapted(gpa, @as([]const u8, key), StringIndexAdapter{ |
| | 8787 | .bytes = string_bytes, |
| | 8788 | }, StringIndexContext{ |
| | 8789 | .bytes = string_bytes, |
| | 8790 | }); |
| 8785 | if (gop.found_existing) { | 8791 | if (gop.found_existing) { |
| 8786 | string_bytes.shrinkRetainingCapacity(str_index); | 8792 | string_bytes.shrinkRetainingCapacity(str_index); |
| 8787 | return gop.value_ptr.*; | 8793 | return gop.key_ptr.*; |
| 8788 | } else { | 8794 | } else { |
| 8789 | // We have to dupe the key into the arena, otherwise the memory | 8795 | gop.key_ptr.* = str_index; |
| 8790 | // becomes invalidated when string_bytes gets data appended. | | |
| 8791 | // TODO https://github.com/ziglang/zig/issues/8528 | | |
| 8792 | gop.key_ptr.* = try astgen.arena.dupe(u8, key); | | |
| 8793 | gop.value_ptr.* = str_index; | | |
| 8794 | try string_bytes.append(gpa, 0); | 8796 | try string_bytes.append(gpa, 0); |
| 8795 | return str_index; | 8797 | return str_index; |
| 8796 | } | 8798 | } |
| ... | @@ -8805,19 +8807,19 @@ fn strLitAsString(astgen: *AstGen, str_lit_token: Ast.TokenIndex) !IndexSlice { | ... | @@ -8805,19 +8807,19 @@ fn strLitAsString(astgen: *AstGen, str_lit_token: Ast.TokenIndex) !IndexSlice { |
| 8805 | const token_bytes = astgen.tree.tokenSlice(str_lit_token); | 8807 | const token_bytes = astgen.tree.tokenSlice(str_lit_token); |
| 8806 | try astgen.parseStrLit(str_lit_token, string_bytes, token_bytes, 0); | 8808 | try astgen.parseStrLit(str_lit_token, string_bytes, token_bytes, 0); |
| 8807 | const key = string_bytes.items[str_index..]; | 8809 | const key = string_bytes.items[str_index..]; |
| 8808 | const gop = try astgen.string_table.getOrPut(gpa, key); | 8810 | const gop = try astgen.string_table.getOrPutContextAdapted(gpa, @as([]const u8, key), StringIndexAdapter{ |
| | 8811 | .bytes = string_bytes, |
| | 8812 | }, StringIndexContext{ |
| | 8813 | .bytes = string_bytes, |
| | 8814 | }); |
| 8809 | if (gop.found_existing) { | 8815 | if (gop.found_existing) { |
| 8810 | string_bytes.shrinkRetainingCapacity(str_index); | 8816 | string_bytes.shrinkRetainingCapacity(str_index); |
| 8811 | return IndexSlice{ | 8817 | return IndexSlice{ |
| 8812 | .index = gop.value_ptr.*, | 8818 | .index = gop.key_ptr.*, |
| 8813 | .len = @intCast(u32, key.len), | 8819 | .len = @intCast(u32, key.len), |
| 8814 | }; | 8820 | }; |
| 8815 | } else { | 8821 | } else { |
| 8816 | // We have to dupe the key into the arena, otherwise the memory | 8822 | gop.key_ptr.* = str_index; |
| 8817 | // becomes invalidated when string_bytes gets data appended. | | |
| 8818 | // TODO https://github.com/ziglang/zig/issues/8528 | | |
| 8819 | gop.key_ptr.* = try astgen.arena.dupe(u8, key); | | |
| 8820 | gop.value_ptr.* = str_index; | | |
| 8821 | // Still need a null byte because we are using the same table | 8823 | // Still need a null byte because we are using the same table |
| 8822 | // to lookup null terminated strings, so if we get a match, it has to | 8824 | // to lookup null terminated strings, so if we get a match, it has to |
| 8823 | // be null terminated for that to work. | 8825 | // be null terminated for that to work. |