authorgravatar for fncontroloption@noreply.codeberg.orgFnControlOption <fncontroloption@noreply.codeberg.org> 2021-09-03 06:55:38-07:00
committergravatar for fncontroloption@noreply.codeberg.orgFnControlOption <fncontroloption@noreply.codeberg.org> 2021-09-03 06:55:38-07:00
log5672ee4ed7de11fa3c39ba43619035fdbfc507a5
tree00dc38a9218143b36e1eab2737b02a0ee6101d18
parentfdf1918b39c7554daeaa05e3c537ff9a83e9c3bd

AstGen: use string index as key for string table


1 files changed, 17 insertions(+), 15 deletions(-)

src/AstGen.zig+17-15
...@@ -7,6 +7,8 @@ const mem = std.mem;...@@ -7,6 +7,8 @@ const mem = std.mem;
7const Allocator = std.mem.Allocator;7const Allocator = std.mem.Allocator;
8const assert = std.debug.assert;8const assert = std.debug.assert;
9const ArrayListUnmanaged = std.ArrayListUnmanaged;9const ArrayListUnmanaged = std.ArrayListUnmanaged;
10const StringIndexAdapter = std.hash_map.StringIndexAdapter;
11const StringIndexContext = std.hash_map.StringIndexContext;
1012
11const Zir = @import("Zir.zig");13const Zir = @import("Zir.zig");
12const trace = @import("tracy.zig").trace;14const 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.
32arena: *Allocator,34arena: *Allocator,
33string_table: std.StringHashMapUnmanaged(u32) = .{},35string_table: std.HashMapUnmanaged(u32, void, StringIndexContext, std.hash_map.default_max_load_percentage) = .{},
34compile_errors: ArrayListUnmanaged(Zir.Inst.CompileErrors.Item) = .{},36compile_errors: ArrayListUnmanaged(Zir.Inst.CompileErrors.Item) = .{},
35/// The topmost block of the current function.37/// The topmost block of the current function.
36fn_block: ?*GenZir = null,38fn_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 memory8795 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 memory8822 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 table8823 // 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 to8824 // 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.