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;
77const Allocator = std.mem.Allocator;
88const assert = std.debug.assert;
99const ArrayListUnmanaged = std.ArrayListUnmanaged;
10const StringIndexAdapter = std.hash_map.StringIndexAdapter;
11const StringIndexContext = std.hash_map.StringIndexContext;
1012
1113const Zir = @import("Zir.zig");
1214const trace = @import("tracy.zig").trace;
......@@ -30,7 +32,7 @@ source_column: u32 = 0,
3032/// Used for temporary allocations; freed after AstGen is complete.
3133/// The resulting ZIR code has no references to anything in this arena.
3234arena: *Allocator,
33string_table: std.StringHashMapUnmanaged(u32) = .{},
35string_table: std.HashMapUnmanaged(u32, void, StringIndexContext, std.hash_map.default_max_load_percentage) = .{},
3436compile_errors: ArrayListUnmanaged(Zir.Inst.CompileErrors.Item) = .{},
3537/// The topmost block of the current function.
3638fn_block: ?*GenZir = null,
......@@ -8781,16 +8783,16 @@ fn identAsString(astgen: *AstGen, ident_token: Ast.TokenIndex) !u32 {
87818783 const str_index = @intCast(u32, string_bytes.items.len);
87828784 try astgen.appendIdentStr(ident_token, string_bytes);
87838785 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 });
87858791 if (gop.found_existing) {
87868792 string_bytes.shrinkRetainingCapacity(str_index);
8787 return gop.value_ptr.*;
8793 return gop.key_ptr.*;
87888794 } else {
8789 // We have to dupe the key into the arena, otherwise the memory
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;
8795 gop.key_ptr.* = str_index;
87948796 try string_bytes.append(gpa, 0);
87958797 return str_index;
87968798 }
......@@ -8805,19 +8807,19 @@ fn strLitAsString(astgen: *AstGen, str_lit_token: Ast.TokenIndex) !IndexSlice {
88058807 const token_bytes = astgen.tree.tokenSlice(str_lit_token);
88068808 try astgen.parseStrLit(str_lit_token, string_bytes, token_bytes, 0);
88078809 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 });
88098815 if (gop.found_existing) {
88108816 string_bytes.shrinkRetainingCapacity(str_index);
88118817 return IndexSlice{
8812 .index = gop.value_ptr.*,
8818 .index = gop.key_ptr.*,
88138819 .len = @intCast(u32, key.len),
88148820 };
88158821 } else {
8816 // We have to dupe the key into the arena, otherwise the memory
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;
8822 gop.key_ptr.* = str_index;
88218823 // Still need a null byte because we are using the same table
88228824 // to lookup null terminated strings, so if we get a match, it has to
88238825 // be null terminated for that to work.